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

cd $mars
date=`date +%F`

subsystemdir=/magic/subsystemdata
rawdir=/magic/data
logpath=/magic/datacenter/autologs/filesondisk/`date +%Y`

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



echo "checking disk for ccfiles..." 

filename=$logpath/ccfilesondisk-$date.txt
column=fCCFileAvail

find $subsystemdir/cc/ -name '*_S.rep' | cut -d_ -f2  > $filename


echo "resetting runs..."
check3=`root -q -b $macrospath/resetallruns.C+\("\"$filename\""\,"\"$column\""\) | tee $logpath/resetall-$column-$date.log | grep int | sed -e 's/(int)//'`

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



echo "checking disk for cacofiles..." 

filename=$logpath/cacofilesondisk-$date.txt
column=fCaCoFileAvail

find $subsystemdir/caco/ -name '*.txt' | cut -d_ -f8 | grep [0-9] > $filename


echo "resetting runs..."
check0=`root -q -b $macrospath/resetallruns.C+\("\"$filename\""\,"\"$column\""\) | tee $logpath/resetall-$column-$date.log | grep int | sed -e 's/(int)//'`

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



echo "checking missing cacofiles..."
check1=`root -q -b $macrospath/findcacofiles.C+\("\"$date\""\) | tee $logpath/findcacofiles-$date.log | grep int | sed -e 's/(int)//'`

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

missingcacoruns=(`cat $logpath/findcacofiles-$date.txt`)

for missingcacorun in ${missingcacoruns[@]}
do 
   runno=$missingcacorun
   echo "no cacofile found for run "$runno
   echo "finding cacofile"
   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`   
   echo "ccfile: "$ccfile
   if [ "$ccfile" = "" ]
   then
      echo "no ccfile found for run "$runno
      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*`
      if [ "$cacofile" = "" ]
      then
         continue
      else
         echo "inserting cacofile $file for run $missingcacorun..."
         check2=`root -q -b $macrospath/insertcacofile.C+\("\"$runno\""\,"\"$newrun\""\) | tee $logpath/insertcacofile-$missingcacorun.log | grep int | sed -e 's/(int)//'`

         case $check2 in
            1)   echo "check2=$check2 -> everthing ok -> insert is done";;
            *)   echo "check2=$check2 -> ERROR -> something went wrong while inserting run "$missingcacorun;;
         esac
         break
      fi
   done
   echo "cacofile: "$cacofile

done



echo "checking disk for rawfiles..." 

filename=$logpath/rawfilesondisk-$date.txt
column=fRawFileAvail

find $rawdir -name '*.raw' | cut -d_ -f2 > $filename

echo "resetting runs..."                                    
check4=`root -q -b $macrospath/resetallruns.C+\("\"$filename\""\,"\"$column\""\) | tee $logpath/resetall-$column-$date.log | grep int | sed -e 's/(int)//'` 

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


