#!/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-2006
#
#
# ========================================================================
#
# This script is launching ganymed for datasets.
# 
# In the case of ganymed only one dataset is processed. Despite of 
# that the structure of the script is such, that also more datasets could
# be processed. The restriction to one dataset has been made, as ganymed 
# takes some time. There's one todo file per dataset. 
# First the script searches for a todo file. Then ganymed is run for the 
# dataset from this todo file. 
#
# the ganymed.rc files are stored in the setup directory
#

program=ganymed
source `dirname $0`/sourcefile

set -C

column=fGanymed
#pno=24 # number of processes, i.e. number of todo-files

lockfile=$lockpath/lock-getting-$program-list.txt

scriptlogpath=$logpath/run/$program/`date +%Y/%m/%d`
makedir $scriptlogpath
scriptlog=$scriptlogpath/run$program-$datetime.log

date >> $scriptlog 2>&1

# get todo file
possibletodofiles=`ls -r $listpath/ToDo-*-$column-*.txt`  >> $scriptlog 2>&1
if [ "$possibletodofiles" = "" ]
then 
   echo "ERROR: in $program no todofiles found => something went wrong in jobmanager"
   finish >> $scriptlog 2>&1
else
   singleprocess="yes"
   echo "todofiles: "${possibletodofiles[@]} >> $scriptlog 2>&1
   for possibletodofile in ${possibletodofiles[@]}
   do 
      if ! ls $possibletodofile >> $scriptlog 2>&1
      then 
         echo "file is not on disk -> continue" >> $scriptlog 2>&1
         continue
      fi
      lockfile=`echo $possibletodofile | sed -e 's/lists/locks/' -e 's/ToDo/lock/'`
      checklock >> $scriptlog 2>&1
      todofile=$possibletodofile
   done
fi
if [ "$todofile" = "" ]
then 
   echo "no todofile found -> exit"  >> $scriptlog 2>&1
   finish >> $scriptlog 2>&1
fi

# retrieve dataset from todo file
datasets=(`cat $todofile`)
if [ "$datasets" = "" ]
then 
   echo "nothing to do -> exit"  >> $scriptlog 2>&1
   finish >> $scriptlog 2>&1
fi

# run ganymed for dataset
echo "datasets: "${datasets[@]} >> $scriptlog 2>&1
for dataset in ${datasets[@]}
do 
  echo "run $program for dataset $dataset..." >> $scriptlog 2>&1
  no=`printf %08d $dataset | cut -c 0-5`
  no2=`printf %08d $dataset`
  var1=$no
  var2=$no2
  outpath="$datapath/$program/$no/$no2"
  makedir $outpath >> $scriptlog 2>&1

  datasetfile="$datasetpath/$no/dataset$no2.txt"
  # get observation mode to choose ganymed.rc file
  wobble=`grep 'WobbleMode:' $datasetfile` >> $scriptlog 2>&1 
  wobble2=`echo $wobble | grep ^\#` >> $scriptlog 2>&1 
  if [ "$wobble2" = "" ]
  then 
     mode="wobble" >> $scriptlog 2>&1 
  else
     mode="onoff" >> $scriptlog 2>&1 
  fi
  ganymedrc=$setuppath/ganymed/ganymed_$mode.rc
  
  setstatus "start" >> $scriptlog 2>&1

  ./ganymed -b -q -v4 -f --ind=$datapath/star --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 ;;
     *)   echo " check1=$check1 -> ERROR -> step has to be repeated" >> $scriptlog 2>&1
          com=$Fganymed
          check=$check1
          ;;
  esac
  
  setstatus "stop" >> $scriptlog 2>&1
done

finish >> $scriptlog 2>&1

