
#======================================================================
# 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
  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 env_run.xml settings 
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 none

#======================================================================
# do an initial run test with NINST 1
#======================================================================

cd $CASEROOT

echo "default: doing a ${STOP_N} ${STOP_OPTION} with NINST1" >>& $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

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

#======================================================================
# do an initial run test with NINST 2
# want to run on same pe counts per instance and same cpl pe count
#======================================================================

cd $CASEROOT

echo "default: doing a ${STOP_N} ${STOP_OPTION} with NINST 2" >>& $TESTSTATUS_LOG 

cp -f env_mach_pes.xml.2 env_mach_pes.xml
cp -f env_mach_pes.xml.2 LockedFiles/env_mach_pes.xml
./cesm_setup -clean -testmode
./cesm_setup

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

./$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 multiinst"  >>& $TESTSTATUS_LOG 
echo "" >>& $TESTSTATUS_LOG

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

#======================================================================
# Check test status:
#======================================================================

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 multiinst -msg compare .base with .multiinst" >>& $TESTSTATUS_LOG
echo ""																   	                                   >>& $TESTSTATUS_LOG

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

