#!/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 runs the timing correction for all run, that need the timing
# correction
#
# After checking, if the script is already running, the todolist is 
# written by using the macro getdolist.C
# Then for each run the timing correction is done 
# If this was successful, the status is inserted into the database using 
# the macro setstatus.C
#
# This script was needed for the data between 06/2004 and 02/2005 (including
# this months). 
#

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

table=RunProcessStatus
column=fTimingCorrection
date=NULL
datetime=`date +%F_%T`
year=`date +%Y`

getstatuslogpath=$logpath/getstatus/$program/$year
getstatuslog=$getstatuslogpath/getstatus-$table-$column-$datetime.log
makedir $getstatuslogpath >> $scriptlog 2>&1

if [ -e $listpath/ToDo-$table-$column.txt ]
then 
   echo "$program is already running -> exit"
   exit
fi

cd $mars

echo "getting list..."
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 -> run $program";;
   *)   echo "check0=$check0 -> ERROR -> step has to be repeated";;
esac

# retrieving runs from todo file
runs=(`cat $listpath/ToDo-$table-$column.txt`)
if [ "$runs" = "" ]
then 
   echo "nothing to do -> exit" 
   rm -v $listpath/ToDo-$table-$column.txt
   exit
fi

echo "runs: "${runs[@]}
for run in ${runs[@]}
do 
  echo "getting path for run $run ... "
  infile=`find $datapath/rawfiles-wrong-timing/ -name *${run}_[D,C,P,S]_*`
  echo "infile:"$infile
  outfile=`echo $infile | sed -e 's/-wrong-timing//g'`
  echo "outfile:"$outfile
  outpath=`dirname $outfile`
  echo "outpath:"$outpath
  makedir $outpath

  timecorrpath=$outpath"/timecorrlogs"
  echo "timecorrpath: "$timecorrpath
  makedir $timecorrpath 
  
  check1=0
  echo "run $program..."
  /home/tape/MagicReadout/correct_raw_time $infile $outfile > $timecorrpath/$program-$run.log
  check1=$?

  case $check1 in
     1)   echo "check1=$check1 -> everything ok -> setting status..."
          no=`printf %08d $run | cut -c 1-5`
          statustime="Now()"
          failed="NULL"
          starttime="NULL"
          failedtime="NULL"
          var1=$no
          var2=$run
          setstatus
  	;;
     *)   echo "check1=$check1 -> ERROR -> step has to be repeated";;
  esac
done

rm -v $listpath/ToDo-$table-$column.txt

