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

cd $CASEROOT

# Reset beginning test settings
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

# 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 CONTINUE_RUN -val FALSE
./xmlchange -file env_run.xml -id REST_OPTION  -val never
./xmlchange -file env_run.xml -id HIST_OPTION  -val '$STOP_OPTION'
./xmlchange -file env_run.xml -id HIST_N       -val '$STOP_N'

#======================================================================
# do a run on 1 pe with mpi
#======================================================================

cd $CASEROOT

echo "doing a ${STOP_N} ${STOP_OPTION} run with 1 pe and mpi " >>& $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

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

set cplprof = `ls -1t $CASEROOT/timing/cesm_timing.$CASE* | head -1`
set CPLPROF_GENCMP = $cplprof

#======================================================================
# do another run on 1 pe with mpiserial
#======================================================================

cd $CASEROOT

echo "doing a ${STOP_N} ${STOP_OPTION} run with 1 pe and mpiserial " >>& $TESTSTATUS_LOG 

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

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

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

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 mpiserial -msg "compare .base and .mpiserial files" >>& $TESTSTATUS_OUT



