#!/bin/bash
#
# Usage: ./make_man
#
# Generate man files for debian package.
# To work, PyFAI must be installed on the system.

OUTPUT_DIR=man

# TODO: Extract it in a better way (maybe from scripts themself)
CHECK_CALIB_DESCRIPTION="Is the poni correct?"
DETECTOR2NEXUS_DESCRIPTION="Create NeXus detector definition"
DIFF_MAP_DESCRIPTION="Azimuthal integration for diffraction imaging"
DIFF_TOMO_DESCRIPTION="Azimuthal integration for diffraction tomograph"
PYFAI_DRAWMASK_DESCRIPTION="Draw a mask for integration"
EIGER_MASK_DESCRIPTION="Extract mask from an Eiger detector file"
MX_CALIBRATE_DESCRIPTION="Validate detector position for MX beamlines"
PYFAI_AVERAGE_DESCRIPTION="Image preprocessing tool"
PYFAI_BENCHMARK_DESCRIPTION="Evaluate performances of PyFAI"
PYFAI_CALIB_DESCRIPTION="Calibration tool"
PYFAI_INTEGRATE_DESCRIPTION="Graphical interface for azimuthal integration"
PYFAI_RECALIB_DESCRIPTION="Deprecated"
PYFAI_SAXS_DESCRIPTION="Integration in q-range"
PYFAI_WAXS_DESCRIPTION="Powder diffraction integration"

if [ ! -d $OUTPUT_DIR ]
    then
    mkdir $OUTPUT_DIR
    fi
# Build the project before, to be able to display logs
python setup.py build
version=$(./bootstrap.py ./version.py |tail -n 1)
echo $version

export PATH=./build/scripts-2.7:$PATH
export PYTHONPATH=./build/lib.linux-x86_64-2.7

for i in scripts/*;
    do
    script_name=$(basename $i)
    echo "Generate $script_name man"

	# Read description from script variables defined above
    script_description=${script_name^^}
	script_description=${script_description/-/_}
	script_description=`eval echo "\\\$${script_description}_DESCRIPTION"`

    help2man --no-discard-stderr --version-string=$version -N -n "$script_description" -s1 -S PyFAI -o $OUTPUT_DIR/$script_name.1 $script_name
    man -t $OUTPUT_DIR/$script_name.1 | ps2pdf - $OUTPUT_DIR/$script_name.pdf
#    man2html $OUTPUT_DIR/$script_name.1 > $OUTPUT_DIR/$script_name.html
#    pandoc -t rst $OUTPUT_DIR/$script_name.html -o $OUTPUT_DIR/$script_name.rst
    done
