#!/bin/ksh
#echo === soumet a guillimin with arbitrary job id === 1>&2
Cell="$1"
if [[ "$2" != "submit" || "$1" != *guillimin*.clumeq.ca || ! -f $3 ]]
then
  echo usage: ${0##*/} guillimin[p2].clumeq.ca submit file_name
  exit 1
fi
QueuingTime=$(date +%s)
#TheFile="$(true_path $3)"
TheFile="$3"
#cd
#set -x
#[[ "$Cell" == guillimin.clumeq.ca ]]   && HeadNode=gm-schrm
#[[ "$Cell" == guilliminp2.clumeq.ca ]] && HeadNode=gm-schrmat
#[[ $(hostname) == lg-* ]] && HeadNode=$(hostname)
#HeadNode=localhost
HeadNode=$(hostname)
set -x
export MQSUB=$(which qsub 2>/dev/null)
export MQSUB=${MQSUB:-/opt/torque/x86_64/bin/qsub}
MY_JOB_ID=$(sed -e "s/=JoBQuEuEd/=${QueuingTime}/" <${TheFile} | ${MQSUB} - | grep '^[0-9][0-9]*' | head -1 | sed 's/[.].*//' )
#export MQSUB=/opt/moab/bin/msub
#[[ -n ${USE_QSUB} ]] && export MQSUB=/opt/torque/x86_64/bin/qsub
#if [[ $(hostname) == lg-* ]] ; then
#  MY_JOB_ID=$(sed -e "s/=JoBQuEuEd/=${QueuingTime}/" <${TheFile} | ssh localhost ${MQSUB} - | grep '^[0-9][0-9]*' | head -1 | sed 's/[.].*//' )
#else
#  MY_JOB_ID=$(sed -e "s/=JoBQuEuEd/=${QueuingTime}/" <${TheFile} | ssh $HeadNode ${MQSUB} - | grep '^[0-9][0-9]*' | head -1 | sed 's/[.].*//' )
#fi
set +x
#
if [[ -n $MY_JOB_ID ]] 
then
  echo "Your job $MY_JOB_ID has been submitted by $HeadNode"
  Status=0
else
  echo "WARNING: msub on $HeadNode may not have been able to submit job ${TheFile} properly" 1>&2
#  echo "ERROR: msub on $HeadNode was not able to submit job ${TheFile} properly" 1>&2
  Status=0
  MY_JOB_ID="UNKNOWN"
fi
echo "# JOB ID = $MY_JOB_ID" >>${TheFile}
exit ${Status}
