#!/bin/ksh

############################## standard interface to /sw tools
# Input:
#   Environment variables
#     SW_BLDDIR    current directory (PWD) minus /autofs/na1_ stuff
#     SW_ENVFILE   file to be sourced which has alternate prog environment
#                     only to be used in special circumstances
#     SW_WORKDIR   work dir that local script can use
# Output:
#   Return code of 0=success or 1=failure   or 2=job submitted
#
# Notes:
#   If this script is called from swtest, then swtest requires 
#   SW_WORKDIR to be set.  Then swtest adds a unique path to what 
#   user gave swtest (action+timestamp+build) and provides this
#   script with a uniquely valued SW_WORKDIR.  swtest will
#   automatically remove this unique workspace when retest is done.
##################################################################

# exit 3 is a signal to the sw infrastructure that this template has not 
# been updated; please delete it when ready

if [ -z $SW_BLDDIR ]; then
  echo "Error: SW_BLDDIR not set!"
  exit 1
else
  cd $SW_BLDDIR
fi

if [ -z $SW_ENVFILE ]; then
  ### Set Environment (do not remove this line only change what is in between)
  . ${MODULESHOME}/init/ksh
  . ${SW_BLDDIR}/remodule
  ### End Environment (do not remove this line only change what is in between)
else
  . $SW_ENVFILE
fi

############################## app specific section
#  
set -o verbose
#clear out status file since re-testing
rm -f status 

cd $SW_WORKDIR

#-- Four things are required in the PBS script such that it can be followed
#   correctly by jenkins:
#   1. The PBS_JOBID (given upon submission with 'qsub') needs to be written
#      to the ${SW_BLDDIR}/.running file on submission
#   2. The main output of the test (i.e. typically "aprun" command output)
#      needs to be appended to ${SW_BLDDIR}/.running as the test runs
#   3. If the test succeed, the first line of $SW_BLDDIR/status file must be 
#      the string "verified"
#   4. ${SW_BLDDIR}/.running must be removed after the test completes
    

cat > ${PACKAGE}.pbs << EOF
#!/bin/bash
#PBS -N ${PACKAGE}
#PBS -j oe
#PBS -l nodes=1:ppn=32,walltime=30:00

set -o verbose
cd \$PBS_O_WORKDIR

TIME="/usr/bin/time -f 'elapsed %e'"
UPTIME=/usr/bin/uptime

module load xalt/0.7.1

#-- Wrapped aprun and Real aprun:
export W_APRUN=\$(which aprun)
export R_APRUN=/usr/bin/aprun

#-- Test with XALT's aprun wrapper

#-- directdb
export XALT_TRANSMISSION_STYLE=directdb
echo "W_APRUN: \$W_APRUN" | tee -a ${SW_BLDDIR}/.running
echo "XALT_TRANSMISSION_STYLE: \$XALT_TRANSMISSION_STYLE" | tee -a ${SW_BLDDIR}/.running
eval \$TIME \$W_APRUN -n 1 \$UPTIME 2>&1 | tee x_aprun_directdb.log | tee -a ${SW_BLDDIR}/.running

#-- file
export XALT_TRANSMISSION_STYLE=file
echo "W_APRUN: \$W_APRUN" | tee -a ${SW_BLDDIR}/.running
echo "XALT_TRANSMISSION_STYLE: \$XALT_TRANSMISSION_STYLE" | tee -a ${SW_BLDDIR}/.running
eval \$TIME \$W_APRUN -n 1 \$UPTIME 2>&1 | tee x_aprun_file.log | tee -a ${SW_BLDDIR}/.running

#-- syslog
export XALT_TRANSMISSION_STYLE=syslog
echo "W_APRUN: \$W_APRUN" | tee -a ${SW_BLDDIR}/.running
echo "XALT_TRANSMISSION_STYLE: \$XALT_TRANSMISSION_STYLE" | tee -a ${SW_BLDDIR}/.running
eval \$TIME \$W_APRUN -n 1 \$UPTIME 2>&1 | tee x_aprun_syslog.log | tee -a ${SW_BLDDIR}/.running

#-- Test with real aprun
echo "R_APRUN: \$R_APRUN" | tee -a ${SW_BLDDIR}/.running
eval \$TIME \$R_APRUN -n 1 \$UPTIME 2>&1 | tee r_aprun.log | tee -a ${SW_BLDDIR}/.running

echo "verified" > ${SW_BLDDIR}/status

grep elapsed x_aprun_directdb.log | grep -v time | awk '{print "YVALUE=",\$2}' \
  | sed 's/ //g' > ${SW_BLDDIR}/x_aprun_directdb
grep elapsed x_aprun_file.log | grep -v time | awk '{print "YVALUE=",\$2}' \
  | sed 's/ //g' > ${SW_BLDDIR}/x_aprun_file
grep elapsed x_aprun_syslog.log | grep -v time | awk '{print "YVALUE=",\$2}' \
  | sed 's/ //g' > ${SW_BLDDIR}/x_aprun_syslog
grep elapsed r_aprun.log | grep -v time | awk '{print "YVALUE=",\$2}' \
  | sed 's/ //g' > ${SW_BLDDIR}/r_aprun

JOBID=\`echo \$PBS_JOBID | cut -d "." -f1 \`
chmod 775 ${SW_BLDDIR}/status
rm ${SW_BLDDIR}/.running
cat \${JOBID}.OU >> ${SW_BLDDIR}/test.log
cat ${PACKAGE}.log >> ${SW_BLDDIR}/test.log
chmod 664 ${SW_BLDDIR}/test.log
EOF


#submit job and touch .running file - marker to infrastructure that
qsub ${PACKAGE}.pbs > ${SW_BLDDIR}/.running

# qsub returns 0 on successful job launch, so if failure return 1
if [ $? -ne 0 ]; then
  echo "Error submitting job"
  rm -f .running
  exit 1
else
  echo "Job submitted"
  cat ${SW_BLDDIR}/.running
  exit 2
fi

cd ../

cd ../../

############################### if this far, return 0
exit 0