#!/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/2006 <mailto:dorner@astro.uni-wuerzburg.de>
#
#   Copyright: MAGIC Software Development, 2000-2006
#
#
# ========================================================================
#
# This a script, which launches other scripts
#

source `dirname $0`/sourcefile
printprocesslog "INFO starting $0 $@"

makedir $listpath
makedir $lockpath

errorlog=$runlogpath/scriptlauncher-error`date +%F`.log
scriptlog=$runlogpath/scriptlauncher`date +%F`.log

echo `date`": starting $0 $@"  >> $scriptlog 2>&1

path=`dirname $0`
date=`date +%Y-%m-%d`

for i in $@
do 
   if [ "$i" = "allatthesametime" ]
   then 
      continue
   fi
   echo " launching $i..." >> $scriptlog 2>&1
   if ! pid=`/usr/local/bin/condor_submit -a path=$path -a prog=$i -a date=$date -a dir=$runlogpath $path/run.condor | grep cluster | cut -dr -f2`  2>$errorlog
   then 
      echo "condor is not working " >> $errorlog 2>&1
      printprocesslog "ERROR submitting $i to condor failed"
   fi
   if ! echo $@ | grep "allatthesametime" >/dev/null 2>$errorlog
   then
      echo "  waiting for $i to be done..." >> $scriptlog 2>&1
      /usr/local/bin/condor_wait $runlogpath/condor-$date.log $pid >/dev/null 2>$errorlog
   fi
done
echo `date`": all done/submitted." >> $scriptlog 2>&1
echo "" >> $scriptlog 2>&1

printprocesslog "INFO finished $0 $@"

