
#======================================================================
# Clear coupler log files for comparison tests
#======================================================================

set OS   = `./xmlquery OS    -value`
set CASE = `./xmlquery CASE  -value`

#--- This only works on AIX systems
if ($OS != AIX) then
  echo "${0}:  running APT test on AIX system - Error" >>& $TESTSTATUS_LOG
  echo "ERROR: APT test must be run on AIX system" >>& $TESTSTATUS_LOG
  set basestatus = "ERROR"
  echo "$basestatus ${CASEBASEID} " >>& $TESTSTATUS_OUT
  exit -1
else
  echo "${0}:  running APT test on AIX system - OK" >>& $TESTSTATUS_LOG
endif

#======================================================================
# do a base run, with length taken from compset default
#======================================================================

cd $CASEROOT

echo "doing a base run with length taken from compset default" >>& $TESTSTATUS_LOG 

./xmlchange -file env_run.xml -id CONTINUE_RUN -val FALSE
./xmlchange -file env_run.xml -id REST_OPTION  -val none

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

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

#--- set apt case name, remember main case name and exeroot
set CASER0 = ${CASEROOT}
set CASERR = ${CASEROOT}.apt
set EXEROOT0 = ${EXEROOT}

#======================================================================
# clone the case and running there
#======================================================================

echo "doing an autopromotion case" >>& $TESTSTATUS_LOG 

#--- clone the main case to create apt case
cd $CIMEROOT/scripts
./create_clone -case ${CASERR} -clone ${CASER0} 

#======================================================================
# do an autopromotion case
#======================================================================

cd ${CASERR}

set CASE = `./xmlquery CASE  -value`

./cesm_setup -clean -testmode
./cesm_setup

./xmlchange -file env_build.xml -id BUILD_COMPLETE -val TRUE

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

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 apt $add_iop" >>& $TESTSTATUS_LOG
echo "" >>& $TESTSTATUS_LOG

# the following creates the restart file used in the restart test
$SCRIPTSROOT/Tools/component_compare_move.sh -rundir $RUNDIR -testcase $CASE -suffix "rest"

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

set CPLLOG = $CplLogFile

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

echo "obtaining test functionality from history files with command " >>& $TESTSTATUS_LOG
echo "$SCRIPTSROOT/Tools/component_compare_test.sh -rundir $RUNDIR -testcase $CASE -testcase_base $CASEBASEID -suffix1 base -suffix2 apt $add_iop -msg $msg">>& $TESTSTATUS_LOG
echo "" >>& $TESTSTATUS_LOG

$SCRIPTSROOT/Tools/component_compare_test.sh -rundir $RUNDIR -testcase $CASE -testcase_base $CASEBASEID -suffix1 base -suffix2 apt $add_iop -msg "$msg" >>& $TESTSTATUS_OUT


