#!/bin/csh -f
# cesm_prerun without performance data archiving: 
#  ./cesm_prerun with the following argument keyword/value pairs, in any order
#   -machine <system name>
#   -compiler <compiler name> 
#   -case <case name>
#   -jobid <should be same as $PBS_JOBID for PBS and $COBALT_JOBID> for Cobalt>
#   -cesmid <$LID in cesm-lingo>
#   -user <should be same as $LOGNAME>
#   -caseroot <directory from which job script was submitted>
#   -rundir <run directory>
#   -save <TRUE|true|anything else>
#   -archive <performance archive root directory>
#   -sampling <sampling interval in seconds>

set machine = 'unknown'
set compiler = 'unknown'
set case = 'unknown'
set app_jobid = 'unknown'
set lid = 'unknown'
set user = 'unknown'
set caseroot = '.'
set run_dir = '.'
set save_timing = 'FALSE'
set save_timing_dir = '/UNSET'
set sample_interval = 0

set i = 1
while ($i < $#argv)
  if      ("X$argv[$i]" == 'X-machine') then
    @ i = $i + 1
    set machine = $argv[$i]
  else if ("X$argv[$i]" == 'X-compiler') then
    @ i = $i + 1
    set compiler = $argv[$i]
  else if ("X$argv[$i]" == 'X-case') then
    @ i = $i + 1
    set case = $argv[$i]
  else if ("X$argv[$i]" == 'X-jobid') then
    @ i = $i + 1
    set app_jobid = $argv[$i]
  else if ("X$argv[$i]" == 'X-cesmid') then
    @ i = $i + 1
    set lid = $argv[$i]
  else if ("X$argv[$i]" == 'X-user') then
    @ i = $i + 1
    set user = $argv[$i]
  else if ("X$argv[$i]" == 'X-caseroot') then
    @ i = $i + 1
    set caseroot = $argv[$i]
  else if ("X$argv[$i]" == 'X-rundir') then
    @ i = $i + 1
    set run_dir = $argv[$i]
  else if ("X$argv[$i]" == 'X-save') then
    @ i = $i + 1
    set save_timing = $argv[$i]
  else if ("X$argv[$i]" == 'X-archive') then
    @ i = $i + 1
    set save_timing_dir = $argv[$i]
  else if ("X$argv[$i]" == 'X-sampling') then
    @ i = $i + 1
    set sample_interval = $argv[$i]
  endif
  @ i = $i + 1
end

if (-e $run_dir/cesm_prerun_done) then
  /bin/rm $run_dir/cesm_prerun_done
endif

if ($app_jobid == 'unknown') then
  if ($machine == 'titan') then

    set app_jobid = $PBS_JOBID

  else if (($machine == 'edison') || ($machine == 'corip1') || ($machine == 'cori')) then

    set app_jobid = $SLURM_JOB_ID

  else if ($machine == 'mira') then

    set app_jobid = $COBALT_JOBID

  endif  
endif

if ($machine == 'titan') then

  set string = `qstat -f $app_jobid | grep ctime`
  echo "job $app_jobid created: $string" >>& $caseroot/CaseStatus

  set string = `qstat -f $app_jobid | grep qtime`
  echo "job $app_jobid queued: $string" >>& $caseroot/CaseStatus

  set string = `qstat -f $app_jobid | grep etime`
  echo "job $app_jobid eligible to run: $string" >>& $caseroot/CaseStatus

  set string = `qstat -f $app_jobid | grep start_time`
  echo "job $app_jobid started: $string" >>& $caseroot/CaseStatus

else if (($machine == 'edison') || ($machine == 'corip1') || ($machine == 'cori')) then

  set string = `sqs -f $app_jobid | grep SubmitTime | sed 's/ *\(SubmitTime=\S*\) *\(EligibleTime=\S*\) */\1/' `
  echo "job $app_jobid submitted: $string" >>& $caseroot/CaseStatus

  set string = `sqs -f $app_jobid | grep SubmitTime | sed 's/ *\(SubmitTime=\S*\) *\(EligibleTime=\S*\) */\2/' `
  echo "job $app_jobid eligible: $string" >>& $caseroot/CaseStatus

  set string = `sqs -f $app_jobid | grep StartTime | sed 's/ *\(StartTime=\S*\) *\(EndTime=\S*\) */\1/' `
  echo "job $app_jobid started: $string" >>& $caseroot/CaseStatus

else if ($machine == 'mira') then

  set string = `qstat -lf $app_jobid | grep SubmitTime`
  echo "job $app_jobid submitted: $string" >>& $caseroot/CaseStatus

  set string = `qstat -lf $app_jobid | grep StartTime`
  echo "job $app_jobid started: $string" >>& $caseroot/CaseStatus

endif

sleep 10

set sdate = `date +"%Y-%m-%d %H:%M:%S"`
echo "model started $sdate" >>& $caseroot/CaseStatus

touch $run_dir/cesm_prerun_done

