
#======================================================================
# Test 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

#--- clone the main case to create ref1 case
set CASER0  = ${CASEROOT}
set CASERR1 = ${CASEROOT}.ref1
set CASE0   = ${CASE}

cd $CASER0
./xmlchange DOUT_S_SAVE_ALL_ON_DISK=FALSE
set STOP_N      = `./xmlquery STOP_N          -value`
set STOP_OPTION = `./xmlquery STOP_OPTION     -value`

@ stopnf = ${STOP_N}
@ stopn1 = ${STOP_N} / 2
@ stopn2 = ${stopnf} - $stopn1

cd $CIMEROOT/scripts
rm -r -f ${CASERR1}; ./create_clone -case ${CASERR1} -clone ${CASER0} 

cd $CASERR1
./xmlchange -file env_build.xml -id EXEROOT        -val ${EXEROOT} || exit -1
./xmlchange -file env_build.xml -id BUILD_COMPLETE -val TRUE       || exit -1
./cesm_setup -testmode

#======================================================================
# (1) Test run: do a spinup run in the main case
#======================================================================

cd $CASER0

echo ""  >>& $TESTSTATUS_LOG
echo "doing a ${stopn1} ${STOP_OPTION} initial spinup test, writing restarts at end of run" >>& $TESTSTATUS_LOG 
echo " short term archiving is on" >>& $TESTSTATUS_LOG 

# set up run
set DOUT_S_ROOT          = `./xmlquery DOUT_S_ROOT     -value`
set DOUT_S_ROOT_LOCKED   = `echo $DOUT_S_ROOT | sed 's/archive/archive.locked/'`
set DOUTSR0              =  $DOUT_S_ROOT_LOCKED
set orig_clm_bldnml_opts = `./xmlquery CLM_BLDNML_OPTS -value`
set CASE                 = `./xmlquery CASE -value`
./xmlchange -file env_run.xml -id STOP_N          -val ${stopn1}                || exit -1
./xmlchange -file env_run.xml -id CLM_BLDNML_OPTS -val "-bgc_spinup on" -append || exit -1

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

# Run the short-term archiver.. this is a short-term hack until
# a better solution can be found.
./$CASE.st_archive
if($status != 0) then
    echo " ERROR: $CASE.st_archive failed ">>& $TESTSTATUS_LOG
    exit -1
endif 

# must look in short term archiving directory for cpl log files
set CplLogFile = `ls -1t $DOUT_S_ROOT_LOCKED/cpl/logs/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 spinup $add_iop" >>& $TESTSTATUS_LOG
echo "" >>& $TESTSTATUS_LOG

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

#======================================================================
# (2) Test run: do the final non-spinup run in the cloned ref case
#======================================================================

cd ${CASERR1}

echo ""  >>& $TESTSTATUS_LOG
echo "doing a ${stopn2} ${STOP_OPTION} final spinup test" >>& $TESTSTATUS_LOG 
echo " short term archiving is off" >>& $TESTSTATUS_LOG 

# determine refdate for run
set RUNDIR  = `./xmlquery RUNDIR   -value`
mkdir -p $RUNDIR
set refdate = `ls -1dt ${DOUTSR0}/rest/*-00000* | head -1 | sed "s/-00000.*//" | sed "s/^.*rest\///" ` || exit -1
ln -s ${DOUTSR0}/rest/${refdate}-00000/*${refdate}*  $RUNDIR/.

# set up run
./xmlchange RUN_TYPE="hybrid"
./xmlchange GET_REFCASE=FALSE
./xmlchange RUN_REFCASE=$CASE0
./xmlchange RUN_REFDATE=${refdate}
./xmlchange STOP_N=${stopn2} 
./xmlchange DOUT_S=FALSE
./xmlchange -file env_run.xml -id CLM_BLDNML_OPTS -val "$orig_clm_bldnml_opts -bgc_spinup off" || exit -1

set CASE = `./xmlquery CASE -value`

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

# since short term archiving is off, must now look in $RUNDIR for cpl log files
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

# the following line creates the component history files used in comparison to baselines
$SCRIPTSROOT/Tools/component_compare_move.sh -rundir $RUNDIR -testcase "$CASE" -suffix "base" $add_iop

#======================================================================
# Test status check:
#======================================================================

set CPLLOG = $CplLogFile

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

if ( $?IOP_ON ) then
    # note that suffix2 is none - since there is no functionality that 
    # this test performs other than running
    # by setting the suffix to none - will not be generating any comparisions 
    # other than the iop comparison for the base case
    $SCRIPTSROOT/Tools/component_compare_test.sh -rundir $RUNDIR -testcase $CASE -testcase_base $CASEBASEID -suffix1 base -suffix2 none $add_iop -msg "$msg" >>& $TESTSTATUS_OUT
endif


