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

# define INPUT Directory from where input file will be processed
INPUTS_DIRECTORY=$SCRIPTBASE/xml

# Just do a simple log to get traces
echo "$(date) : $SCRIPTNAME starts" >> $SCRIPTBASE/log/yoga.log

storeInfo(){
    echo "$*" >> test.info
    echo "$*"
}

usage(){
 echo "Usage: $(basename $0) [-h] [-d] [-k] -o <outputDir>"
 echo "  -d : change diff style"
 echo "  -k : do not remove temporary directory after loop"
 echo "  -h : show this message"
 echo "  -o <outputDir> : set output directory"
}

# parse cmd line
while getopts "dhko:" option
do
    case $option in
        h )
        usage && exit ;;
        k ) 
        DEBUGMODE="-y" ;;
        d ) 
        DIFFOPTION="-y" ;;
        o ) 
        DEBUGMODE="-y" ;
        WORKING_DIRECTORY=$OPTARG;;
        * ) # Unknown option
        usage && exit ;;
    esac
done

# creating one working area
if [ -z "$WORKING_DIRECTORY" ]
then
        echo "Please specify -o option"
        usage
        exit 1
else 
    if [ -d $WORKING_DIRECTORY ]
    then
        echo "$WORKING_DIRECTORY already exists"
        exit 1
    fi
    mkdir -p $WORKING_DIRECTORY
fi

echo "DEBUG: INPUTS DIRECTORY is $INPUTS_DIRECTORY"
echo "DEBUG: WORKING DIRECTORY is $WORKING_DIRECTORY"
cd $WORKING_DIRECTORY

INPUT_SETTINGS=$(ls $INPUTS_DIRECTORY/*with*data*.xml)

#
# Loop over result files and produce
# html report
for f in $INPUT_SETTINGS
do
    SETTINGSFILE=$f
    SETTINGSNAME=$(basename $f .xml)
    HTMLSETTINGSFILE="$SETTINGSNAME.html"
    RESULTFILE="FitResult_$SETTINGSNAME.xml"
    SHORTRESULTFILE="ShortFitResult_$SETTINGSNAME.xml"
    HTMLRESULTFILE="FitResult_$SETTINGSNAME.html"
    storeInfo "Use '$SETTINGSNAME' as setting to run"
    mkdir $SETTINGSNAME &> /dev/null
    cd $SETTINGSNAME
    cp $SETTINGSFILE .
    
    storeInfo "   Run fit"
    # leave data in result file
    export includeFileInResult="true"
    # add xml result into result file instead of compressed one
    export useYogaB64File="no"
    $SCRIPTBASE/bin/runFit.sh  $SETTINGSFILE &> $RESULTFILE 
    if  xml val $RESULTFILE 
    then
        storeInfo "   Generate yoga descriptor file"
        generateYogaDescFile $PWD $RESULTFILE settings.i &> /dev/null
        generateYogaDescFile $PWD $SETTINGSFILE settings.i &> /dev/null

        storeInfo "   Decode base64 files if any" $(ls *.b64)
        decodeB64Files $(ls *.b64) &> /dev/null
        rm -f *.b64 

        storeInfo "   Generate html report files"
        xsltproc --stringparam outputFilename $HTMLSETTINGSFILE $SCRIPTBASE/xslt/settingsToHtml.xsl $SETTINGSFILE
        xsltproc --path .:$SCRIPTBASE/xslt:$PWD --stringparam outputFilename $HTMLRESULTFILE $SCRIPTBASE/xslt/resultToHtml.xsl $RESULTFILE
    else
        storeInfo "   Fit encountered errors ($RESULTFILE.debug generated)"
        $SCRIPTBASE/bin/runFit.sh  $SETTINGSFILE &> $RESULTFILE.debug
    fi
    
    storeInfo ""
    cd - &> /dev/null
done

echo 


storeInfo "Generate main report"
#
# Loop over inpu files and produce
# main html report index file
XMLDOC="<report>"
XMLDOC=$XMLDOC"<variable name=\"reportDate\">"
XMLDOC=$XMLDOC"$(date)"
XMLDOC=$XMLDOC"</variable>"
XMLDOC=$XMLDOC"<variable name=\"machineInfo\">"
XMLDOC=$XMLDOC"$(uname -a)"
XMLDOC=$XMLDOC"</variable>"
XMLDOC=$XMLDOC"<variable name=\"yorickVersion\">"
CMD="quit"
RETURNLINE=( $(echo -e $CMD | yorick |tail -1) )
XMLDOC=$XMLDOC"${RETURNLINE[4]}"
XMLDOC=$XMLDOC"</variable>"
XMLDOC=$XMLDOC"<variable name=\"yetiVersion\">"
CMD="include, \"yeti.i\";"
RETURNLINE=( $(echo -e $CMD | yorick |tail -2) )
XMLDOC=$XMLDOC"${RETURNLINE[1]}"
XMLDOC=$XMLDOC"</variable>"
XMLDOC=$XMLDOC"<variable name=\"whichYorick\">"
XMLDOC=$XMLDOC$(which yorick)
XMLDOC=$XMLDOC"</variable>"
XMLDOC=$XMLDOC"<variable name=\"_LPB_DBL_MIN\">"
CMD="#include \"$YOGA_LOCATION/LITpro/yorick/LITpro.i\"\nprint,_LPB_DBL_MIN"
XMLDOC=$XMLDOC$(echo -e $CMD | yorick |tail -2)
XMLDOC=$XMLDOC"</variable>"
XMLDOC=$XMLDOC"<variable name=\"_LPB_DBL_MIN/2\">"
CMD="#include \"$YOGA_LOCATION/LITpro/yorick/LITpro.i\"\nprint,_LPB_DBL_MIN/2"
XMLDOC=$XMLDOC$(echo -e $CMD | yorick |tail -2)
XMLDOC=$XMLDOC"</variable>"
XMLDOC=$XMLDOC"<variable name=\"cvs stat\">"
XMLDOC=$XMLDOC"$(cd $YOGA_LOCATION/LITpro/yorick; cvs stat|grep -v Sticky|grep -v Identifier)"
XMLDOC=$XMLDOC"</variable>"

XMLDOC=$XMLDOC"<variable name=\"whatElse\">"
XMLDOC=$XMLDOC"what ever we want"
XMLDOC=$XMLDOC"</variable>"

for f in $INPUT_SETTINGS
do
    SETTINGSFILE=$f
    SETTINGSNAME=$(basename $f .xml)
    HTMLSETTINGSFILE="$SETTINGSNAME.html"
    RESULTFILE="FitResult_$SETTINGSNAME.xml"
    HTMLRESULTFILE="FitResult_$SETTINGSNAME.html"
    
    XMLDOC=$XMLDOC"<file n=\"$(basename $SETTINGSFILE)\" fn=\"$PWD/$SETTINGSNAME/$(basename $SETTINGSFILE)\" settingsName=\"$SETTINGSNAME\" htmlSettingsFile=\"$HTMLSETTINGSFILE\" resultFile=\"$RESULTFILE\" htmlResultFile=\"$HTMLRESULTFILE\" fullResultFile=\"$PWD/$SETTINGSNAME/$RESULTFILE\"/>"
done

XMLDOC=$XMLDOC"$($SCRIPTBASE/bin/getModelList.sh)"
XMLDOC=$XMLDOC"</report>"


echo "$XMLDOC" > report.xml
xsltproc --path .:$SCRIPTBASE/xslt:$PWD  $SCRIPTBASE/xslt/resultToHtml.xsl report.xml

# HERE ENDS THE SCRIPT
echo "$(date) : $SCRIPTNAME stops" >> $SCRIPTBASE/log/yoga.log
echo "DEBUG: WORKING DIRECTORY is $WORKING_DIRECTORY"
exit 0
