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

cd $CASEROOT
source ./Tools/ccsm_getenv

# -------------------------------------------------------------------------
# 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
grep 'SUCCESSFUL TERMINATION' $CPLLogFile  || echo "Model did not complete - see $RUNDIR/$CESMLogFile" && echo "run FAILED $sdate" >>& $CASEROOT/CaseStatus && exit -1

echo "run SUCCESSFUL $sdate" >>& $CASEROOT/CaseStatus


# -------------------------------------------------------------------------
# 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 $EXEROOT/*build.$LID.* $LOGDIR/bld  
  cp -p $RUNDIR/*log.$LID.*   $LOGDIR/      
endif

# -------------------------------------------------------------------------
# Perform short term archiving of output
# -------------------------------------------------------------------------
cd $CASEROOT

if ($DOUT_S == 'TRUE') 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') 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) 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 -file env_run.xml -id CONTINUE_RUN -val TRUE
      endif
    else
      ./xmlchange -file env_run.xml -id CONTINUE_RUN -val TRUE
    endif
    ./xmlchange -file env_run.xml -id RESUBMIT     -val $RESUBMIT

    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.csh -lid $LID 
  gzip timing/ccsm_timing_stats.$LID
endif

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


