#!/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/linkmc/`date +%Y/%m/%d`
makedir $scriptlogpath
scriptlog=$scriptlogpath/linkmc`date +%F`.log

date >> $scriptlog 2>&1

lockfile=$lockpath/lock-mclinks.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


mccampath=/montecarlo/camera
mcpath=/montecarlo/rawfiles
next=$mcpath/.next
processed=$mcpath/.processed
processeddir=$mcpath/.processed.dir
readme=$mcpath/README.txt

if ! ls $next >> $scriptlog 2>&1
then
   echo "file $next not found -> no start-runno -> exit" >> $scriptlog 2>&1
   rm -v $lockfile >> $scriptlog 2>&1
   exit
fi

date >| $readme 2>&1


modes=("" "Gammawobble+" "Gammanowobble0" "GammawobbleHE+" "GammanowobbleHE0" "Gammawobble0" "GammawobbleHE0" "Proton" "DiffuseGamma" )

echo "" >> $readme 2>&1
echo "Explanation for the structure in which the mc files are linked" >> $readme 2>&1
echo "--------------------------------------------------------------" >> $readme 2>&1
echo "" >> $readme 2>&1
echo "the files are linked in a YYYY/MM/DD structure like the data files" >> $readme 2>&1
echo "YYYY represents 19zbin" >> $readme 2>&1
echo "MM represents the mode" >> $readme 2>&1
echo "DD represents the psf in mm" >> $readme 2>&1
echo "" >> $readme 2>&1
echo "explanation of the modes" >> $readme 2>&1
echo "modes: "${modes[@]} >> $readme 2>&1
echo "" >> $readme 2>&1
for (( i=1 ; i <= 12 ; i++ )) 
do 
   if [ "${modes[i]}" != "" ]
   then 
      numofmode=`printf %02d $i`
      echo "mode (MM) = $numofmode means ${modes[$i]}" >> $readme 2>&1
   fi
done
echo "" >> $readme 2>&1
echo "the mode is indicating " >> $readme 2>&1
echo " - the particle type" >> $readme 2>&1
echo " - wobble/nowobble" >> $readme 2>&1
echo " - HE" >> $readme 2>&1
echo " - w+/w0 (as +/0)" >> $readme 2>&1
echo " a combination of w0 and wobble means 'fake wobble'" >> $readme 2>&1
echo "  (normal mc shifted by 0.4 deg -> abberation not taken into account)" >> $readme 2>&1
echo "" >> $readme 2>&1


runno=`cat $next`

camfiles=`find $mccampath -type f | grep -v Cal_and_Ped`

for camfile in ${camfiles[@]}
do 
   if grep $camfile $processed >> $scriptlog 2>&1
   then 
      continue
   fi
   file=`basename $camfile`
   no=`printf %08d $runno | cut -c 0-5`
#   no2=`printf %08d $runno`
# workaround due to 5digit runnumber for data with runnumber < 35487
   no2=`printf %05d $runno`
   zbin=`echo $file | cut -d_ -f2 | cut -c 5-6`
   zbin=`printf %02d $zbin`
   wobble=`echo $file | cut -d_ -f6 | cut -c 2`
   particle=`echo $file | cut -d_ -f1`
   psf=`echo $camfile | cut -d/ -f5 | cut -c 6,8`
   
   particledir=`echo $camfile | cut -d/ -f4`
   wobbledir=`echo $camfile | cut -d/ -f6`
   
   testmode=$particle$wobbledir$wobble 
#   echo "testmode:"$testmode >> $scriptlog 2>&1

   case $wobble in 
       0)   wobblemode="";;
       +)   wobblemode="W1";;
       -)   wobblemode="W2";;
       *)   echo "couldn't find any indication for mode (wobble)" >> $scriptlog 2>&1
            ;;
   esac
   
   for (( i=1 ; i <= 12 ; i++ )) 
   do 
       if [ "${modes[$i]}" == "$testmode" ]
       then 
#          echo "modes[$i]=${modes[$i]}" >> $scriptlog 2>&1
          totalmode=$i
          break
       fi
   done

   totalmode=`printf %02d $totalmode`
#   echo "totalmode: "$totalmode >> $scriptlog 2>&1

   project=${particle}${zbin}${wobblemode}
   newfile="$mcpath/19$zbin/$totalmode/$psf/19${zbin}${totalmode}${psf}_${no2}_D_${project}_E.root"
   newdir=`dirname $newfile`
   makedir $newdir >> $scriptlog 2>&1

   runno=`expr $runno + 1`
   echo $runno >| $next 

   ln -sv $camfile $newfile >> $scriptlog 2>&1
   echo $camfile >> $processed

done

echo "linking cal and ped file" >> $scriptlog 2>&1
pedfile=`find $mccampath/Cal_and_Ped -name *_P_*.root`
calfile=`find $mccampath/Cal_and_Ped -name *_C_*.root`
echo "calfile"$calfile >> $scriptlog 2>&1
echo "pedfile"$pedfile >> $scriptlog 2>&1
numfiles=`echo $pedfile $calfile | wc -w`
if [ "$numfiles" != "2" ]
then 
   "too many files in the directory $mccampath/Cal_and_Ped -> exit" >> $scriptlog 2>&1
   rm -v $lockfile >> $scriptlog 2>&1
   exit
fi

dirs=`find $mcpath -type d`

for dir in ${dirs[@]}
do 
   if grep $dir $processeddir >> $scriptlog 2>&1
   then 
      continue
   fi
   date=`echo $dir | cut -c 22-25,27,28,30,31`
# workaround due to 5digit runnumber for data with runnumber < 35487
#   newcalfile="${dir}/${date}_00000001_C_MonteCarlo_E.root"
   newcalfile="${dir}/${date}_00001_C_MonteCarlo_E.root"
   ln -sv $calfile $newcalfile >> $scriptlog 2>&1
# workaround due to 5digit runnumber for data with runnumber < 35487
#   newpedfile="${dir}/${date}_00000002_P_MonteCarlo_E.root"
   newpedfile="${dir}/${date}_00002_P_MonteCarlo_E.root"
   ln -sv $pedfile $newpedfile >> $scriptlog 2>&1
   echo $dir >> $processeddir
done


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

set +C

date  >> $scriptlog 2>&1


