#!/bin/sh
# RemoteBatchAppScript:makeMPIPackingTemplate
# SubmissionScripts/Distributor/Batch/APP/mpipacking
#
SCRIPT=`basename $0`
WORKINGDIR=`dirname $0`

cd ${WORKINGDIR}

@@{USEENVIRONMENT}
@@{ENVIRONMENT}

tar xfz @@{JOBID}_@@{INSTANCEID}_input.tar.gz
touch @@{JOBID}_@@{INSTANCEID}_output.tar.gz
rm -f @@{JOBID}_@@{INSTANCEID}_output.tar
touch .__fileTimeMarker
sleep 1

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

rankExtension=`printf "%06d" ${@@{MPIRANKVARIABLE}}`
${TIMEPATH} --format "Command exited with status %x\nreal %e\nuser %U\nsys %S" -o @@{TIME_RESULTS}-${rankExtension} \
   @@{EXECUTABLE} @@{ARGUMENTS} "$@" < @@{STDIN}

# Allow for file/directory names with embedded spaces.
find . -newer .__fileTimeMarker -not -name . -not -iname '*hub-proxy.*' -print0 | \
   xargs --null --no-run-if-empty tar rf @@{JOBID}_@@{INSTANCEID}_output.tar.gz --no-recursion
if [ -f @@{JOBID}_@@{INSTANCEID}_output.tar ] ; then
   gzip --force @@{JOBID}_@@{INSTANCEID}_output.tar
fi
