#!/bin/bash
#
# Executes run_shell_test in pbs.  Uses environment variables,
#
#  SHELL_TEST_DIR   = if set, directory in which to run shell_test.  Can
#                     be /local/...
#
#  TRANSP_EXEC_METHOD  = from run_shell_test
#  TRANSP_EXEC_VERBOSE = from run_shell_test
#
# ------------------------------------------------------------
# Basic Information
#
# --- the name of your job
#  (script replaces nnxmm with <N>x<M>)
#PBS -N shell_test_nnxmm
 
# --- Mail options
#     mail on execution("b"), termination ("e"), or interruption ("a")
#PBS -m aeb
 
# ------------------------------------------------------------
# Job execution details
 
# --- run the job on 'n' nodes with 'y' processors per node (ppn)
#     (script to replace value, below, with nodes=<n>:ppn=<m>)
#PBS -l NxM

# --- specify the memory required per node (in mb or gb)
#PBS -l mem=1gb

# --- specify the period of time the job will run in the format
#     hh:mm:ss (ex. 72 hours is 72:00:00)
#PBS -l walltime=1:00:00

# --- (optional) submit this job to a special queue. Typically, you 
#     do not specify a special queue, as the job scheduler will find 
#     the appropriate systems for you, given the job details noted 
#     above.
##PBS -q kruskal

# --- do not rerun this job if it fails
#PBS -r n

# ------------------------------------------------------------
# When a batch job starts execution, a number of environment
# variables are predefined, which include:
#
#      Variables defined on the execution host.
#      Variables exported from the submission host with
#                -v (selected variables) and -V (all variables).
#      Variables defined by the scheduler.
#
# The following reflect the environment where the user ran qsub:
# PBS_O_HOST    The host where you ran the qsub command.
# PBS_O_LOGNAME Your user ID where you ran qsub.
# PBS_O_HOME    Your home directory where you ran qsub.
# PBS_O_WORKDIR The working directory where you ran qsub.
#
# These reflect the environment where the job is executing:
# PBS_ENVIRONMENT is set to "PBS_BATCH" if the job is a batch job, or
#                 to "PBS_INTERACTIVE" if the job is an interactive job.
# PBS_O_QUEUE   The original queue you submitted to.
# PBS_QUEUE     The queue the job is executing from.
# PBS_JOBID     The job's PBS identifier.
# PBS_JOBNAME   The job's name.

# export all my environment variables to the job 
# want TRANSP_EXEC_METHOD, TRANSP_EXEC_VERBOSE
#PBS -V

# --- define standard output and error files for the job
#     default is <jobname>.o<jobid> and <jobname>.e<jobid>
##PBS -o test.out
##PBS -e test.err
# --- or combine both output and error streams into one file with the
#     job name and id as the default file name
#PBS -j oe

# ------------------------------------------------------------
# Log interesting information
#
echo " "
echo "-------------------"
echo "This is a $PBS_ENVIRONMENT job"
echo "This job was submitted to the queue: $PBS_QUEUE"
echo "The job's id is: $PBS_JOBID"
echo "-------------------"
echo "The master node of this job is: $PBS_O_HOST"

# --- the nodes allocated to this job are listed in the
#     file PBS_NODEFILE

# --- count the number of processors allocated to this run 
# --- $NPROCS is required by mpirun.
NPROCS=`wc -l < $PBS_NODEFILE`

NNODES=`uniq $PBS_NODEFILE | wc -l`

echo "This job is using $NPROCS CPU(s) on the following $NNODES node(s):"
echo "-----------------------"
uniq $PBS_NODEFILE | sort
echo "-----------------------"

# ------------------------------------------------------------
# Setup execution variables


# --- PBS_O_WORKDIR is the current working directory from
#     which this job was submitted using 'qsub'
pwd
echo The working directory is $PBS_O_WORKDIR 
 
# ------------------------------------------------------------
# Run the program
#
# --- 'cd' to the current working directory (by default, qsub
#     starts from the home directory of the user).
#     this is useful if you have input files in the working
#     directory.
cd $PBS_O_WORKDIR
 
# --- print out the current time, run the command,
#     then print out the ending time
#
echo -n 'Started job at : ' ; date
 
if [ -n "$SHELL_TEST_DIR" ]
then
  sdir="$SHELL_TEST_DIR"
else
  sdir="$PBS_O_WORKDIR"
fi

run_shell_test $NPROCS $sdir
 
echo -n 'Ended job at  : ' ; date
echo " " 
exit

