# -------------------------------------------------------------------------
# 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 $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 

# -------------------------------------------------------------------------
# Generate time series variable files - currently only working on yellowstone
# -------------------------------------------------------------------------
  if ($DOUT_S_GENERATE_TSERIES == 'TRUE') then
     if ($DOUT_S_SAVE_ALL_ON_DISK == 'TRUE' && $MACH == 'yellowstone') then
	if ($DOUT_S_SAVE_HISTORY_FILES == 'FALSE') then
	    echo "*WARNING* successful generation of the variable time series files"
	    echo "will delete all the time slice history files in $DOUT_S_ROOT"
	 else
	    echo "History time slice files will be saved after variable time series files"
	    echo "are generated. Please manually remove history files in order to"
	    echo "conserve disk space."
	 endif
         echo "Starting generating variable time series files to $DOUT_S_ROOT"
	 cd $CASEROOT/Tools
	 mpirun.lsf ./cesm_tseries_generator.py >>& $CASEROOT/tseriesStatus
	 cd $CASEROOT
         echo "Completed generating variable time series files to $DOUT_S_ROOT"
     else
	 echo "Variable time series file generation depends on $DOUT_S and $DOUT_S_SAVE_ALL_ON_DISK set to TRUE"
	 echo "Variable time series files were not generated."   
     endif
  endif
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


