#!/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  12/2005 <mailto:dorner@astro.uni-wuerzburg.de>
#
#   Copyright: MAGIC Software Development, 2000-2004
#
#
# ========================================================================
#
#

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

set -C

scriptlogpath=$logpath/run/mcsequences/`date +%Y/%m/%d`
makedir $scriptlogpath
scriptlog=$scriptlogpath/mcsequences`date +%F`.log

date >> $scriptlog 2>&1

lockfile=$lockpath/lock-mcsequences.txt
date > $lockfile >> $scriptlog 2>&1 
checklock0=$?
case $checklock0 in 
    0)   echo "checklock0=$checklock0 -> continue " >> $scriptlog 2>&1;;
    1)   echo "checklock0=$checklock0 -> file exists " >> $scriptlog 2>&1
         echo "-> linkmc is running -> exit" >> $scriptlog 2>&1
         date  >> $scriptlog 2>&1
         exit;;
    *)   echo "checklock0=$checklock0 -> something went completely wrong" >> $scriptlog 2>&1;;
esac


mcpath=/montecarlo/rawfiles
mcsequpath=/montecarlo/sequences

dirs=`find $mcpath -type d`

for dir in ${dirs[@]}
do 
   cont=`echo $dir | cut -d/ -f6`
   if [ "$cont" == "" ]
   then
      continue
   fi
   echo $dir >> $scriptlog 2>&1
   files=`ls $dir`
   calfile=`find $dir -name *_C_*`
   pedfile=`find $dir -name *_P_*`
   calrun=`echo $calfile | cut -d_ -f2`
   pedrun=`echo $pedfile | cut -d_ -f2`
   datruns=`find $dir -name *_D_* | cut -d_ -f2 | tr "\n" " "`
# workaround due to 5digit runnumber for data with runnumber < 35487
#   firstrun=`echo $datruns | cut -c 0-8`
   firstrun=`echo $datruns | cut -c 0-5 | sed -e 's/^0//g' -e 's/^0//g' -e 's/^0//g' -e 's/^0//g'`
   date=`echo $dir | cut -c 22-31 | sed -e 's/\//-/g'`
   
# workaround due to 5digit runnumber for data with runnumber < 35487
#   no=`echo $firstrun | cut -c 0-4`
   no=`printf %08d $firstrun | cut -c 0-4`
   no2=`printf %08d $firstrun`
   sequpath=$mcsequpath/$no
   makedir $sequpath >> $scriptlog 2>&1
#   sequfile=$sequpath/sequence$firstrun.txt
   sequfile=$sequpath/sequence$no2.txt
   echo "writing sequfile "$sequfile >> $scriptlog 2>&1
   
   echo "Sequence:        $firstrun" > $sequfile
   echo "Night:           $date" >> $sequfile
   echo "" >> $sequfile
   echo "CalRuns:         $calrun" >> $sequfile
   echo "PedRuns:         $pedrun" >> $sequfile
   echo "DatRuns:         $datruns" >> $sequfile
   echo "" >> $sequfile
done


rm -v $lockfile >> $scriptlog 2>&1

set +C

date  >> $scriptlog 2>&1

