#!/bin/sh
#PBS -S /bin/sh
#PBS -l @@{ATTRIBUTES}
#PBS -l walltime=@@{WALLTIME}
#PBS -N JOB_@@{JOBID}_@@{INSTANCEID}
#PBS -o pbs_@@{RUNNAME}_@@{INSTANCEID}.stdout
#PBS -e pbs_@@{RUNNAME}_@@{INSTANCEID}.stderr
#PBS -mn
@@{REMOTEBATCHACCOUNT}
@@{QUEUE}

@@{ENVIRONMENT}

# RemoteBatchPBS:makeParallelTemplate
# SubmissionScripts/Distributor/Batch/PBS/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=${PBS_O_WORKDIR}:${PBS_O_PATH}
# Change to directory where job was submitted.
cd ${PBS_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
