#!/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 checks which files are on disk and updates the database 
# accordingly. It is not yet running automatically. 
# 
# It is checking the:
# - ccfiles
# - cacofiles 
#   this includes also the search for missing cacofiles:
#   Sometimes the DAQ aborts a run and starts itself a new one. In this 
#   cases the camera controll doesn't start a new file, as the command to 
#   start a new run was not sent by the central control. So the caco 
#   information is stored in the previous caco file, which has a 
#   different runnumber. To be able to merpp the information into the 
#   calibrated data file, the runnumber of the file containing the 
#   information has to be found.
#   In this script the search and inserting into the database is done 
#   using the macros findcacofiles.C and insertcacofile.C
# - rawfiles
# The update in the database is done using the macro resetallruns.C
# 

source /home/$USER/Mars/datacenter/scripts/sourcefile

set -C

cd $mars

program=filesondisk

lockfile=$lockpath/lock-$program.txt

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

date >> $scriptlog 2>&1

# check if script is already running
checklock  >> $scriptlog 2>&1

date=`date +%F`

subsystemdir=/magic/subsystemdata
filesondisklogpath=$logpath/$program/`date +%Y/%m`
makedir $filesondisklogpath 


echo "checking disk for ccfiles..."  >> $scriptlog 2>&1
filename=$filesondisklogpath/ccfilesondisk-$datetime.txt
column=fCCFileAvail
find $subsystemdir/cc/ -name '*_S.rep' | cut -d_ -f2  > $filename

echo "resetting runs..." >> $scriptlog 2>&1
check3=`root -q -b $macrospath/resetallruns.C+\("\"$filename\""\,"\"$column\""\) | tee $filesondisklogpath/resetall-$column-$datetime.log | grep int | sed -e 's/(int)//'`

case $check3 in 
   1)   echo "check3=$check3 -> everything ok -> reset is done" >> $scriptlog 2>&1 ;;
   *)   echo "check3=$check3 -> ERROR -> something went wrong while resetting" >> $scriptlog 2>&1 ;;
esac


echo "checking disk for cacofiles..."  >> $scriptlog 2>&1 
filename=$filesondisklogpath/cacofilesondisk-$datetime.txt
column=fCaCoFileAvail
find $subsystemdir/caco/ -name '*.txt' | cut -d_ -f8 | grep [0-9] > $filename

echo "resetting runs..." >> $scriptlog 2>&1 
check0=`root -q -b $macrospath/resetallruns.C+\("\"$filename\""\,"\"$column\""\) | tee $filesondisklogpath/resetall-$column-$datetime.log | grep int | sed -e 's/(int)//'`

case $check0 in 
   1)   echo "check0=$check0 -> everything ok -> reset is done" >> $scriptlog 2>&1 ;;
   *)   echo "check0=$check0 -> ERROR -> something went wrong while resetting" >> $scriptlog 2>&1 ;;
esac

echo "checking missing cacofiles..." >> $scriptlog 2>&1 
check1=`root -q -b $macrospath/findcacofiles.C+\("\"$date\""\,"\"$filesondisklogpath\""\) | tee $filesondisklogpath/findcacofiles-$datetime.log | grep int | sed -e 's/(int)//'`

case $check1 in 
   1)   echo "check1=$check1 -> everything ok -> missing cacofiles are found" >> $scriptlog 2>&1 ;;
   *)   echo "check1=$check1 -> ERROR -> something went wrong while resetting" >> $scriptlog 2>&1 ;;
esac

missingcacoruns=(`cat $filesondisklogpath/findcacofiles-$date.txt`)
for missingcacorun in ${missingcacoruns[@]}
do 
   runno=$missingcacorun
   echo "missing cacofile for run "$runno >> $scriptlog 2>&1 
   echo "-> finding cacofile" >> $scriptlog 2>&1 
   ccfile=`find $subsystemdir/cc/ -name [2][0][0-2][0-9][0,1][0-9][0-3][0-9]_*${runno}_[P,D,C,S]_*_S.rep` 2>/dev/null
   echo " ccfile: "$ccfile  >> $scriptlog 2>&1 
   if [ "$ccfile" = "" ]
   then
      echo " no ccfile found for run "$runno >> $scriptlog 2>&1 
      continue
   fi
   for (( i = 0; i <= 10; i++ ))
   do 
      newrun=`echo $runno - $i | bc`
      path=`dirname $ccfile`
      path=`echo $path | sed -e 's/cc/caco/'`
      cacofile=`find $path -name *$newrun*` 2>/dev/null
      if [ "$cacofile" = "" ]
      then
         continue
      else
         echo " inserting cacofile $file for run $missingcacorun..." >> $scriptlog 2>&1 
         check2=`root -q -b $macrospath/insertcacofile.C+\("\"$runno\""\,"\"$newrun\""\) | tee $filesondisklogpath/insertcacofile-$missingcacorun.log | grep int | sed -e 's/(int)//'`

         case $check2 in
            1)   echo " check2=$check2 -> everything ok -> insert is done" >> $scriptlog 2>&1 ;;
            *)   echo " check2=$check2 -> ERROR -> something went wrong while inserting run "$missingcacorun >> $scriptlog 2>&1 ;;
         esac
         break
      fi
   done
   if [ "$cacofile" = "" ]
   then 
      echo " no cacofile found for run "$runno >> $scriptlog 2>&1 
   else
      echo " cacofile: "$cacofile >> $scriptlog 2>&1 
   fi
done


echo "checking disk for rawfiles..."  >> $scriptlog 2>&1 
filename=$filesondisklogpath/rawfilesondisk-$datetime.txt
column=fRawFileAvail
find $datapath -name '*.raw' -o -name '*.gz' | cut -d_ -f2 > $filename

echo "resetting runs..." >> $scriptlog 2>&1 
check4=`root -q -b $macrospath/resetallruns.C+\("\"$filename\""\,"\"$column\""\) | tee $filesondisklogpath/resetall-$column-$datetime.log | grep int | sed -e 's/(int)//'` 

case $check4 in 
   1)   echo "check4=$check4 -> everything ok -> reset is done" >> $scriptlog 2>&1 ;;
   *)   echo "check4=$check4 -> ERROR -> something went wrong while resetting" >> $scriptlog 2>&1 ;;
esac

finish >> $scriptlog 2>&1


