#!/bin/sh
#
# ========================================================================
#
# *
# * This file is part of MARS, the MAGIC Analysis and Reconstruction
# * Software. It is distributed to you in the hope that it can be a useful
# * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
# * It is distributed WITHOUT ANY WARRANTY.
# *
# * Permission to use, copy, modify and distribute this software and its
# * documentation for any purpose is hereby granted without fee,
# * provided that the above copyright notice appear in all copies and
# * that both that copyright notice and this permission notice appear
# * in supporting documentation. It is provided "as is" without express
# * or implied warranty.
# *
#
#
#   Author(s): Daniela Dorner  08/2004 <mailto:dorner@astro.uni-wuerzburg.de>
#
#   Copyright: MAGIC Software Development, 2000-2007
#
#
# ========================================================================
#
# This script is launching the calibration of sequences. 
# 
# As callisto takes some time, only one sequence is processed at once. 
# First the script gets a sequence number from the database, for which 
# the calibration has to be done (function gettodo). After setting the 
# status in the database (set fStartTime to know, that the sequence is 
# already being processed), the sequence is calibrated and the merpp 
# update is done. Afterwards the status in the database is updated 
# according to the return value of the program. In case one of the merpp 
# update failes, also the corresponding runnumber is inserted into the 
# database. 
# The important INFOs, WARNings and ERRORs are written not only to the 
# scriptlog but also to the processlog. 
# 
# The callisto.rc files are stored in the setup directory
#

source `dirname $0`/sourcefile
printprocesslog "INFO starting $0"
program=callisto
column=fCallisto

set -C

scriptlog=$runlogpath/run$program-$datetime.log
date >> $scriptlog 2>&1

# get sequence # 
gettodo >> $scriptlog 2>&1
sequence=$process

# lock sequ for cal
lockfile=$lockpath/lock-$table-$column-$sequence.txt
checklock >> $scriptlog 2>&1

cd $mars

# run calibration for sequence
echo "run $program for sequence $sequence..." >> $scriptlog 2>&1
printprocesslog "INFO starting $program for sequence $sequence"
no=`printf %08d $sequence | cut -c 0-4`
no2=`printf %08d $sequence`
outpath="$datapath/$program/$no/$no2"
makedir $outpath >> $scriptlog 2>&1
sequfile="$sequpath/$no/sequence$no2.txt"

# define callisto.rc files
callistorcnew=$setuppath/$program/callisto.rc
callistorcmux=$setuppath/$program/callisto_mux.rc
# find callisto.rc file
if [ -e $outpath/callisto.rc ]
then
   echo "found $program.rc in $outpath -> using this " >> $scriptlog 2>&1
   callistorcseq=$outpath/callisto.rc
else
   echo "no $program.rc found in $outpath -> making link " >> $scriptlog 2>&1
   if [ $sequence -gt 200000 ]
   then
      ln -vs $callistorcmux $outpath/callisto.rc >> $scriptlog 2>&1
   else
      ln -vs $callistorcnew $outpath/callisto.rc >> $scriptlog 2>&1
   fi
   callistorcseq=$outpath/callisto.rc
fi

# lock sequ for zipping
lockfile=$lockpath/calzip$sequence.txt
# if lockfile is already existing, 1 is returned
if ! checklock return 1 >> $scriptlog 2>&1
then
   # reset lockfile name
   lockfile=$lockpath/lock-$table-$column-$sequence.txt
   finish >> $scriptlog 2>&1
fi

primvar=$no2
setstatus "start" >> $scriptlog 2>&1

echo "./callisto -b -q -v4 -f --log=$outpath/$program$no2.log --html=$outpath/$program$no2.html --out=$outpath --config=$callistorcseq $sequfile  2>> $scriptlog > /dev/null" >> $scriptlog 2>&1
./callisto -b -q -v4 -f --log=$outpath/$program$no2.log --html=$outpath/$program$no2.html --out=$outpath --config=$callistorcseq $sequfile  2>> $scriptlog > /dev/null
check1=$?

# remove lockfile for zip and reset lockfile name
rm -v $lockfile >> $scriptlog 2>&1
lockfile=$lockpath/lock-$table-$column-$sequence.txt

case $check1 in
   0)   echo " check1=$check1 -> everything ok -> doing update..." >> $scriptlog 2>&1
        printprocesslog "INFO $program finished successfully for sequence $sequence"
        # running merpp update if calibration worked
        # finding files, which have to be updated
        echo "finding files to be updated..." >> $scriptlog 2>&1
        calfiles=`find $outpath -name *_Y_* `
        if [ "$calfiles" = "" ]
        then 
           echo " no files found -> continue with next sequence" >> $scriptlog 2>&1
           printprocesslog "ERROR no calfiles found"
        fi
        echo " files to be updated: "$calfiles >> $scriptlog 2>&1
        
        merpplogpath=$outpath"/merpplogs"
        makedir $merpplogpath >> $scriptlog 2>&1
        
        printprocesslog "INFO doing merppupdate for sequence $sequence"
        # updated calibrated data files with the information from the cc and caco files
        for calfile in ${calfiles[@]}
        do 
           echo "calfile: "$calfile >> $scriptlog 2>&1
           # find cc and caco file
           # if file is missing continue with next sequence
           date=`date --date \`basename $calfile | cut -d_ -f1\` +%Y/%m/%d`
           ccpath=$subsystempath/cc/$date
           cacopath=$subsystempath/caco/$date
           runno=`echo $calfile | cut -d_ -f2 | sed -e 's/^0//' | sed -e 's/^0//' | sed -e 's/^0//' `
           ccfile=`find $ccpath -name 20[0-2][0-9][01][0-9][0-3][0-9]_*${runno}_[PDCS]_*_S.rep`
           source=`echo $ccfile | cut -d_ -f4`
           cacofile=`find /magic/subsystemdata/caco/$date -name dc_20[0-2][0-9]_[01][0-9]_[0-3][0-9]_*${runno}_${source}.txt`
           if [ "$ccfile" = "" ]
           then
              echo "no ccfile found for run "$runno >> $scriptlog 2>&1
              printprocesslog "ERROR  ccfile $ccfile not found for $calfile"
              com=$Fnoccfile
              comadd=$runno
              check=0
              break
           fi
           if [ "$cacofile" = "" ]
           then 
              echo "cacofile with no $runno not found" >> $scriptlog 2>&1
              echo "finding cacofile..." >> $scriptlog 2>&1
              for (( i = 0; i <= 10; i++ ))
              do 
                 newrun=`echo $runno - $i | bc`
                 cacofile=`find $cacopath -name *$newrun*`
                 if [ "$cacofile" = "" ]
                 then
                    if [ $i -eq 9 ]
                    then
                       echo "no cacofile found for runno $newrun in $cacopath" >> $scriptlog 2>&1
                       printprocesslog "ERROR cacofile $cacofile not found for $calfile"
                       com=$Fnocacofile
                       comadd=$runno
                       check=0
                       break 2
                    fi   
                    continue
                 else
                    echo "cacofile: "$cacofile >> $scriptlog 2>&1
                    break
                 fi
              done
           fi
           echo "./merpp -u --log=$merpplogpath/merppccupdate$runno.log --html=$merpplogpath/merppccupdate$runno.html --auto-time-stop --runfile=$runno $ccfile $calfile 2>> $scriptlog> /dev/null" >> $scriptlog 2>&1
           ./merpp -u --log=$merpplogpath/merppccupdate$runno.log --html=$merpplogpath/merppccupdate$runno.html --auto-time-stop --runfile=$runno $ccfile $calfile 2>> $scriptlog> /dev/null
           check2=$?
           case $check2 in
              0)   echo " check2=$check2 -> everything ok, merppccupdate worked -> continue" >> $scriptlog 2>&1
                   printprocesslog "INFO merppupdated $calfile sucessfully with $ccfile"
                   ;;
              *)   echo " check2=$check2 -> ERROR -> merppccupdate failed" >> $scriptlog 2>&1
                   printprocesslog "ERROR merppccupdate with file $ccfile failed for $calfile"
                   com=$Fmerppcc
                   comadd=$runno
                   check=$check2
                   break ;;
           esac
           echo "./merpp -u --log=$merpplogpath/merppcacoupdate$runno.log --html=$merpplogpath/merppcacoupdate$runno.html --auto-time $cacofile $calfile 2>> $scriptlog> /dev/null" >> $scriptlog 2>&1
           ./merpp -u --log=$merpplogpath/merppcacoupdate$runno.log --html=$merpplogpath/merppcacoupdate$runno.html --auto-time $cacofile $calfile 2>> $scriptlog> /dev/null
           check3=$?
           case $check3 in
              0)   echo " check3=$check3 -> everything ok, merppcacoupdate worked -> continue" >> $scriptlog 2>&1
                   printprocesslog "INFO merppupdated $calfile sucessfully with $cacofile"
                   ;;
              *)   echo " check3=$check3 -> ERROR -> merppcacoupdate failed" >> $scriptlog 2>&1
                   printprocesslog "ERROR merppcacoupdate with file $cacofile failed for $calfile"
                   com=$Fmerppcaco
                   comadd=$runno
                   check=$check3
                   break ;;
           esac
        done
	printprocesslog "INFO finished merppupdate successfully for sequence $sequence"
        ;;
   *)   echo " check1=$check1 -> ERROR -> step has to be repeated" >> $scriptlog 2>&1
        printprocesslog "ERROR $program failed for sequence $sequence (return code $check1)"
        com=$Fcallisto
        check=$check1
        ;;
esac

setstatus "stop"  >> $scriptlog 2>&1

finish >> $scriptlog 2>&1

