#! /bin/csh -x
#SBATCH -J 999slurm
#SBATCH -o 999slrXX.log
#SBATCH -n 48 
#SBATCH -t 01:00:00
#SBATCH -p coaps_q 
#
set echo
C
setenv OS `uname`
switch ($OS)
case 'AIX':
    hostname
    setenv TMPDIR /scr/${user}
    breaksw
case 'SunOS':
#   assumes /usr/5bin is before /bin and /usr/bin in PATH.
    setenv TMPDIR /tmp
    breaksw
case 'Linux':
    which yod
    if (! $status) then
      setenv OS XT3
      setenv TMPDIR /tmp
    else
      setenv TMPDIR /tmp
    endif
    which aprun
    if (! $status) then
#     setenv OS XT4
#     setenv OS XT5
      setenv OS XC30
      setenv TMPDIR /tmp
    endif
    which dplace
    if (! $status) then
      setenv OS HPE
    endif
    which poe
    if (! $status) then
      setenv OS IDP
    endif
    breaksw
case 'OSF1':
    setenv TMPDIR /tmp
    breaksw
case 'IRIX64':
    setenv TMPDIR /tmp
    breaksw
case 'unicos':
    setenv ACCT "NO2031"
#   newacct $ACCT
#   source ~/.login
    breaksw
default:
    echo 'Unknown Operating System: ' $OS
    exit (1)
endsw


C --- Automatic Run Script.
C --- Submit with msub, or msub_csh, or msub_slurm_wrapper command.
C --- Multiple segment version, set no. of segments on foreach below.
C
setenv EX ~/HYCOM3-examples/GOMb0.08/expt_10.0 
cd ${EX}
C
C --- E is expt, P is permenant directory,  SE is /tmp directory.
C
setenv E 100
setenv P $cwd
switch ($OS)
case 'HPE':
case 'XC30':
#                           substitute /p/work1 for /p/home
    setenv S `echo $cwd | awk '{print "/p/work1" substr($0,8,length)}'`
    breaksw
case 'AIX':
case 'XT5':
case 'IDP':
#                           substitute /scr for /u/home
    setenv S `echo $cwd | awk '{print "/scr" substr($0,8,length)}'`
    breaksw
case 'SunOS':
#                           substitute /scr for /u/home
    setenv S `echo $cwd | awk '{print "/scr" substr($0,8,length)}'`
#                           substitute /net/ajax/scrf for /home/ajax
#   setenv S `echo $cwd | awk '{print "/net/ajax/scrf" substr($0,11,length)}'`
    breaksw
case 'XT3':
case 'XT4':
case 'OSF1':
#                           substitute /work for /usr/local/u
    setenv S `echo $cwd | awk '{print "/work" substr($0,13,length)}'`
    breaksw
case 'IRIX64':
#                           substitute /scr for /u/home
    setenv S `echo $cwd | awk '{print "/scr" substr($0,8,length)}'`
    breaksw
case 'unicos':
#                           substitute /tmp for /u/b
    setenv S `echo $cwd | awk '{print "/tmp" substr($0,5,length)}'`
    breaksw
endsw

setenv SE ${S}/

switch ($OS)
case 'AIX':
  hostname
  setenv TMPDIR /scr/${user}
  breaksw
case 'SunOS':
# assumes /usr/5bin is before /bin and /usr/bin in PATH.
  setenv TMPDIR /tmp
  breaksw
case 'Linux':
case 'XT3':
case 'XC30':
case 'XC40':
case 'HPE':
case 'HPEI':
case 'OSF1':
case 'IRIX64':
  setenv TMPDIR /tmp
  breaksw
case 'unicos':
    setenv ACCT "NO2031"
#   newacct $ACCT
#   source ~/.login
    breaksw
default:
    echo 'Unknown Operating System: ' $OS
    exit (1)
endsw
C
setenv
if ($?JOBNAME) then
    setenv SBATCH_JOBNAME ${JOBNAME}
    setenv SBATCH_JOBID   $$ 
endif
C
C
ls -laFq
C
C --- check the RUNNING flag.
C
if ( -e RUNNING && ! -e RUNNING_${SLURM_JOB_ID}) then
C
C --- MODEL IS ALREADY RUNNING - EXIT.
C
  exit
endif
touch RUNNING
touch RUNNING_${SLURM_JOB_ID}
C
C --- Number of segments specified by y and n foreach loops
C --- Use '( 1 )' in both for a single segment per run.
C
@ i = 0
foreach y ( 1  )
  foreach m ( 1 )
    @ i = $i + 1
    echo "Starting SEGMENT " $i
C
C --- Check the model list.
C
touch LIST
if ( -z LIST ) then
C
C --- MODEL RUN IS COMPLETE - EXIT.
C
  /bin/rm -f RUNNING
  /bin/rm -f RUNNING_${SLURM_JOB_ID}
  exit
endif
C
C --- Take the next run from LIST.
C
setenv Y01 `head -1 LIST | awk '{print $1}'`
C
if ( `echo $Y01 | egrep 'com' | wc -l` != 0 ) then
C
C --- Precalculated model script.
C
  setenv SCRIPT ${Y01}
  if (! -e $SCRIPT ) then
C
C --- PREDEFINED SCRIPT DOES NOT EXIST - EXIT.
C
    ls -laFq
    /bin/rm -f RUNNING
    /bin/rm -f RUNNING_$SLURM_JOB_ID
    exit
  endif
else
C
C --- Generate the next model script.
C
  setenv AB `head -1 LIST | awk '{print $2}'`
  setenv SCRIPT ${E}y${Y01}${AB}.csh
  /bin/rm -f ${SCRIPT}
  awk -f ${E}.awk y01=${Y01} ab=${AB} ${E}.csh > ${SCRIPT}
endif
C
C --- Run the Script.
C
set script = $SCRIPT
set reqname = ${SLURM_JOB_NAME}
ln -fs ${reqname}.log $script:r.log
df -k
C
mkdir -p $SE
cp  ${SCRIPT} $SE/${SCRIPT}
cd  $SE
C
C ---------------------------------------------------------------------------
csh ${SCRIPT}
C ---------------------------------------------------------------------------
C
cd  $EX
C
C --- Clean Up.
C
ls -laFq
C
mv LIST LISTOLD
head -1 LISTOLD | comm -23 LISTOLD - > LIST
diff LIST LISTOLD
/bin/rm -f LISTOLD
C
C --- end of SEGMENT foreach loops
C
  end
end
C
C --- Final Clean Up.
C
/bin/rm -f RUNNING
/bin/rm -f RUNNING_${SLURM_JOB_ID}
C
touch LIST
if ( -z LIST ) then
C
C --- MODEL RUN IS COMPLETE - EXIT.
C
  exit
endif
C
C --- Submit the next job.
C
cat - > $TMPDIR/$$.awk <<'E-o-A'
BEGIN { for ( i=65;i <= 89; i++)
        c[sprintf("%c",i)] = sprintf("%c",i+1)
}
/[0-9]$/  { printf("%s%s\n",$0,"A")}
/[A-Y]$/  { printf("%s%s\n",substr($0,1,length($0)-1),c[substr($0,length($0))])}
/[1-9]Z$/ { printf("%s%s%s\n",substr($0,1,length($0)-2),substr($0,length($0)-1,1)+1,"A")}
/0Z$/     { next }
'E-o-A'
#
set newname = `echo ${reqname} | cut -c 2- | awk -f $TMPDIR/$$.awk`
#
/bin/rm -f $TMPDIR/$$.awk
if ( `echo $newname | wc -l` != 0 ) then
    if ($?JOBNAME) then
        setenv JOBNAME ${newname}
#       /bin/nice -7 csh ${E}pbs.csh >& ${newname}.log &
        csh ${E}slurm.csh >& ${newname}.log &
    else
#       kinit -R
#       qsub -N R${newname} -j oe -o R${newname}.log ${E}pbs.csh
#             -j oe -o R${newname}.log < ${E}pbs.csh
        sed -e "s/^#SBATCH  *-J .*/#SBATCH -J R${newname}/" -e "s/^#SBATCH  *-o .*/#SBATCH -o R${newname}.log/" ${E}slurm.csh >! ${E}slurm_$$.csh
        sbatch  ${E}slurm_$$.csh
        /bin/rm ${E}slurm_$$.csh
    endif
else
C
C --- newname failed
C
    echo $newname
    echo $newname | wc -l
endif
C
C --- Exit.
C
exit

