
#======================================================================
# Setup
#======================================================================

cd $CASEROOT

# turn on memory leak check - if comparing with baseline also 
# compare memory highwater mark from baseline 
set DETECT_MEMORY_LEAK
if ($?COMPARE_BASELINE ) then
  set COMPARE_MEMORY
  set COMPARE_THROUGHPUT
endif

# Reset beginning test settings
if ( -e env_mach_pes.xml.1 )  then
  cp -f env_mach_pes.xml.1   env_mach_pes.xml
  cp -f env_mach_pes.xml.1   LockedFiles/env_mach_pes.xml.locked
  rm    $EXEROOT/cesm.exe >& /dev/null
  cp -f $EXEROOT/cesm.exe.1 $EXEROOT/cesm.exe || exit -9
  cp -f env_build.xml.1      env_build.xml
  cp -f env_build.xml.1      LockedFiles/env_build.xml.locked
else
  echo " ERROR: env_mach_pes.xml.1 does not exist" 
  echo "   this would been produced in the build - must run $CASE.test_build"
  exit -1
endif

# note - if you change the env_mach_pes.xml file - should always
# rerun the following two cesm_setup commands to ensure that the right
# settings are in the run script
# note that the following two commands will eliminate all the batch files except
# for the test file and copy the env_mach_pes.xml to the LockedFiles directory
./cesm_setup -clean -testmode
./cesm_setup

set STOP_N      = `./xmlquery STOP_N      -value`
set STOP_OPTION = `./xmlquery STOP_OPTION -value`

if (${STOP_N} < 3) then
  echo "ERROR:  stop_n too short" >>& $TESTSTATUS_LOG
  echo "TFAIL ${CASE} "     >>& $TESTSTATUS_OUT
  exit -1
endif

@ stopn1 = ${STOP_N}
@ restn1 = ${stopn1} / 2 + 1
@ histn  = ${stopn1}
@ stopn2 = ${stopn1} - ${restn1}

#======================================================================
# (1) do an initial run test
#======================================================================

cd $CASEROOT

echo "" >>& $TESTSTATUS_LOG 
echo "initial: doing a ${stopn1} ${STOP_OPTION} initial test writing a restart at ${restn1} ${STOP_OPTION}" >>& $TESTSTATUS_LOG 

./xmlchange -file env_run.xml -id CONTINUE_RUN -val FALSE
./xmlchange -file env_run.xml -id STOP_N       -val ${stopn1}
./xmlchange -file env_run.xml -id REST_OPTION  -val ${STOP_OPTION}
./xmlchange -file env_run.xml -id REST_N       -val ${restn1}
./xmlchange -file env_run.xml -id HIST_OPTION  -val ${STOP_OPTION}
./xmlchange -file env_run.xml -id HIST_N       -val ${histn}

./$CASE.run
if ($status != 0) then
    echo " ERROR: $CASE.run failed" >>& $TESTSTATUS_LOG
    exit -1
endif

set CplLogFile = `ls -1t $RUNDIR/cpl.log* | head -1`
if ( $?CplLogFile ) then
    if (-e $CplLogFile) then
       set pass = `zgrep "SUCCESSFUL TERMINATION" $CplLogFile | wc -l`
       if ($pass != 1) then
           echo "ERROR: coupler log $CplLogFile indicates model run failed" >>& $TESTSTATUS_LOG 
 	   exit -1
       else 
           echo "Success: test log is $CplLogFile" >>& $TESTSTATUS_LOG 
       endif
    endif
else
    echo "ERROR: no coupler log created, model run failed" >>& $TESTSTATUS_LOG
    exit -1
endif

echo "" >>& $TESTSTATUS_LOG
echo "moving relevant history files to suffix with command " >>& $TESTSTATUS_LOG
echo "$SCRIPTSROOT/Tools/component_compare_move.sh -rundir $RUNDIR -testcase $CASE -suffix base $add_iop" >>& $TESTSTATUS_LOG
echo "" >>& $TESTSTATUS_LOG

$SCRIPTSROOT/Tools/component_compare_move.sh -rundir $RUNDIR -testcase $CASE -suffix "base" $add_iop

#======================================================================
# (2) do a restart run
#======================================================================

cd $CASEROOT

echo "restart: doing a ${stopn2} ${STOP_OPTION} restart test"  >>& $TESTSTATUS_LOG 
echo " halving the number of mpi    tasks   for all components with ntasks > 1" >>& $TESTSTATUS_LOG 
echo " halving the number of openmp threads for all components with nthrds > 1" >>& $TESTSTATUS_LOG 

./xmlchange -file env_run.xml -id CONTINUE_RUN -val TRUE
./xmlchange -file env_run.xml -id STOP_N       -val ${stopn2}
./xmlchange -file env_run.xml -id REST_OPTION  -val never
./xmlchange -file env_run.xml -id HIST_N       -val ${histn}

cp -f env_mach_pes.xml.2 env_mach_pes.xml
cp -f env_mach_pes.xml.2 LockedFiles/env_mach_pes.xml

rm $EXEROOT/cesm.exe >& /dev/null
cp -f $EXEROOT/cesm.exe.2 $EXEROOT/cesm.exe || exit -9
cp -f env_build.xml.2    env_build.xml
cp -f env_build.xml.2    LockedFiles/env_build.xml.locked

./cesm_setup -clean -testmode
./cesm_setup

./$CASE.run
if ($status != 0) then
    echo " ERROR: $CASE.run failed" >>& $TESTSTATUS_LOG
    exit -1
endif

set CplLogFile = `ls -1t $RUNDIR/cpl.log* | head -1`
if ( $?CplLogFile ) then
    if (-e $CplLogFile) then
       set pass = `zgrep "SUCCESSFUL TERMINATION" $CplLogFile | wc -l`
       if ($pass != 1) then
           echo "ERROR: coupler log $CplLogFile indicates model run failed" >>& $TESTSTATUS_LOG 
 	   exit -1
       else 
           echo "Success: test log is $CplLogFile" >>& $TESTSTATUS_LOG 
       endif
    endif
else
    echo "ERROR: no coupler log created, model run failed" >>& $TESTSTATUS_LOG
    exit -1
endif

echo "" >>& $TESTSTATUS_LOG
echo "moving relevant history files to suffix with command " >>& $TESTSTATUS_LOG
echo "$SCRIPTSROOT/Tools/component_compare_move.sh -rundir $RUNDIR -testcase $CASE -suffix rest $add_iop" >>& $TESTSTATUS_LOG
echo "" >>& $TESTSTATUS_LOG

$SCRIPTSROOT/Tools/component_compare_move.sh -rundir $RUNDIR -testcase $CASE -suffix rest $add_iop

#======================================================================
# Check test status for all relevant component history files
#======================================================================

echo "DONE ${CASEBASEID} : ($msg finished, successful coupler log) " >>& $TESTSTATUS_LOG
echo "" >>& $TESTSTATUS_LOG

$SCRIPTSROOT/Tools/component_compare_test.sh -rundir $RUNDIR -testcase $CASE -testcase_base $CASEBASEID -suffix1 base -suffix2 rest $add_iop -msg "$msg" >>& $TESTSTATUS_OUT




