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

user=`whoami`
source /home/$user/Mars/datacenter/scripts/sourcefile

set -C

cd $mars

table=RunProcessStatus
column=fDataCheckDone
date=NULL
datetime=`date +%F-%H-%M-%S`
year=`date +%Y`

todofile=$listpath/ToDo-$table-$column
getstatuslogpath=$logpath/getstatus/dodatacheck/$year
getstatuslog=$getstatuslogpath/getstatus-dodatacheck-$datetime.log

scriptlogpath=$logpath/run/dodatacheck/`date +%Y/%m`
makedir $scriptlogpath

scriptlog=$scriptlogpath/dodatacheck-$datetime.log

date >> $scriptlog 2>&1

makedir $getstatuslogpath >> $scriptlog 2>&1

echo "checking if other todo-files are there" >> $scriptlog 2>&1
if  ls $todofile-[1-9]*.txt >> $scriptlog 2>&1
then 
   echo "other file(s) on disk " >> $scriptlog 2>&1
   echo " -> choose one file and start calibrating" >> $scriptlog 2>&1
else
   date > $lockpath/lock-getting-dodatacheck-list.txt >> $scriptlog 2>&1 
   checklock0=$?
   case $checklock0 in 
       0)   echo "checklock0=$checklock0 -> continue " >> $scriptlog 2>&1;;
       1)   echo "checklock0=$checklock0 -> file exists " >> $scriptlog 2>&1
            echo "-> getting list for dodatacheck is running -> exit" >> $scriptlog 2>&1
            date  >> $scriptlog 2>&1
            exit;;
       *)   echo "checklock0=$checklock0 -> something went completely wrong" >> $scriptlog 2>&1;;
   esac

   if  ls $todofile-[1-9]*.txt >> $scriptlog 2>&1
   then
      echo "other file(s) on disk " >> $scriptlog 2>&1
      echo " -> choose one file and start datacheck" >> $scriptlog 2>&1
   else
      echo "getting list..." >> $scriptlog 2>&1
      check0=`root -q -b $macrospath/getdolist.C+\("\"$table\""\,"\"$column\""\,"\"$date\""\,"\"$listpath\""\) | tee $getstatuslog | grep int | sed -e 's/(int)//'`

      case $check0 in
          1)   echo "check0=$check0 -> everything ok -> do datacheck" >> $scriptlog 2>&1;;
          *)   echo "check0=$check0 -> ERROR -> step has to be repeated" >> $scriptlog 2>&1;;
      esac
   fi
   rm -v $lockpath/lock-getting-dodatacheck-list.txt >> $scriptlog 2>&1
fi


nr=bla
echo "finding the right todo-file" >> $scriptlog 2>&1
todofiles=`ls -r $listpath/ToDo-$table-$column-*`

echo "todofiles: "${todofiles[@]} >> $scriptlog 2>&1

for todofile in ${todofiles[@]}
do 
   if ! ls $todofile >> $scriptlog 2>&1
   then 
      echo "file is not on disk -> continue" >> $scriptlog 2>&1
      continue
   fi
   lockfile=`echo $todofile | sed -e 's/lists/locks/' -e 's/ToDo/lock/'`
   date > $lockfile >> $scriptlog 2>&1
   checklock=$?
   case $checklock in 
       0)   echo "checklock=$checklock -> setting number" >> $scriptlog 2>&1
            nr=${i}
            break;;
       1)   echo "checklock=$checklock -> file exists -> continue" >> $scriptlog 2>&1;;
       *)   echo "checklock=$checklock -> something went completely wrong" >> $scriptlog 2>&1;;
   esac
done

case $nr in
   bla)      echo "everything is beeing processed -> exit" >> $scriptlog 2>&1
             date  >> $scriptlog 2>&1
             exit;;
   12345678) echo "process nr: "$nr >> $scriptlog 2>&1;;
esac


runs=(`cat $todofile`)

if [ "$runs" = "" ]
then 
   echo "nothing to do -> exit"  >> $scriptlog 2>&1
   rm -v $todofile >> $scriptlog 2>&1
   rm -v $lockfile >> $scriptlog 2>&1
   date  >> $scriptlog 2>&1
   exit
fi

echo "runs: "${runs[@]} >> $scriptlog 2>&1

for run in ${runs[@]}
do 
   date
   echo "do datacheck for run "$run >> $scriptlog 2>&1
   no=`printf %08d $run | cut -c 0-2`
   no2=`printf %08d $run`
   rawfile=`find /magic/data/rawfiles/ -name *${run}_[D,P,C,S]_*_E.raw`
   echo "rawfile: "$rawfile >> $scriptlog 2>&1
   date=`find /magic/data/rawfiles/ -name *${run}_[D,P,C,S]_*_E.raw | cut -c 22-31`
   echo "date: "$date >> $scriptlog 2>&1
   date2=`echo $date | sed -e 's/\//-/g'`
   echo "date2: "$date2 >> $scriptlog 2>&1

   filldotrawpath=$logpath/filldotraw/$date
   makedir $filldotrawpath >> $scriptlog 2>&1
   filldotrawlogfile=$filldotrawpath/filldotraw-$no2.log

   echo "doing filldotraw..." >> $scriptlog 2>&1
   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 datacheck..." >> $scriptlog 2>&1
            ;;
       *)   echo "check1=$check1 -> ERROR -> step has to be repeated" >> $scriptlog 2>&1
            continue;;
   esac
   
   sinopepath=$datapath/sinope/$date
   makedir $sinopepath >> $scriptlog 2>&1
   
   sins=( "-dat"  "-cal")
   echo ${sins[@]} >> $scriptlog 2>&1
   for sin in ${sins[@]}
   do 
      sinopefile=sinope$sin$no2
      echo $sinopefile >> $scriptlog 2>&1
      
      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 datacheck..." >> $scriptlog 2>&1
               ;;
          *)   echo "check2=$check2 -> ERROR -> step has to be repeated" >> $scriptlog 2>&1
               continue 2;;
      esac
      check2=1
   done
   
   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 exclusions..." >> $scriptlog 2>&1
            ;;
       *)   echo "check3=$check3 -> ERROR -> step has to be repeated" >> $scriptlog 2>&1
            continue;;
   esac

   echo "resetting the status for fExclusions done for date $date2" >> $scriptlog 2>&1
   resetlogpath=$logpath/resetexclusions/$date
   echo "resetlogpath: $resetlogpath" >> $scriptlog 2>&1
   makedir $resetlogpath >> $scriptlog 2>&1
   resetlog=$resetlogpath/reset-exclusions-$no2-$date2.log
   echo "setstatuslogfile: $setstatuslog" >> $scriptlog 2>&1

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

   echo "inserting the status for the datacheck for run $run into the db" >> $scriptlog 2>&1
   setstatuslogpath=$logpath/setstatus/datacheck/$date
   echo "setstatuslogpath: $setstatuslogpath" >> $scriptlog 2>&1
   makedir $setstatuslogpath >> $scriptlog 2>&1
   setstatuslog=$setstatuslogpath/setstatus-datacheck-$no2.log
   echo "setstatuslogfile: $setstatuslog" >> $scriptlog 2>&1

   check4=`root -q -b $macrospath/setstatus.C+\("\"$run\""\,"\"$table\""\,"\"$column\""\,"\"Now()\""\) | tee $setstatuslog | grep int | sed -e 's/(int)//'`
   case $check4 in
      1)   echo "check4=$check4 -> everything ok, status has been set" >> $scriptlog 2>&1
           ;;
      *)   echo "check4=$check4 -> ERROR -> step could not be set" >> $scriptlog 2>&1
           ;;
   esac
done

rm -v $todofile >> $scriptlog 2>&1 
rm -v $lockfile >> $scriptlog 2>&1 

set +C

date  >> $scriptlog 2>&1

