#!/bin/sh
#
# This is a example script invoked by WIND at the completion of a run.
# It must be named 'wind_post' in the directory from where the job is run.
#
# 02/08/07 - change $1.job to $1.job.pl now that WINDUS uses Pearl
#            scripts instead of shell scripts
#
# The input parameters are:
#
#   $1 = prefix of '.dat' and '.job.pl' file. 
#   $2 = prefix of '.lis' file.
#   $3 = prefix of '.cgd' file.
#   $4 = prefix of '.cfl' file.
#   $5 = current iteration of the job.
#   $6 = run command used to resubmit the job
#

# This example resubmits the job with a different input file for each job
# iteration.  Each time through, it looks for a new input file of the form:
#
#    base.dat.iter
#
# where 'base' is the original prefix of the '.dat' file and 'iter' is the
# NEXT job iteration number.  The new file, if it exists, is COPIED OVER THE
# ORIGINAL INPUT FILE and the original job file is resubmitted.  REMEMBER,
# ALWAYS USE A COPY OF YOUR INITIAL INPUT FILE TO START THE FIRST JOB.
#
# A typical method for using this would be (assuming 'base' is 'test') would
# be to create files 'test.dat.1', 'test.dat.2' up through 'test.dat.n'.  Then
# copy 'test.dat.1' to 'test.dat' and submit the job.  By keeping the initial
# input file in 'test.dat.1' and copying it to 'test.dat', the original initial
# input file is preserved.  Remember that 'test.dat' gets overwritten with
# 'test.dat.n'.

echo "Current iteration is $5"
next=`expr $5 + 1`
echo "Next    iteration is $next"
cp $4.cfl $4.cfl.$5

if [ -s $1.dat.${next} ] ; then
  cp $1.dat.${next} $1.dat
  case $6 in
    qsub* | bsub* | llsubmit* )
      $6 $1.job.pl
    ;;
    at* )
      if [ -z "`echo $6 | grep -i now`" ] ; then
#        $6 now + 1 minutes < $1.job.pl
        echo $1.job.pl | $6 now + 1 minutes
      else
#        $6 < $1.job.pl
        echo $1.job.pl | $6
      fi
    ;;
    batch* )
#      $6 < $1.job
      echo $1.job | $6
    ;;
    * )
      echo "Unknown queue $6"
    ;;
  esac
else
  echo "No more job files found"
  /bin/rm -f $1.job
fi
#
exit
