
#======================================================================
# Clear coupler log files for comparison tests
#======================================================================
unsetenv CPLLOG_INIT
unsetenv CPLLOG_REST

#======================================================================
# do a 2 month initial run test
#======================================================================
cd $CASEROOT
echo "doing a 2 month initial test" >>& $TESTSTATUS_LOG 

source ./Tools/ccsm_getenv || exit -1

# The following assumes that the start month is 01 

set start_date = $RUN_STARTDATE
set start_year = `echo $start_date | cut -f 1 -d "-"`
set rest_date  = ${start_year}0201 
set hist_date  = ${start_year}0227 
 
./xmlchange -file env_run.xml -id REST_OPTION  -val date
./xmlchange -file env_run.xml -id REST_DATE    -val $rest_date
./xmlchange -file env_run.xml -id HIST_DATE    -val $hist_date

./xmlchange -file env_run.xml -id CONTINUE_RUN -val FALSE
./xmlchange -file env_run.xml -id STOP_OPTION  -val nmonths
./xmlchange -file env_run.xml -id STOP_N       -val 2

./$CASE.run

gunzip $RUNDIR/cpl.log*.gz
set CplLogFile = `ls -1t $RUNDIR/cpl.log* | head -1`
set cplhist_init = `ls -1t $RUNDIR/*.cpl.hi.*.nc | head -1`
mv ${cplhist_init} ${cplhist_init}.base
set cplhist_init = ${cplhist_init}.base
setenv CPLLOG_INIT $CplLogFile
set cplprof_init = `ls -1t $CASEROOT/timing/ccsm_timing.$CASE* | head -1`
setenv CPLPROF_GENCMP $cplprof_init

#======================================================================
# do a 1 month restart run
#======================================================================
cd $CASEROOT
echo "doing a 1 month restart run" >>& $TESTSTATUS_LOG 

./xmlchange -file env_run.xml -id CONTINUE_RUN -val TRUE
./xmlchange -file env_run.xml -id REST_OPTION  -val never
./xmlchange -file env_run.xml -id STOP_OPTION  -val nmonths
./xmlchange -file env_run.xml -id STOP_N       -val 1

./$CASE.run

gunzip $RUNDIR/cpl.log*.gz
set CplLogFile = `ls -1t $RUNDIR/cpl.log* | head -1`
set cplhist_rest = `ls -1t $RUNDIR/*.cpl.hi.*.nc | head -1`
setenv CPLLOG_REST $CplLogFile

#======================================================================
# runs complete
#======================================================================
./xmlchange -file env_run.xml -id CONTINUE_RUN -val FALSE

setenv CPLLOG_GENCMP $CPLLOG_REST
echo "Initial Test hist is $cplhist_init" >>& $TESTSTATUS_LOG 
echo "Restart Test hist is $cplhist_rest" >>& $TESTSTATUS_LOG 

#======================================================================
# Check case validation first
#======================================================================

set basestatus = "UNDEF"

if ( $?cplhist_init ) then
  if ( $?cplhist_rest ) then
    echo "Comparing initial hist file with second hist file" >>& $TESTSTATUS_LOG
    cd $RUNDIR
    $CASETOOLS/hist_compare.csh $cplhist_init $cplhist_rest >>& $TESTSTATUS_LOG
    cd $CASEROOT
    set pass = `tail -1 $TESTSTATUS_LOG | grep PASS | wc -l`
    if ( $pass != 0 ) then
      set basestatus = "PASS "
    else
      set basestatus = "FAIL "
    endif
  else
    echo "ERROR in ${0}:  restart coupler hist file NOT set" >>& $TESTSTATUS_LOG
    set basestatus = "ERROR"
  endif
else
  echo "ERROR in ${0}:  initial coupler hist file NOT set" >>& $TESTSTATUS_LOG
  set basestatus = "ERROR"
endif

echo "$basestatus ${CASEBASEID} " >&! $TESTSTATUS_OUT

