#!/bin/bash


# HERE BEGINS THE SCRIPT

SCRIPTNAME=$(basename $0)
SCRIPTBASE=$(dirname $0)/..
#make SCRIPTBASE fully qualified
SCRIPTBASE=$( cd $SCRIPTBASE && pwd )

source $SCRIPTBASE/bin/env.sh

usage()
{
    echo "\nUsage:\n $(basename $0) xmlDescriptorFile.xml <VIS2|VISamp|VISphi|T3amp|T3phi> <groupId>\n"   
}

echo "<response>"
trap beforeExit 0 1 2 5 15

# check cmd line
if test $# -lt 1
then
    echoPre "Missing file as argument"
    usage 
    exit 1
fi
# Store full path of INPUTFILE if given path was not absolute
INPUTFILE=$1
if test ${INPUTFILE:0:1} != "/" 
then
    INPUTFILE=$PWD/$1
fi
shift
# Handle arguments that should be one of form given in usage function
if [ $# -eq 2 ]
then
    GROUP_ARG=$2
    ARGS="\"$1\" , groups=$2"
else
    echoXmlErrorMessage "Wrong arguments:\n $(usage)"
    echoXmlComment "DEBUG: EXITING"
    exit 1
fi

echoXmlComment "DEBUG: ENTERING"
echoXmlComment "DEBUG: using $SCRIPTNAME script"
echoXmlComment "DEBUG: working base is $SCRIPTBASE"
echoXmlComment "DEBUG: path is $PATH"
echoXmlComment "DEBUG: $(date) : starting"

# Just do a simple log to get traces
echo "$(date) : $SCRIPTNAME $ARGS starts" >> $SCRIPTBASE/log/yoga.log
echoXmlComment "DEBUG: using logfile '$SCRIPTBASE/log/yoga.log'"

# creating one working area
TMPDIR=$SCRIPTBASE/tmp
if ! WORKING_DIRECTORY=$(mktemp -d)
then
    echoXmlErrorMessage "can't create working directory"
    echoXmlComment "DEBUG: EXITING"
    exit 1
fi
#trap "rm -rf $WORKING_DIRECTORY" 0 1 2 5 15

echoXmlComment "DEBUG: INPUTFILE is $INPUTFILE"
# get temporary filenames
XML_SETTINGS_FILE=settings.xml
cd $WORKING_DIRECTORY
echoXmlComment "DEBUG: entering working directory : $WORKING_DIRECTORY"
if ! cp $INPUTFILE $XML_SETTINGS_FILE &> /dev/null
then
    echoXmlErrorMessage "can't copy given file $INPUTFILE"
    echoXmlComment "DEBUG: EXITING"
    exit 1
fi

# Start real job:
# check xml descriptor file
checkXmlDescriptorFile $XML_SETTINGS_FILE || exit $?
# generating yoga descriptor file
generateYogaDescFile $WORKING_DIRECTORY $XML_SETTINGS_FILE settings.i || exit $?
# decode b64 files
decodeB64Files $(ls *.b64) || exit $?

# generating batch into current directory
BATCH_FILE=batch.i
echo "/* Generated by $SCRIPTBASE/$SCRIPTNAME */" > $BATCH_FILE
echo "_GUI_SPECIALS_=1;" >> $BATCH_FILE
echo "#include \"$YOGA_LOCATION/LITpro/yorick/LITpro.i\"" >> $BATCH_FILE
echo "window, display=\"\",hcp=\"img.ps\";" >> $BATCH_FILE
echo "world = lp_load_model(\"settings.i\");" >> $BATCH_FILE
# Try to define x range using hack plotting radial model (but not for T3 data)
if  echo "$ARGS" | grep -v T3 &> /dev/null
then
    echo "lp_plot_radial_model, world, degree=1, $ARGS;" >> $BATCH_FILE
fi
cat >> $BATCH_FILE << EOF
lp_plot_radial, world, degree=1, $ARGS;
prevlimits=limits();
winkill();
window, display="",hcp="img.ps";
limits(prevlimits(1),prevlimits(2));
lp_plot_radial, world, $ARGS, degree=1, residuals=1;
EPSGS_CMD += " -dAutoRotatePages=/None"; 
pdf, "img";
png, "img";
quit;
EOF

# Run batch
if ! RES="$(runYorickBatch "$BATCH_FILE")"
then
    echoXmlComment "$RES"
    extractError "$RES"
    exit 1
fi
echoXmlComment "$RES"
extractError "$RES"
convert img.pdf $LITPROWRAPPER_RESIZE_OPTION img.png
encode64.py img.png img.png.b64
encode64.py img.pdf img.pdf.b64
echo "<resultFile name=\"img.png\" href=\"data:image/png;base64,$(cat img.png.b64)\"/>"
echo "<resultFile name=\"img.pdf\" href=\"data:application/pdf;base64,$(cat img.pdf.b64)\"/>"

# HERE ENDS THE SCRIPT
echoXmlComment "DEBUG: EXITING"
echo "$(date) : $SCRIPTNAME stops" >> $SCRIPTBASE/log/yoga.log
exit 0
