#!/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  07/2010 <mailto:daniela.dorner@unige.ch>
#
#   Copyright: MAGIC Software Development, 2000-2010
#
#
# ========================================================================
#
# This script is launching star for mc sequences. 
# 

source `dirname $0`/sourcefile
printprocesslog "INFO starting $0"
program=star
step=Star

set -C

# get sequence # 
numchanged=0
while (( 0 < 21 ))
do 
   if ! [ $numchanged -eq 1 ]
   then 
      if ! [ "$sequence" = "" ] && ! [ "$cereskey" = "" ]
      then
         printprocesslog "INFO $program for sequence $sequence cereskey $cereskey is already running => request new number "
      fi
      gettodo "1" 
      sequence=${primaries[0]}
      cereskey=${primaries[1]}
      if [ "$sequence" = "" ] || [ "$cereskey" = "" ]
      then
         printprocesslog "INFO nothing to do for $program "
      fi
   else
      printprocesslog "INFO starting $program for sequence $sequence cereskey $cereskey"
      break
   fi
   setstatus "start"
done

cd $mars

# run calibration for sequence

# define files and paths
sequfile="$mcsequpath/`printf %08d $sequence | cut -c 1-4`/sequence`printf %08d $sequence`.txt"
outpath=$mcpath/$program/`printf %03d $cereskey`/`printf %08d $sequence | cut -c 1-4`/`printf %08d $sequence | cut -c 5-8`
makedir $outpath
log=$outpath/$program`printf %08d $sequence`
inpath=$mcpath/callisto/`printf %03d $cereskey`/`printf %08d $sequence | cut -c 1-4`/`printf %08d $sequence | cut -c 5-8`

command="./star -b -q -v4 -f --ind=$inpath --out=$outpath --log=$log.log --html=$log.html $sequfile"
printprocesslog "INFO executing "$command
$command
check1=$?

case $check1 in
   0)  printprocesslog "INFO $program finished successfully for sequence $sequence cereskey $cereskey (return code $check1)"
       ;;
   *)  printprocesslog "ERROR $program failed for sequence $sequence cereskey $cereskey (return code $check1)"
       check=$check1
       ;;
esac

setstatus "stop"

finish

