#!/bin/sh
#
# submission script for PMSCO calculations on Merlin cluster
#

if [ $# -lt 1 ]; then
  echo "Usage: $0 [NOSUB] JOBNAME NODES WALLTIME:HOURS PROJECT MODE [LOG_LEVEL]"
  echo ""
  echo "       NOSUB (optional): do not submit the script to the queue. default: submit."
  echo "       WALLTIME:HOURS (integer): sets the wall time limits."
  echo "          soft limit = HOURS:00:00"
  echo "          hard limit = HOURS:00:30"
  echo "          for short.q: HOURS = 0 (-> MINUTES=30)"
  echo "          for all.q:   HOURS <= 24"
  echo "          for long.q:  HOURS <= 96"
  echo "       PROJECT: python module (file path) that declares the project and starts the calculation."
  echo "       MODE: PMSCO calculation mode (single|swarm|gradient|grid)."
  echo "       LOG_LEVEL (optional): one of DEBUG, INFO, WARNING, ERROR if log files should be produced."
  echo ""
  echo "the job script complete with the program code and input/output data is generated in ~/jobs/\$JOBNAME"
  exit 1
fi

# location of the pmsco package is derived from the path of this script
SCRIPTDIR="$(dirname $(readlink -f $0))"
SOURCEDIR="$SCRIPTDIR/.."
PHD_SOURCE_DIR="$SOURCEDIR"

PHD_CODE="edac"

# read arguments
if [ "$1" == "NOSUB" ]; then
  NOSUB="true"
  shift
else
  NOSUB="false"
fi

PHD_JOBNAME=$1
shift

PHD_NODES=$1
shift

PHD_WALLTIME_HR=$1
PHD_WALLTIME_MIN=0
shift

PHD_PROJECT_FILE="$(readlink -f $1)"
PHD_PROJECT_ARGS=""
shift

PHD_MODE="$1"
shift

PHD_LOGLEVEL=""
if [ "$1" == "DEBUG" ] || [ "$1" == "INFO" ] || [ "$1" == "WARNING" ] || [ "$1" == "ERROR" ]; then
  PHD_LOGLEVEL="$1"
  shift
fi

# ignore remaining arguments
PHD_SCAN_FILES=""

# select allowed queues
QUEUE=short.q,all.q,long.q

# for short queue (limit 30 minutes)
if [ "$PHD_WALLTIME_HR" -lt 1 ]; then
    PHD_WALLTIME_HR=0
    PHD_WALLTIME_MIN=30
fi

# set up working directory
cd ~
if [ ! -d "jobs" ]; then
    mkdir jobs
fi
cd jobs
if [ ! -d "$PHD_JOBNAME" ]; then
    mkdir "$PHD_JOBNAME"
fi
cd "$PHD_JOBNAME"
WORKDIR="$(pwd)"
PHD_WORK_DIR="$WORKDIR"

# provide revision information, requires git repository
cd "$SOURCEDIR"
PHD_REV=$(git log --pretty=format:"Data revision %h, %ad" --date=iso -1)
if [ $? -ne 0 ]; then
   PHD_REV="Data revision unknown, "$(date +"%F %T %z")
fi
cd "$WORKDIR"
echo "$PHD_REV" > revision.txt

# generate job script from template
sed -e "s:_PHD_WORK_DIR:$PHD_WORK_DIR:g" \
    -e "s:_PHD_JOBNAME:$PHD_JOBNAME:g" \
    -e "s:_PHD_NODES:$PHD_NODES:g" \
    -e "s:_PHD_WALLTIME_HR:$PHD_WALLTIME_HR:g" \
    -e "s:_PHD_WALLTIME_MIN:$PHD_WALLTIME_MIN:g" \
    -e "s:_PHD_PROJECT_FILE:$PHD_PROJECT_FILE:g" \
    -e "s:_PHD_PROJECT_ARGS:$PHD_PROJECT_ARGS:g" \
    -e "s:_PHD_CODE:$PHD_CODE:g" \
    -e "s:_PHD_MODE:$PHD_MODE:g" \
    -e "s:_PHD_SOURCE_DIR:$PHD_SOURCE_DIR:g" \
    -e "s:_PHD_SCAN_FILES:$PHD_SCAN_FILES:g" \
    -e "s:_PHD_LOGLEVEL:$PHD_LOGLEVEL:g" \
    "$SCRIPTDIR/pmsco.sge.template" > $PHD_JOBNAME.job

chmod u+x "$PHD_JOBNAME.job"

if [ "$NOSUB" != "true" ]; then

# suppress bash error [stackoverflow.com/questions/10496758]
unset module

# submit the job script
# EMAIL must be defined in the environment
if [ -n "$EMAIL" ]; then
  qsub -q $QUEUE -m ae -M $EMAIL $PHD_JOBNAME.job
else
  qsub -q $QUEUE $PHD_JOBNAME.job
fi

fi

exit 0
