#! /bin/sh
 
#   Dec 03/08 - F.Majaess (Revised for sa/saiph, ib/dorval-ib, af/alef)
#   Oct 16/06 - F.Majaess (Revised for ma/maia, ns/naos)
#   Mar 01/06 - F.Majaess (Revised for rg/rigel)
#   Jan 25/05 - F.Majaess (Revised for "lxsrv,[lx,ltop][0-9][0-9]")
#   Jun 20/03 - F.Majaess (Revised for az/azur)
#   Feb 25/02 - F.Majaess (Revised for ya/yata)
#   Sep 14/00 - F.Majaess (Revised for tu/tsunami)
#   Jan 05/00 - F.Majaess (Added kz/kaze)
#   Jun 11/99 - F.Majaess (Removed "oldmod" support)
#   May 04/98 - F.Majaess (Removed o2000-2)
#   Mar 27/98 - F.Majaess (Added o2000-2)
#   Jul 14/97 - F.Majaess (Added sx/hiru)
#   Nov 04/96 - F.Majaess (Revised to honour and use "oldiag" environment 
#                          variable, when it's set by the user, in setting 
#                          the path to the standard diagnotic library to 
#                          use) 
#    Sep 11/96 - F.Majaess (Adjust path massaging for Victoria)
#    Mar 14/96 - F.Majaess (Added sx4)
#    Jan 24/95 - F.Majaess (Revise for loadleveler setup in Victoria)
#    Sep 27/95 - F.Majaess (Revise the path massaging for Victoria)
#    Jun 01/95 - F.Majaess (Replace cr01 by orion)
#    Apr 11/95 - M.Lazare. Modified to add Downsview as a Site_ID and
#                          replacing cidsv08 by cr01.
#    Feb 21/95 - F.Majaess
 
#id  rsub3   - submit a local file to a local or remote site. 
 
#    AUTHOR  - F.Majaess
 
#hd  PURPOSE - rsub3 script is used to submit a batch job to run
#hd            on a local site machine or to transfer "fn" from
#hd            local machine to remote server's "despath" 
#hd            directory and then submit the file to the 
#hd            appropriate nqs queue. 

#hd           The "log" option routes all printout back to the user's .queue
#hd           directory via NQS. This is useful if a job dies before the
#hd           printout is routed via the comjcl/endjcl common decks. Usually,
#hd           the printout does not return to the user in such situations.
#hd           This option ensures that some printout comes back which may be
#hd           prove useful in debugging the problem. Also, this is a way to
#hd           get a listing of all parmsub and condef parameters as they are
#hd           defined at the beginning of the job.
 
#pr  PARAMETERS:
#pr
#pr    POSITIONAL     
#pr
#pr      fn = file containing submission job (filename may contain path
#pr           information)
#pr
#pr    PRIMARY
#pr
#pr     hr/hare                   = switch to execute job on hare.
#pr
#pr     br/brooks                 = switch to execute job on brooks.
#pr
#pr     dy/daley                  = switch to execute job on daley.
#pr
#pr     ba/banting                = switch to execute job on banting.
#pr
#pr     p1/ppp1                   = switch to execute job on ppp1.
#pr
#pr     p2/ppp2                   = switch to execute job on ppp2.
#pr
#pr     p3/ppp3                   = switch to execute job on ppp3.
#pr
#pr     p4/ppp4                   = switch to execute job on ppp4.
#pr
#pr     pp                        = Generic switch targeting "ppp[1,2,3,4]" if invoked
#pr                                 from platforms associated with same "hall".
#pr                                (Decison is based on "HDNODE" active setting)
#pr
#pr     despath= subdirectory relative to $HOME in which the transferred 
#pr              job get temporarily stored at remote server (if applicable)
#pr              (=.queue).
#pr
#pr    PRIMARY/SECONDARY
#pr
#pr     script = switch to generate a copy of the pre-processed job
#pr              script (i.e the next job in the string) without sending
#pr              the job to NQS (diagnostic jobs only)
#pr     resume = switch to resume a string
#pr        log = switch to turn on routing of all printout via NQS
#pr              instead of relying on code in comjcl/endjcl (or
#pr              comgcm/endgcm for the model)
#pr              
#pr
#pr   continue = switch to continue a string (used only in endjcl.cdk)
#pr
#pr   delaymin = optional switch valid only for job targeting one of AIX 
#pr              Power7 clusters at CMC in Dorval. If specified,it signals
#pr              to defer start execution of the job by "delaymin" time
#pr              in minutes.
#pr
#pr     premtn = optional switch which allows targeting "preemptable" instead
#pr              of "development" class on the "Production" P7 cluster where,
#pr              the job will be subject to pre-emption. 
#pr              =on/yes; option enabled.
 
#ex  EXAMPLE: 
#ex
#ex          rsub3 myjob hr
#ex
#ex    The above example submits 'myjob' in the current directory to Hare.
#ex
#ex     rsub3 jobfil delaymin=30 br
#ex
#ex     The above example results in submitting the job to Brooks with
#ex     deferred start execution by 30 minutes.
#ex

# set -xv 
# set -x
# echo "Rsub3 - `hostname`"
rsub3_drctv="$@"
if [ -s "$CUSTOM_ENV_FILE" ] ; then
 . $CUSTOM_ENV_FILE || :
else
#  if [ -z "$SYSTEM_SETUP" -a -z "$CCRNSRC" -a -d "/users/tor/acrn/src/generic/."  ] ; then [ -s "/users/tor/acrn/src/generic/cccma_setup_profile" ] && . /users/tor/acrn/src/generic/cccma_setup_profile || : ; fi
 if [ -z "$SYSTEM_SETUP" -a -z "$CCRNSRC" -a -d /home/scrd101/generic/. ] ; then [ -s /home/scrd101/generic/sc_cccma_setup_profile ] && . /home/scrd101/generic/sc_cccma_setup_profile || : ; fi
fi
if [ "$SITE_ID" != 'Victoria' -a "$SITE_ID" != 'Dorval' -a "$SITE_ID" != 'DrvlSC' ] ; then
  exec submit3 $* 
fi
resume=''
nonqs=''
continue=''
log=''
script=''

#  * Obtain the submission file name and any specified option.

# for arg in $@
for arg in $rsub3_drctv
do
  case $arg in
       -*) set $arg ; set_opt=$arg                        ;;
      *=*) eval $arg                                      ;;
   hr|hare|br|brooks|dy|daley|ba|banting) dest=${dest:=$arg} ;;
   p1|ppp1|p2|ppp2|p3|ppp3|p4|ppp4) dest=${dest:=$arg} ;;
#  ha|hadar|sp|spica) dest=${dest:=$arg} ;;
#     lxsrv|lxsrv2|lxsrvnew|lxwrk1|lxwrk2|lx[0-9][0-9]|ltop[0-9][0-9]) dest=${dest:=$arg} ;;
#     lxlp[0-9][0-9]) dest=${dest:=$arg} ;;
#                               victest) dest=${dest:=$arg} ;;
      resume) resume='resume'                             ;;
      nonqs) nonqs='nonqs'                             ;;
     topdog) topdog='yes'                               ;;
     premtn) premtn='yes'                               ;;
     pha) dest='hadar' ; premtn='yes' ;;
     psp) dest='spica' ; premtn='yes' ;;
    continue) continue='continue'                         ;;
         log) log='log'                                   ;;
      script) script='script'                             ;;
       pp) if [ "$HDNODE" = 'hare' -o "$HDNODE" = 'eccc-ppp1' ] ; then
            dest=${dest:='ppp1'}
           elif [ "$HDNODE" = 'brooks' -o "$HDNODE" = 'eccc-ppp2' ] ; then
            dest=${dest:='ppp2'}
           elif [ "$HDNODE" = 'banting' -o "$HDNODE" = 'eccc-ppp3' ] ; then
            dest=${dest:='ppp3'}
           elif [ "$HDNODE" = 'daley' -o "$HDNODE" = 'eccc-ppp4' ] ; then
            dest=${dest:='ppp4'}
           else
            fn=${fn:=$arg}
           fi ;;
        *) fn=${fn:=$arg} ;;
  esac
done
 
#  * Set the defaults.

eval "despath=${despath='.queue'}"
eval "continue=${continue='  '}"
eval "script=${script='  '}"
eval "resume=${resume='  '}"
eval "nonqs=${nonqs='  '}"
eval "log=${log='  '}"
if [ -n "$oldiag" ] ; then
  oldiag="oldiag=$oldiag"
fi
if [ -n "$topdog" ] ; then
  topdog="topdog=$topdog"
fi
if [ -n "$premtn" ] ; then
  premtn="premtn=$premtn"
fi
if [ -n "$delaymin" ] ; then
  delaymin="delaymin=$delaymin"
fi

if [ -n "$mdest" ] ; then
  dest=$mdest
fi
#  * Prompt for a destination if none was specified.

while [ -z "$dest" ]
do
  echo "please enter a destination: > \\c"
  read tdest
  case $tdest in
   hr|hare|br|brooks|dy|daley|ba|banting) dest=$tdest ;;
   p1|ppp1|p2|ppp2|p3|ppp3|p4|ppp4) dest=$tdest ;;
   # ha|hadar|sp|spica)  dest=$tdest ;;
   #lxsrv|lxsrv2|lxsrvnew|lxwrk1|lxwrk2|lx[0-9][0-9]|ltop[0-9][0-9])    dest=$tdest ;;
   #lxlp[0-9][0-9])    dest=$tdest ;;
   #                                    victest)         dest=$tdest ;;
       *) echo "illegal destination $tdest ! "                       ;;
  esac
done

if [ -z "$mdest" ] ; then
  case $dest in
    hare|hr)       mdest='hare'         ;;
    brooks|br)       mdest='brooks'         ;;
    daley|dy)       mdest='daley'         ;;
    banting|ba)       mdest='banting'         ;;
    ppp1|p1)       mdest='ppp1'         ;;
    ppp2|p2)       mdest='ppp2'         ;;
    ppp3|p3)       mdest='ppp3'         ;;
    ppp4|p4)       mdest='ppp4'         ;;
   #  lxsrv)        mdest='lxsrv'         ;;
   # lxsrv2)        mdest='lxsrv2'        ;;
   #lxsrvnew)        mdest='lxsrvnew'      ;;
   # lxwrk1)        mdest='lxwrk1'        ;;
   # lxwrk2)        mdest='lxwrk2'        ;;
   # lx[0-9][0-9]|lxlp[0-9][0-9]|ltop[0-9][0-9]) mdest=$dest ;;
  esac
fi
## if [ -n "$mdest" -a "$mdest" = 'orion' ] ; then 
##  echo "" ; echo "rsub3: Error; $mdest destination is no longer supported!"
##  tty -s && InTrCtv='true' || InTrCtv='false'
##  if [ "$InTrCtv" = 'false' ] ; then 
##   touch haltit
##   ( echo "" ; echo "rsub3: Error; $mdest destination is no longer supported!") >> haltit
##  fi
##  exit 2
## fi
# Ensure Loadleveler queues are not specified under NQS setup.

if [ "$SITE_ID" = 'Victoria' -a ! -z "$NQS_QSUB" ] ; then
 case $mdest in
  vic*) echo " Sorry, $mdest destination is not valid with NQS setup !" ;;
   *  ) : ;;
 esac
fi

#
# * Make sure submission job file is specified
#

if [ "$resume" != "resume" -a "$continue" != "continue" ] ; then

 while [ -z "$fn" ] ; do
  echo "please enter [path/]filename ? > \\c"
  read fn
 done

#  * Set default path to the submission job (if necessary)

 tarpath=`expr $fn : '\(.*\)/'`
 if [ "$tarpath" = '..' -o "$tarpath" = '.' ] ; then Cwd=`pwd` ; tarpath=`expr ${Cwd}/${fn} : '\(.*\)/'` ; fi
 tarpath=${tarpath:=`pwd`}
 target=`expr //$fn : '.*/\(.*\)'`
 while [ -z "$target" ] ; do
  echo "please enter filename ? > \\c"
  read target
 done

#  * Check if the submission job file exists, readable and non-empty
#  * otherwise, issue an abort message and exit.

 HOSTID=${HOSTID-`hostname | cut -d'.' -f1`}
 HOSTID=`echo $HOSTID | sed -e 's/hpcr4-in/ppp4/' -e 's/hpcr3-in/ppp3/' `

 if [ ! -s "$tarpath/$target" ] ; then
  eval "echo $tarpath/$target is not a valid filename !"
  exit 1
 fi
 target="$tarpath/$target"
 
else

 while [ -z "$fn" ] ; do
  echo "please enter filename ? > \\c"
  read fn
 done
 target=$fn

fi 

if [ -n "$JHOME" ] ; then
 jhome="JHOME=$JHOME" 
fi
if [ -n "$reset_jhome" ] ; then
 reset_jhome="reset_jhome=$reset_jhome" 
fi
#
#
# * Invoke 'submit3' if the destination is local
# * otherwise, invoke 'rsubmit3' to transfer and submit the
# * on the remote site.

if [ "$SITE_ID" = 'Victoria' -o "$SITE_ID" = 'Dorval' -o "$SITE_ID" = 'DrvlSC' ] ; then
  case $mdest in
             dy|daley|ba|banting|hr|hare|p1|ppp1|p2|ppp2|p3|ppp3|p4|ppp4) if [ "$SITE_ID" = 'DrvlSC' ] 
                                       then
                                        Cwd=`pwd`
                                        if [ -s "$CUSTOM_ENV_FILE" ] ; then
                                         echo " . $CUSTOM_ENV_FILE ; cd $Cwd ; submit3 $set_opt $target $mdest $script $continue $resume $log $nonqs $oldiag $topdog $premtn $delaymin $jhome $reset_jhome" | ssh $mdest "bash -l"
                                        else
                                         echo "cd $Cwd ; submit3 $set_opt $target $mdest $script $continue $resume $log $nonqs $oldiag $topdog $premtn $delaymin $jhome $reset_jhome" | ssh $mdest "bash -l"
                                        fi
                                      #else
                                      # rsubmit3 $set_opt $target $mdest $script $continue $resume despath=$despath $log $nonqs $oldiag $topdog $premtn $delaymin $jhome $reset_jhome
                                       fi ;;
                           br|brooks) if [ "$SITE_ID" = 'DrvlSC' ] 
                                       then
                                        HOSTIDf6=`echo $HOSTID | sed -e 's/eccc.*-ppp/cs/g' -e 's/^ppp/cs/g' | cut -c 1-6`
                                        HOSTIDfm=`echo $HOSTID | sed -e 's/eccc.*-ppp/cs/g' -e 's/^ppp/cs/g' | sed -e 's/xc2mom.*-p2/xc2mom-p2/'`
                                        Cwd=`pwd`
                                        Tmdest="$mdest"
                                        if [ "$HOSTIDf6" = 'xc2net' -o "$HOSTIDfm"  = 'xc2mom-p2' ] ; then
                                         Tmdest='brooks-test'
                                        fi
                                        if [ -s "$CUSTOM_ENV_FILE" ] ; then
                                         echo " . $CUSTOM_ENV_FILE ; cd $Cwd ; submit3 $set_opt $target $mdest $script $continue $resume $log $nonqs $oldiag $topdog $premtn $delaymin $jhome $reset_jhome" | ssh $Tmdest "bash -l"
                                        else
                                         echo "cd $Cwd ; submit3 $set_opt $target $mdest $script $continue $resume $log $nonqs $oldiag $topdog $premtn $delaymin $jhome $reset_jhome" | ssh $Tmdest "bash -l"
                                        fi
                                      #else
                                      # rsubmit3 $set_opt $target $mdest $script $continue $resume despath=$despath $log $nonqs $oldiag $topdog $premtn $delaymin $jhome $reset_jhome
                                       fi ;;
#  lxsrv|lxsrv2|lxsrvnew|lxwrk1|lxwrk2|lx[0-9][0-9]|lxlp[0-9][0-9]|ltop[0-9][0-9]) if [ "$SITE_ID" = 'Victoria' -a "$OS" = 'Linux' -a "$HOSTID" = "$mdest" ]
#                                      then
#                                       submit3 $set_opt $target mdest=$mdest $script $continue $resume $log $nonqs $oldiag $topdog $premtn $delaymin $jhome $reset_jhome 
#                                      else
#                                       rsubmit3 $set_opt $target $mdest $script $continue $resume despath=$despath $log $nonqs $oldiag $topdog $premtn $delaymin $jhome $reset_jhome
#                                      fi ;; 
       *) echo "illegal destination $mdest ! "
          exit 2
  esac
else
  echo " SITE_ID environment must be set to supported sites Victoria or Dorval, sorry"
  exit 3
fi
