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

todofile=$listpath/ToDo-$table-$column.txt
lockfile=$lockpath/lock-$program.txt

if [ -e $todofile ]
then 
   echo "$program is already running -> exit"
   exit
fi

cd $mars

# get todo list
getdolist  >> $scriptlog 2>&1

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

echo "runs: "${runs[@]}
for run in ${runs[@]}
do 
  no=`printf %08d $run | cut -c 1-5`
  var1=$no
  var2=$run
  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..." >> $scriptlog 2>&1
  setstatus "start" >> $scriptlog 2>&1

  /home/tape/MagicReadout/correct_raw_time $infile $outfile > $timecorrpath/$program-$run.log
  check1=$?

  case $check1 in
     1)   echo "check1=$check1 -> everything ok -> setting status..." >> $scriptlog 2>&1 ;;
     *)   echo "check1=$check1 -> ERROR -> step has to be repeated" >> $scriptlog 2>&1 ;;
          com=$Ftimecorr
          check=$check1
          ;;
  esac
  echo "inserting the status for $program for run $run into the db" >> $scriptlog 2>&1
  setstatus "stop" >> $scriptlog 2>&1
done

finish >> $scriptlog 2>&1

