#!/bin/sh
#$ -S /bin/sh
#$ -cwd
#$ -l nodes=@@{NNODES}:ppn=@@{PPN}:@@{ATTRIBUTES}
#$ -l h_rt=@@{WALLTIME}
#$ -N JOB_@@{JOBID}_@@{INSTANCEID}
#$ -o sge_@@{RUNNAME}_@@{INSTANCEID}.stdout
#$ -e sge_@@{RUNNAME}_@@{INSTANCEID}.stderr
#$ -m n
@@{REMOTEBATCHACCOUNT}
@@{QUEUE}

@@{ENVIRONMENT}

# RemoteBatchSGE:makeParallelTemplate
# SubmissionScripts/Distributor/Batch/SGE/parallel

trap cleanup HUP INT QUIT ABRT TERM

cleanup()
{
   echo "Abnormal termination by signal"
   if [ ! -s @@{TS_FINISH} ] ; then
      date +"%s" > @@{TS_FINISH}
   fi
   if [ ! -s @@{TIME_RESULTS} ] ; then
      cat > @@{TIME_RESULTS} << EOF
Command terminated by signal 3
real 0.00
user 0.00
sys 0.00
EOF
   fi
   exit 1
}

export PATH=${SGE_O_WORKDIR}:${PATH}
# Change to directory where job was submitted.
cd ${SGE_O_WORKDIR}

TIMEPATH=
for timePath in ${HOME}/bin/time /usr/bin/time /usr/local/bin/time ; do
   if [ -x ${timePath} ] ; then
      TIMEPATH=${timePath}
      break
   fi
done

APPLICATION=@@{EXECUTABLE}
timeMeasured=0

@@{QUOTA}

date +"%s" > @@{TS_START}

@@{PREMANAGERCOMMANDS}
${TIMEPATH} --format "Command exited with status %x\nreal %e\nuser %U\nsys %S" -o @@{TIME_RESULTS} \
   @@{MANAGERCOMMAND} ${APPLICATION} @@{ARGUMENTS} < @@{STDIN} > @@{RUNNAME}_@@{INSTANCEID}.stdout 2> @@{RUNNAME}_@@{INSTANCEID}.stderr
@@{POSTMANAGERCOMMANDS}

date +"%s" > @@{TS_FINISH}

if [ ${timeMeasured} -eq 0 ] ; then
   mv @@{TIME_RESULTS} @@{TIME_RESULTS}.status
   grep status @@{TIME_RESULTS}.status > @@{TIME_RESULTS}
   startTime=`cat @@{TS_START}`
   finishTime=`cat @@{TS_FINISH}`
   let wallTime=finishTime-startTime
   cnt=0
   while [ "${cnt}" != "@@{NPROCESSORS}" ] ; do
      cat >> @@{TIME_RESULTS} << EOF
real ${wallTime}.00
user ${wallTime}.00
sys 0.00
EOF
      cnt=`expr ${cnt} + 1`
   done
   rm -f @@{TIME_RESULTS}.status
fi
