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

export ROOTSYS=/opt/root_v3.10.02
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ROOTSYS/lib
export PATH=$PATH:$ROOTSYS/bin

mars=/home/operator/Mars
macrospath=$mars/datacenter/macros

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

listdir=/magic/datacenter/lists
logpath=/magic/datacenter/autologs
getstatuslogpath=$logpath/getstatus/correct_raw_time/$year
getstatuslog=$getstatuslogpath/getstatus-$table-$column-$datetime.log

if [ ! -d $getstatuslogpath ]
then
  mkdir -pv $getstatuslogpath 
  if [ ! -d $getstatuslogpath ]
  then 
     echo "could not make getstatuslogpath "$getstatuslogpath 
     exit
  fi
fi


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

cd $mars

echo "getting list..."
check0=`root -q -b $macrospath/getdolist.C+\("\"$table\""\,"\"$column\""\,"\"$date\""\) | tee $getstatuslog | grep int | sed -e 's/(int)//'`

case $check0 in 
   1)   echo "check0=$check0 -> everthing ok -> run correct_raw_time";;
   *)   echo "check0=$check0 -> ERROR -> step has to be repeated";;
esac

runs=(`cat $listdir/ToDo-$table-$column.txt`)

if [ "$runs" = "" ]
then 
   echo "nothing to do -> exit" 
   rm -v $listdir/ToDo-$table-$column.txt
   exit
fi

echo "runs: "${runs[@]}

for run in ${runs[@]}
do 
  echo "getting path for run $run ... "
  infile=`find /magic/data/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
  if [ ! -d $outpath ]
  then
    mkdir -pv $outpath
    if [ ! -d $outpath ]
    then 
       echo "could not make outpath "$outpath
       continue
    fi
  fi
  timecorrpath=$outpath"/timecorrlogs"
  echo "timecorrpath: "$timecorrpath
  if [ ! -d $timecorrpath ]
  then
    mkdir -pv $timecorrpath
    if [ ! -d $timecorrpath ]
    then
       echo "could not make timecorrpath "$timecorrpath
       continue
    fi
  fi
  
  check1=0
  echo "run correct_raw_time..."
  /home/tape/MagicReadout/correct_raw_time $infile $outfile > $timecorrpath/correct_raw_time-$run.log
  check1=$?

  case $check1 in
     1)   echo "check1=$check1 -> everthing ok -> setting status..."
          no=`printf %08d $run | cut -c 1-5`
          setstatuspath=$logpath/setstatus/correct_raw_time/$no
          echo "setstatuspath: "$setstatuspath 
          if [ ! -d $setstatuspath ]
          then
            mkdir -pv $setstatuspath 
            if [ ! -d $setstatuspath ]
            then
               echo "could not make setstatuspath $setstatuspath -> continue " 
               continue
            fi
          fi
          check2=`root -q -b $macrospath/setstatus.C+\("\"$run\""\,"\"$table\""\,"\"$column\""\,"\"Now()\""\) | tee $setstatuspath/setstatus-correct_raw_time-$run.log | grep int | sed -e 's/(int)//'`
          case $check2 in
  	     0)   echo "check2=0 -> ERROR -> step could not be set";;
  	     1)   echo "check2=1 -> everthing ok, status has been set";;
          esac
  	;;
     *)   echo "check1=$check1 -> ERROR -> step has to be repeated";;
  esac
done

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

