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

# 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

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

if (${STOP_N} < 3) then
  echo "ERROR in ${0}:  stop_n too short" >>& $TESTSTATUS_LOG
  echo "TFAIL ${CASE} " >>& $TESTSTATUS_OUT
  exit -1
endif

@ stopn1 = ${STOP_N}
@ restn1 = ${stopn1} / 2 + 1
@ histn  = ${stopn1}

#======================================================================
# do an initial run test
#======================================================================

cd $CASEROOT

echo "" >>& $TESTSTATUS_LOG 
echo "doing a ${stopn1} ${STOP_OPTION} initial test writing a restart at ${stopn1} ${STOP_OPTION}" >>& $TESTSTATUS_LOG

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

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

set CplLogFile = `ls -1t $DOUT_S_ROOT/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

#======================================================================
# do a restart run
#======================================================================

cd $CASEROOT
echo "doing a ${stopn1} ${STOP_OPTION} restart test" >>& $TESTSTATUS_LOG 

./xmlchange -file env_run.xml -id CONTINUE_RUN -val TRUE
./xmlchange -file env_run.xml -id STOP_N       -val ${stopn1}
./xmlchange -file env_run.xml -id REST_OPTION  -val ${STOP_OPTION}
./xmlchange -file env_run.xml -id HIST_N       -val ${histn}

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

set CplLogFile = `ls -1t $DOUT_S_ROOT/cpl/logs/cpl.log* | head -1`
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
else
    echo "ERROR: no coupler log created, model run failed" >>& $TESTSTATUS_LOG
    exit -1
endif

set NINST_ATM   = `./xmlquery NINST_ATM   -value`

if ($NINST_ATM == 1) then
  $SCRIPTSROOT/Tools/component_compare_move.sh -rundir $RUNDIR -testcase $CASE -suffix "base"
endif
if ($NINST_ATM == 2) then
  $SCRIPTSROOT/Tools/component_compare_move.sh -rundir $RUNDIR -testcase $CASE -suffix "base"  -model_inst 0001
  $SCRIPTSROOT/Tools/component_compare_move.sh -rundir $RUNDIR -testcase $CASE -suffix "inst2" -model_inst 0002
endif

#======================================================================
# do a second restart run, using the first restart set generated
#======================================================================

cd $CASEROOT
echo "doing a ${stopn1} ${STOP_OPTION} second restart test, using the first restart set copied from" >>& $TESTSTATUS_LOG
echo "the short-term archive directory" >>& $TESTSTATUS_LOG

./xmlchange -file env_run.xml -id CONTINUE_RUN -val TRUE
./xmlchange -file env_run.xml -id STOP_N       -val ${stopn1}
./xmlchange -file env_run.xml -id REST_OPTION  -val never
./xmlchange -file env_run.xml -id HIST_N       -val ${histn}

# Remove the current restart files, and copy the first set of restart files back to the rundir..
rm -rf $RUNDIR/${CASE}*.r*
rm -rf $RUNDIR/rpointer*

# Copy the first set of restart files back to the rundir..
set FirstRestSet = `ls -1v $DOUT_S_ROOT/rest/ | head -1`
echo "Restart set dir : $FirstRestSet"                   >>& $TESTSTATUS_LOG
echo "Restart set path: $DOUT_S_ROOT/rest/$FirstRestSet" >>& $TESTSTATUS_LOG

cp -Rp $DOUT_S_ROOT/rest/$FirstRestSet/* $RUNDIR/

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

set CplLogFile = `ls -1t $DOUT_S_ROOT/cpl/logs/cpl.log* | head -1`
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
else
    echo "ERROR: no coupler log created, model run failed" >>& $TESTSTATUS_LOG
    exit -1
endif

set NINST_ATM  = `./xmlquery NINST_ATM   -value`

if ($NINST_ATM == 1) then
  $SCRIPTSROOT/Tools/component_compare_move.sh -rundir $RUNDIR -testcase $CASE -suffix "rest_sta"
endif
if ($NINST_ATM == 2) then
  $SCRIPTSROOT/Tools/component_compare_move.sh -rundir $RUNDIR -testcase $CASE -suffix "rest_sta"       -model_inst 0001
  $SCRIPTSROOT/Tools/component_compare_move.sh -rundir $RUNDIR -testcase $CASE -suffix "rest_sta_inst2" -model_inst 0002
endif

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

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

if ($NINST_ATM == 1) then
  $SCRIPTSROOT/Tools/component_compare_test.sh -rundir $RUNDIR -testcase $CASE -testcase_base $CASEBASEID -suffix1 base -suffix2 rest_sta -msg "sta test .base and .rest_sta files" >>& $TESTSTATUS_OUT
endif
if ($NINST_ATM == 2) then
  $SCRIPTSROOT/Tools/component_compare_test.sh -rundir $RUNDIR -testcase $CASE -testcase_base $CASEBASEID -suffix1 base -suffix2 rest_sta       -msg "sta test .base and .rest_sta       files" >>& $TESTSTATUS_OUT
  $SCRIPTSROOT/Tools/component_compare_test.sh -rundir $RUNDIR -testcase $CASE -testcase_base $CASEBASEID -suffix1 base -suffix2 rest_sta_inst2 -msg "sta test .base and .rest_sta_inst2 files" >>& $TESTSTATUS_OUT
endif
