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

cd $CASEROOT

# Reset beginning test settings
rm $EXEROOT/cesm.exe >& /dev/null
cp -f $EXEROOT/cesm.exe.mct $EXEROOT/cesm.exe || exit -9
cp -f env_build.xml.mct  env_build.xml
cp -f env_build.xml.mct  LockedFiles/env_build.xml.locked

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

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

#======================================================================
# do an mct test
#======================================================================

cd $CASEROOT

echo "doing a ${STOP_N} ${STOP_OPTION} mct test" >>& $TESTSTATUS_LOG 

# need to source the env_mach_specific file for the mct environment
source $CASEROOT/env_mach_specific
if ($status != 0) then
   echo "Error: problem sourcing env_mach_specific"; exit -2
endif 

./$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

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

#======================================================================
# do an esmf test
#======================================================================

cd $CASEROOT

echo "doing a ${STOP_N} ${STOP_OPTION} esmf test" >>& $TESTSTATUS_LOG 

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

# need to source the env_mach_specific file for the esmf environment
source $CASEROOT/env_mach_specific
if ($status != 0) then
   echo "Error: problem sourcing env_mach_specific"; exit -2
endif 

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

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

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

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

echo "DONE ${CASEBASEID} : (test 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 "esmf" -msg "esmf test .base and .esmf files" >>& $TESTSTATUS_OUT


