#!/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-2007
#
#
# ========================================================================
#
# This script is launching ganymed for datasets.
# 
# As ganymed takes some time, only one dataset is processed at once. 
# First the script gets a dataset number from the database, for which 
# ganymed has to be done (function gettodo). After setting the status in 
# the database (set fStartTime to know, that the dataset is already being 
# processed), the background suppression is done for the dataset. 
# Afterwards the status in the database is updated according to the return 
# value of ganymed. 
# The important INFOs, WARNings and ERRORs are written not only to the 
# scriptlog but also to the processlog. 
#
# The ganymed.rc files are stored in the setup directory.
#

source `dirname $0`/sourcefile
printprocesslog "INFO starting $0"
program=ganymed
column=fGanymed

set -C

scriptlog=$runlogpath/run$program-$datetime.log
date >> $scriptlog 2>&1

# get dataset # 
gettodo "1" >> $scriptlog 2>&1
dataset=${primaries[0]}
telnum=${primaries[1]}

# lock sequ
lockfile=$lockpath/lock-$table-$column-$dataset.txt
checklock >> $scriptlog 2>&1

cd $mars

echo "run $program for dataset $dataset..." >> $scriptlog 2>&1
printprocesslog "INFO starting $program for dataset $dataset"
no=`printf %08d $dataset | cut -c 0-5`
no2=`printf %08d $dataset`
outpath="$datapath/$program/$no/$no2"
makedir $outpath >> $scriptlog 2>&1

datasetfile="$datasetpath/$no/dataset$no2.txt"
# choose ganymed.rc file
if grep 'WobbleMode:' $datasetfile >/dev/null
then 
   ganymedrc=$setuppath/ganymed/ganymed_wobble.rc
else
   ganymedrc=$setuppath/ganymed/ganymed_onoff.rc
fi

setstatus "start" >> $scriptlog 2>&1

echo "./ganymed -b -q -v4 -f --ind=$datapath/star --ins=$sequpath --config=$ganymedrc --log=$outpath/$program$no2.log --html=$outpath/$program$no2.html --out=$outpath $datasetfile "  >> $scriptlog 2>&1
./ganymed -b -q -v4 -f --ind=$datapath/star --ins=$sequpath --config=$ganymedrc --log=$outpath/$program$no2.log --html=$outpath/$program$no2.html --out=$outpath $datasetfile  2>> $scriptlog> /dev/null
check1=$?

case $check1 in
   0)   echo " check1=$check1 -> everything ok " >> $scriptlog 2>&1 
        printprocesslog "INFO $program finished successfully for dataset $dataset"
        ;;
   *)   echo " check1=$check1 -> ERROR -> step has to be repeated" >> $scriptlog 2>&1
        printprocesslog "ERROR $program failed for dataset $dataset (return code $check1)"
        com=$Fganymed
        check=$check1
        ;;
esac

setstatus "stop" >> $scriptlog 2>&1

finish >> $scriptlog 2>&1

