#!/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

#do not display xml comments and closing tag
export NO_XML="y"

usage()
{
    echoPre "\nUsage:\n $(basename $0) file.oifits\n"   
}

# check cmd line
INPUTFILE=$1
if test $# -ne 1
then
    echoPre "Missing file as argument"
    usage 
    exit 1
fi

if gzip -cd $INPUTFILE > $INPUTFILE.unzipped &> /dev/null
then
    mv $INPUTFILE.unzipped $INPUTFILE
fi

trap beforeExit 0 1 2 5 15
echoXmlComment "DEBUG: ENTERING"
echoXmlComment "DEBUG: using $SCRIPTNAME script"
echoXmlComment "DEBUG: working base is $SCRIPTBASE"
echoXmlComment "DEBUG: $(date) : starting"

# Just do a simple log to get traces
echo "$(date) : $SCRIPTNAME 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

# get temporary filenames
XML_SETTINGS_FILE=diskSettings.xml
XML_RESULT_FILE=result.xml
OIFITS_FILE=Obj1.fits

# Store full path of INPUTFILE if given path was not absolute
if test ${INPUTFILE:0:1} != "/" 
then
    INPUTFILE=$PWD/$INPUTFILE
fi
echoXmlComment "DEBUG: INPUTFILE is $INPUTFILE"
cd $WORKING_DIRECTORY
echoXmlComment "DEBUG: entering working directory : $WORKING_DIRECTORY"

if ! cp $INPUTFILE $OIFITS_FILE &> /dev/null
then
    echoXmlErrorMessage "can't copy given file $INPUTFILE"
    echoXmlComment "DEBUG: EXITING"
    exit 1
fi

# Start real job

# encode in b64
encode64.py $OIFITS_FILE ${OIFITS_FILE}.tmpb64
echo "<file id=\"obj1\" name=\"Obj1.fits\" href=\"data:base64,$(cat ${OIFITS_FILE}.tmpb64)\"/>" > ${OIFITS_FILE}.b64
# transform oifits data into xml
$SCRIPTBASE/bin/OifitsViewer $OIFITS_FILE > ${OIFITS_FILE}.xml 2>/dev/null
if ! xml val  ${OIFITS_FILE}.xml &> /dev/null
then
    echo "<message type='ERROR'>No oidata found</message>" > ${OIFITS_FILE}.xml
    xsltproc --path $PWD:. --stringparam filename "$_FILES_FILENAME" $SCRIPTBASE/xslt/diskResultToXhtml.xsl ${OIFITS_FILE}.xml
    exit 1
fi
# generate one disk settings file reading first target from user data file and
# encoded oifits file
xsltproc --stringparam b64OidataFile "$PWD/${OIFITS_FILE}.b64" $SCRIPTBASE/xslt/generateDiskSettings.xsl ${OIFITS_FILE}.xml > $XML_SETTINGS_FILE


# leave data in result file
export includeFileInResult="true"
# add xml result into result file instead of compressed one
export useYogaB64File="no"
# run fit on generated settings file into current working directory
runFit.sh $XML_SETTINGS_FILE > $XML_RESULT_FILE

echo "<plots>" >> plots.xml
    getModelRadialPlot.sh $XML_RESULT_FILE VIS2 1 >> plots.xml
    getModelResidualsPlot.sh $XML_RESULT_FILE VIS2 1 >> plots.xml
    getUVCoveragePlot.sh $XML_RESULT_FILE >> plots.xml
echo "</plots>" >> plots.xml

OBS_DATE=$(xml sel -t -v "//keyword[name='DATE-OBS']/value[1]" ${OIFITS_FILE}.xml)
OBS_LAMBDA=$(xml sel -t -v "math:min(//insname//effwave)" -i "count(//insname//effwave) &gt; 1" -o "..." -v "math:max(.//insname//effwave)" -b ${OIFITS_FILE}.xml)

# return to the browser on result svg file 
xsltproc --path $PWD:.:$SCRIPTBASE/xslt --stringparam filename "$_FILES_FILENAME" --stringparam obs_date "$OBS_DATE" --stringparam obs_lambda "$OBS_LAMBDA" $SCRIPTBASE/xslt/diskResultToXhtml.xsl $XML_RESULT_FILE
#xsltproc $SCRIPTBASE/xslt/resultToSimpleHtml.xsl $XML_RESULT_FILE 2> /dev/null


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