#!/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-2006
#
#
# ========================================================================
#
# This script launches the datacheck for all runs, which are not yet 
# checked
#
# Get todo file
# Then for each run the following steps are done: 
#  - filldotraw.C
#  - sinope (once for data and once for calibration events)
#  - fillsinope
#  - reset ExclusionsDone, so that the result from the datacheck can be 
#    taken into account when doexclusions is running the next time
# If this was successful, the status is inserted into the database using 
# the macro setstatus.C
#

source `dirname $0`/sourcefile
program=datacheck
column=fDataCheckDone

set -C

scriptlogpath=$logpath/run/$program/`date +%Y/%m/%d`
makedir $scriptlogpath
scriptlog=$scriptlogpath/$program-$datetime.log

date >> $scriptlog 2>&1

# get todo file
possibletodofiles=`ls -r $listpath/ToDo-*-$column-*.txt`  >> $scriptlog 2>&1
if [ "$possibletodofiles" = "" ]
then 
   echo "ERROR: in $program no todofiles found => something went wrong in jobmanager"
   finish >> $scriptlog 2>&1
else
   singleprocess="yes"
   echo "todofiles: "${possibletodofiles[@]} >> $scriptlog 2>&1
   for possibletodofile in ${possibletodofiles[@]}
   do 
      if ! ls $possibletodofile >> $scriptlog 2>&1
      then 
         echo "file is not on disk -> continue" >> $scriptlog 2>&1
         continue
      fi
      lockfile=`echo $possibletodofile | sed -e 's/lists/locks/' -e 's/ToDo/lock/'`
      checklock >> $scriptlog 2>&1
      todofile=$possibletodofile
   done
fi
if [ "$todofile" = "" ]
then 
   echo "no todofile found -> exit"  >> $scriptlog 2>&1
   finish >> $scriptlog 2>&1
fi

# get run(s) from todo file
runs=(`cat $todofile`)
if [ "$runs" = "" ]
then 
   echo "nothing to do -> exit"  >> $scriptlog 2>&1
   finish >> $scriptlog 2>&1
fi

cd $mars

# processing run(s)
echo "runs: "${runs[@]} >> $scriptlog 2>&1
for run in ${runs[@]}
do 
   echo "do $program for run "$run >> $scriptlog 2>&1
   no=`printf %08d $run | cut -c 0-2`
   no2=`printf %08d $run`
   var1=$date
   var2=$no2
   rawfile=`find /magic/data/rawfiles/ -name *${run}_[D,P,C,S]_*_E.raw -o -name *${run}_[D,P,C,S]_*_E.raw.gz`
   echo "rawfile: "$rawfile >> $scriptlog 2>&1
   date=`echo $rawfile | cut -c 22-31`
   # for sinope the date is needed in the format YYYY-MM-DD
   date2=`echo $date | sed -e 's/\//-/g'`

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

   # running filldotraw
   echo "doing filldotraw..." >> $scriptlog 2>&1
   filldotrawpath=$logpath/filldotraw/$date
   makedir $filldotrawpath >> $scriptlog 2>&1
   filldotrawlogfile=$filldotrawpath/filldotraw-$no2.log

   check1=`root -q -b $macrospath/filldotraw.C+\("\"$rawfile\""\,kFALSE\) | tee $filldotrawlogfile | grep int | sed -e 's/.*(int)//'`

   case $check1 in 
      1)   echo " check1=$check1 -> everything ok -> go on with $program..." >> $scriptlog 2>&1
           ;;
      0)   echo " check1=$check1 -> no connection to db -> continue..." >> $scriptlog 2>&1 
           check="no"
           setstatus "stop" >> $scriptlog 2>&1
           continue ;;
      *)   echo " check1=$check1 -> ERROR -> step has to be repeated" >> $scriptlog 2>&1
           com=$Ffillraw
           check=$check1
           setstatus "stop" >> $scriptlog 2>&1
           continue ;;
   esac
   
   # running sinope 
   sinopepath=$datapath/sinope/$date
   makedir $sinopepath >> $scriptlog 2>&1
   
   sins=( "-dat"  "-cal")
   for sin in ${sins[@]}
   do 
      sinopefile=sinope$sin$no2
      
      echo "running sinope $sin..." >> $scriptlog 2>&1
      ./sinope -b -q -v4 -f $sin --outf=$sinopefile --out=$sinopepath --log=$sinopepath/$sinopefile.log --html=$sinopepath/$sinopefile.html --run=$run --date=$date2  2>> $scriptlog > /dev/null
      check2=$?
      
      case $check2 in 
          0)   echo " check2=$check2 -> everything ok -> go on with $program..." >> $scriptlog 2>&1
               ;;
          *)   echo " check2=$check2 -> ERROR -> step has to be repeated" >> $scriptlog 2>&1
               com=$Fsinope
#               comadd=
               check=$check2
               setstatus "stop" >> $scriptlog 2>&1
               continue 2;;
      esac
      check2=1
   done
   
   # running fillsinope
   fillsinopepath=$logpath/fillsinope/$date
   makedir $fillsinopepath >> $scriptlog 2>&1
   fillsinopelogfile=$fillsinopepath/fillsinope-$no2.log

   echo "doing fillsinope..." >> $scriptlog 2>&1
   check3=`root -q -b $macrospath/fillsinope.C+\($run\,"\"$datapath\""\,kFALSE\) | tee $fillsinopelogfile | grep int | sed -e 's/.*(int)//'`

   case $check3 in 
      1)   echo " check3=$check3 -> everything ok -> set status..." >> $scriptlog 2>&1
           ;;
      0)   echo " check3=$check3 -> no connection to db -> continue..." >> $scriptlog 2>&1 
           check="no"
           setstatus "stop" >> $scriptlog 2>&1
           continue ;;
      *)   echo " check3=$check3 -> ERROR -> step has to be repeated" >> $scriptlog 2>&1
           com=$Ffillsinope
           check=$check3
           setstatus "stop" >> $scriptlog 2>&1
           continue ;;
   esac

   # resetting the status for exclusions
   echo "resetting the status for fExclusionsDone for date $date2" >> $scriptlog 2>&1
   resetlogpath=$logpath/resetexclusions/$date
   makedir $resetlogpath >> $scriptlog 2>&1
   resetlog=$resetlogpath/reset-exclusions-$no2-$date2.log

   check5=`root -q -b $macrospath/resetcolumn.C+\("\"fExclusionsDone\""\,"\"SequenceBuildStatus\""\,"\"$date2\""\,"\"$date2\""\,kFALSE\) | tee $resetlog | grep int | sed -e 's/(int)//'`
   case $check5 in
      1)   echo " check5=$check5 -> everything ok, fExclusions have been reset " >> $scriptlog 2>&1
           ;;
      0)   echo " check5=$check5 -> no connection to db -> continue..." >> $scriptlog 2>&1 
           check="no"
           setstatus "stop" >> $scriptlog 2>&1
           continue ;;
      *)   echo " check5=$check5 -> ERROR -> step could not be resetted -> repeat step " >> $scriptlog 2>&1
           com=$Fresetexcl
           comadd=`echo $date2 | sed -e 's/-//g'`
           check=$check5
           setstatus "stop" >> $scriptlog 2>&1
           continue ;;
   esac

   # set status
   statustime="Now()"
   failed="NULL"
   starttime="NULL"
   failedtime="NULL"
   var1=$date
   var2=$no2
   setstatus "stop" >> $scriptlog 2>&1
done

finish >> $scriptlog 2>&1

