#!/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  05/2005 <mailto:dorner@astro.uni-wuerzburg.de>
#
#   Copyright: MAGIC Software Development, 2000-2006
#
#
# ========================================================================
#
# This a resource file for the scripts, in which the standard paths and 
# functions, which are needed more often are stored. 
#

export ROOTSYS=/opt/root_v4.04.02g
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ROOTSYS/lib
export PATH=$PATH:$ROOTSYS/bin

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

logpath=/magic/datacenter/autologs
lockpath=/magic/datacenter/locks
listpath=/magic/datacenter/lists
setuppath=/magic/datacenter/setup

datapath=/magic/data
subsystempath=/magic/subsystemdata
sequpath=/magic/sequences
datasetpath=/magic/datasets

check="ok"


#failed codes
#sequence build status
Fbuildsequ=1
Fdoexcl=2
#run process status
Ftimecorr=3
Ffillraw=4
Fsinope=5
Ffillsinope=6
Fresetexcl=7
#sequence process status
Fwritesequfile=8
Ffilesavail=9
Fnoccfile=10
Fnocacofile=11
Fmerppcc=12
Fmerppcaco=13
Fcallisto=14
Ffillcalib=15
Ffillsignal=16
Fstar=17
Ffillstar=18
#dataset process status
Fstardone=19
Fganymed=20
Ffillganymed=21



function finish()
{
   rm -v $todofile
   rm -v $lockfile
   date 
   exit
}

function makedir()
{
   if [ ! -d $@ ]
   then
     mkdir -pv $@ 
     if [ ! -d $@ ]
     then 
        echo "could not make dir "$@
        finish
     fi
   fi
}

function checklock()
{ 
   date > $lockfile
   checklock0=$?
   case $checklock0 in 
       0)   echo "checklock0=$checklock0 -> continue " ;;
       1)   echo "checklock0=$checklock0 -> file $lockfile exists "
            echo "-> $@ $program is running -> exit"
            date 
            exit;;
       *)   echo "checklock0=$checklock0 -> something went completely wrong" ;;
   esac
}

function getdolist()
{
   datetime=`date +%F-%H-%M-%S`
   year=`date +%Y`
   date=NULL
   
   getstatuslogpath=$logpath/getstatus/$program/$year
   getstatuslog=$getstatuslogpath/getstatus-$table-$column-$datetime.log
   makedir $getstatuslogpath

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

   case $check0 in
      1)   echo "check0=$check0 -> everything ok, got todo list -> run $program";;
      *)   echo "check0=$check0 -> ERROR -> could not get todo list -> exit"
           finish ;;
   esac

}

function resetstatusvalues()
{
   statustime=NULL
   starttime=NULL
   returncode=NULL
   failedcode=NULL
   failedcodeadd=NULL
   failedtime=NULL
}

function printstatusvalues()
{
   echo "the current values are:"
   echo " statustime=$statustime"
   echo " starttime=$starttime"
   echo " returncode=$returncode"
   echo " failedcode=$failedcode"
   echo " failedcodeadd=$failedcodeadd"
   echo " failedtime=$failedtime"
   echo "-- check: -$check-"
   echo ""
}

function setstatus()
{
   # set status values
   resetstatusvalues
   case $@ in 
      start)   echo "start"
               starttime="Now()"
               ;;
       stop)   echo "stop"
               case $check in
                 ok)  echo " ok"
                      statustime="Now()"
                      ;;
                  *)  echo " failed"
                      starttime=noreset
                      returncode=$check
                      failedcode=$com
                      if ! [ "$comadd" = "" ]
                      then 
                         failedcodeadd=$comadd
                      fi
                      failedtime="Now()"
                      ;;
               esac
               ;;
          *)   echo "error -> exit"
               exit
               ;;
   esac

#   printstatusvalues
   # set status 
   setstatuslogpath=$logpath/setstatus/$program/$var1
   makedir $setstatuslogpath
   setstatuslog=$setstatuslogpath/setstatus-$@-$program-$var2.log
   checkstatus=`root -q -b $macrospath/setstatus.C+\("\"$sequence\""\,"\"$table\""\,"\"$column\""\,"\"$statustime\""\,"\"$returncode\""\,"\"$failedcode\""\,"\"$failedcodeadd\""\,"\"$starttime\""\,"\"$failedtime\""\) | tee $setstatuslog | grep int | sed -e 's/(int)//'`
   case $checkstatus in
      1)   echo "checkstatus=$checkstatus -> everything ok, status has been set";;
      *)   echo "checkstatus=$checkstatus -> ERROR -> step could not be set -> exit"
           finish ;;
   esac
}



