# -------------------------------------------------------------------------
# Update env variables in case user changed them during run
# -------------------------------------------------------------------------

cd $CASEROOT

setenv CASETOOLS			`./xmlquery CASETOOLS			-value `
setenv CASE				`./xmlquery CASE			-value `
setenv RUNDIR				`./xmlquery RUNDIR			-value `
setenv LOGDIR				`./xmlquery LOGDIR			-value `
setenv DOUT_S				`./xmlquery DOUT_S			-value `
setenv DOUT_S_ROOT			`./xmlquery DOUT_S_ROOT			-value `
setenv DOUT_S_SAVE_ALL_ON_DISK		`./xmlquery DOUT_S_SAVE_ALL_ON_DISK	-value `
setenv DOUT_S_SAVE_HISTORY_FILES	`./xmlquery DOUT_S_SAVE_HISTORY_FILES	-value `
setenv MACH				`./xmlquery MACH			-value `
setenv DOUT_L_MS			`./xmlquery DOUT_L_MS			-value `
setenv BATCHQUERY			`./xmlquery BATCHQUERY			-value `
setenv BATCHSUBMIT			`./xmlquery BATCHSUBMIT			-value `
setenv RESUBMIT				`./xmlquery RESUBMIT			-value `
setenv COMP_RUN_BARRIERS		`./xmlquery COMP_RUN_BARRIERS		-value `
setenv CHECK_TIMING			`./xmlquery CHECK_TIMING		-value `
setenv SAVE_TIMING			`./xmlquery SAVE_TIMING			-value `

# -------------------------------------------------------------------------
# Check for successful run
# -------------------------------------------------------------------------

set sdate = `date +"%Y-%m-%d %H:%M:%S"`
cd $RUNDIR
set CESMLogFile = `ls -1t cesm.log* | head -1` 
if ($CESMLogFile == "") then
  echo "Model did not complete - no cesm.log file present - exiting"
  exit -1
endif
set CPLLogFile = `echo $CESMLogFile | sed -e 's/cesm/cpl/'`
if ($CPLLogFile == "") then
  echo "Model did not complete - no cpl.log file corresponding to most recent CESM log ($RUNDIR/$CESMLogFile)"
  exit -1
endif
set exceeded_max_time = 0
grep -q 'SUCCESSFUL TERMINATION' $CPLLogFile
if ( $status == 0 ) then
   echo "run SUCCESSFUL $sdate" >>& $CASEROOT/CaseStatus
else
  grep -q 'Wall clock time exceeds max_cplstep_time' $CPLLogFile
  if ( $status == 0 ) then
    echo "Model timestep time exceeded max_cplstep_time"
    echo "run EXCEEDED max_cplstep_time $sdate" >>& $CASEROOT/CaseStatus 
    set exceeded_max_time = 1
  else
    echo "Model did not complete - see $RUNDIR/$CESMLogFile" 
    echo "run FAILED $sdate" >>& $CASEROOT/CaseStatus 
    exit -1
  endif
endif

# -------------------------------------------------------------------------
# Save model output logs 
# -------------------------------------------------------------------------

gzip *.$LID
if ($LOGDIR != "") then
  if (! -d $LOGDIR/bld) mkdir -p $LOGDIR/bld || echo " problem in creating $LOGDIR/bld"
  cp -p $RUNDIR/*log.$LID.*   $LOGDIR/      
endif

# -------------------------------------------------------------------------
# Perform short term archiving of output
# -------------------------------------------------------------------------

cd $CASEROOT

if ($DOUT_S == 'TRUE' && $exceeded_max_time == 0) then
  echo "Archiving ccsm output to $DOUT_S_ROOT"
  echo "Calling the short-term archiving script st_archive"
   ./st_archive 
endif

# -------------------------------------------------------------------------
# Submit longer term archiver if appropriate
# -------------------------------------------------------------------------


if ($DOUT_L_MS == 'TRUE' && $DOUT_S == 'TRUE' && $exceeded_max_time == 0) then
  echo "Long term archiving ccsm output using the script $CASE.l_archive"
  set num = 0
  if ($LBQUERY == "TRUE") then
     set num = `$BATCHQUERY | grep $CASE.l_archive | wc -l`
  endif
  if ($LBSUBMIT == "TRUE" && $num < 1) then
cat > templar <<EOF
    $BATCHSUBMIT ./$CASE.l_archive
EOF
    source templar
    if ($status != 0) then
      echo "ccsm_postrun error: problem sourcing templar " 
    endif
    rm templar
  endif 
endif

# -------------------------------------------------------------------------
# Resubmit another run script
# -------------------------------------------------------------------------

if ($RESUBMIT > 0 || $exceeded_max_time == 1 ) then
  if ($exceeded_max_time == 0) then
    @ RESUBMIT = $RESUBMIT - 1
    echo RESUBMIT is now $RESUBMIT

    #tcraig: reset CONTINUE_RUN on RESUBMIT if NOT doing timing runs
    #use COMP_RUN_BARRIERS as surrogate for timing run logical
    if ($?COMP_RUN_BARRIERS) then
      if (${COMP_RUN_BARRIERS} == "FALSE") then
         ./xmlchange -noecho -file env_run.xml -id CONTINUE_RUN -val TRUE
      endif
    else
      ./xmlchange -noecho -file env_run.xml -id CONTINUE_RUN -val TRUE
    endif
    ./xmlchange -noecho -file env_run.xml -id RESUBMIT     -val $RESUBMIT
  endif
   if ($LBSUBMIT == "TRUE") then
cat > tempres <<EOF
   $BATCHSUBMIT ./$CASE.run
EOF
     source tempres
     if ($status != 0) then
       echo "ccsm_postrun error: problem sourcing tempres " 
     endif
     rm tempres
   endif 
endif

if ($CHECK_TIMING == 'TRUE') then
  if !(-d timing) mkdir timing
  $CASETOOLS/getTiming -lid $LID 
  gzip timing/cesm_timing_stats.$LID
endif

if ($SAVE_TIMING == 'TRUE') then
  mv $RUNDIR/timing $RUNDIR/timing.$LID
endif


