#!/usr/local/bin/zsh
#
# script template to run Code_Aster using MPI
#
#
# This template contains following Python strings formatting keys :
#
#     cmd_to_run         : Code_Aster command line
#     mpi_get_procid_cmd : command to retreive processor ID
#
# automatically generated for job number #%(num_job)s
#

ASRUN_PROCID=`%(mpi_get_procid_cmd)s`

if [ -z "$ASRUN_PROCID" ]; then
   echo "Processor ID is not defined !"
   exit 4
fi

ASRUN_WRKDIR=%(local_wrkdir)s/proc.$ASRUN_PROCID

if [ -e $ASRUN_WRKDIR ]; then
   rm -rf $ASRUN_WRKDIR
fi
if [ ! -d %(local_wrkdir)s ]; then
   mkdir -p %(local_wrkdir)s
fi
%(cp_cmd)s %(wrkdir)s $ASRUN_WRKDIR
if [ $? -ne 0 ]; then
    echo "non zero exit status for : %(cp_cmd)s %(wrkdir)s $ASRUN_WRKDIR"
    exit 4
fi
chmod 0700 $ASRUN_WRKDIR

cd $ASRUN_WRKDIR
%(cmd_to_run)s
iret=$?

if [ -f info_cpu ]; then
   infos=`cat info_cpu`
   echo "PROC=$ASRUN_PROCID INFO_CPU=$infos"
fi

if [ $ASRUN_PROCID -eq 0 ]; then
   echo "%(content_after_msg)s $ASRUN_WRKDIR :"
   ls -la . REPE_OUT

   rm -f %(wrkdir)s/glob.* %(wrkdir)s/bhdf.* %(wrkdir)s/pick.*
   rm -rf %(wrkdir)s/REPE_OUT
   # to save time during the following copy
   rm -rf $ASRUN_WRKDIR/REPE_IN $ASRUN_WRKDIR/Python
   cp -rf $ASRUN_WRKDIR/* %(wrkdir)s/
   kret=$?
   if [ $kret -gt $iret ]; then
      iret=$kret
   fi
fi
rm -rf $ASRUN_WRKDIR

exit $iret
