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

cd $CASEROOT

# 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
else
  cp -f env_mach_pes.xml     env_mach_pes.xml.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`

./xmlchange -file env_run.xml -id HIST_OPTION  -val ${STOP_OPTION}
./xmlchange -file env_run.xml -id HIST_N       -val ${STOP_N}
./xmlchange -file env_run.xml -id CONTINUE_RUN -val FALSE
./xmlchange -file env_run.xml -id REST_OPTION  -val never

#======================================================================
# do an initial run test with default layout
#======================================================================

cd $CASEROOT
echo "doing a ${STOP_N} ${STOP_OPTION} initial test with default layout" >>& $TESTSTATUS_LOG 

./$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 "moving relevant history files to suffix with command " >>& $TESTSTATUS_LOG
echo "$SCRIPTSROOT/Tools/component_compare_move.sh -rundir $RUNDIR -testcase $CASE -suffix base" >>& $TESTSTATUS_LOG 
echo "" >>& $TESTSTATUS_LOG

#======================================================================
# do another initial run with all rootpes at 0 (pure sequential)
#======================================================================

cd $CASEROOT

echo "doing a second a ${STOP_N} ${STOP_OPTION} with all rootpes set to 0" >>& $TESTSTATUS_LOG 

./xmlchange -file env_mach_pes.xml -id ROOTPE_ATM -val 0
./xmlchange -file env_mach_pes.xml -id ROOTPE_LND -val 0
./xmlchange -file env_mach_pes.xml -id ROOTPE_ROF -val 0
./xmlchange -file env_mach_pes.xml -id ROOTPE_WAV -val 0
./xmlchange -file env_mach_pes.xml -id ROOTPE_GLC -val 0
./xmlchange -file env_mach_pes.xml -id ROOTPE_OCN -val 0
./xmlchange -file env_mach_pes.xml -id ROOTPE_ICE -val 0
./xmlchange -file env_mach_pes.xml -id ROOTPE_CPL -val 0

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

./$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 "moving relevant history files to suffix with commands " >>& $TESTSTATUS_LOG
echo "$SCRIPTSROOT/Tools/component_compare_move.sh -rundir $RUNDIR -testcase $CASE -suffix seq"	>>& $TESTSTATUS_LOG 
echo "" >>& $TESTSTATUS_LOG

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

#======================================================================
# Check test functionality
#======================================================================

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

echo "obtaining test functionality from history files with commands " >>& $TESTSTATUS_LOG
echo "$SCRIPTSROOT/Tools/component_compare_test.sh -rundir $RUNDIR -testcase $CASE -testcase_base $CASEBASEID -suffix1 base -suffix2 seq -msg compare .base and .seq files" >>& $TESTSTATUS_LOG
echo ""	>>& $TESTSTATUS_LOG

$SCRIPTSROOT/Tools/component_compare_test.sh -rundir $RUNDIR -testcase $CASE -testcase_base $CASEBASEID -suffix1 base -suffix2 seq -msg "compare .base and .seq files" >>& $TESTSTATUS_OUT

