
#======================================================================
# Create instance modification
#======================================================================

cd $CASEROOT

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

# Reset user_nl_files from previous runs
if (-e user_nl_cice_0001.base) then
  cp -f user_nl_cice_0001.base user_nl_cice_0001
endif
if (-e user_nl_cice_0002.base) then
  cp -f user_nl_cice_0002.base user_nl_cice_0002
endif

cp -f user_nl_cice_0001 user_nl_cice_0001.base
cp -f user_nl_cice_0002 user_nl_cice_0002.base

cp -f user_nl_cice_0001 user_nl_cice_0001.mod
cp -f user_nl_cice_0002 user_nl_cice_0002.mod

# make user_nl_cice_0001.mod and user_nl_cice_0002.mod different from
# user_nl_cice_0001.base and user_nl_cice_0002.base

#cat >> user_nl_cice_0001.mod <<EOF
#ndte = 121
#EOF
#cat >> user_nl_cice_0002.mod <<EOF
#ndte = 121
#EOF

#======================================================================
# do an initial run test with NINST 2, with the 
# user_nl_cice_0001 and user_nl_cice_0002 different - and the mod to 
# instance 1 
#======================================================================

cd $CASEROOT
echo "doing an initial test with mod to instance 1, 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
./xmlchange -file env_run.xml -id HIST_OPTION  -val ${STOP_OPTION}
./xmlchange -file env_run.xml -id HIST_N       -val ${STOP_N}

# Note that user_nl_cice_0001 and user_nl_cice_0002 are different below
rm user_nl_cice_0001
rm user_nl_cice_0002
cp -f user_nl_cice_0001.mod  user_nl_cice_0001
cp -f user_nl_cice_0002.base user_nl_cice_0002

./$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 "moving relevant history files to suffix with command " >>& $TESTSTATUS_LOG
echo "$SCRIPTSROOT/Tools/component_compare_move.sh -rundir $RUNDIR -testcase $CASE -suffix base" >>& $TESTSTATUS_LOG
echo "" >>& $TESTSTATUS_LOG

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

#======================================================================
# do an initial run test with NINST 2, with the 
# user_nl_cice_0001 and user_nl_cice_0002 different - and the mod to 
# instance 2 
#======================================================================

cd $CASEROOT
echo "doing a second test with mod to instance 2" >>& $TESTSTATUS_LOG 

rm user_nl_cice_0001
rm user_nl_cice_0002
cp -f user_nl_cice_0001.base user_nl_cice_0001
cp -f user_nl_cice_0002.mod  user_nl_cice_0002

./$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 "moving relevant history files to suffix with commands " >>& $TESTSTATUS_LOG
echo "$SCRIPTSROOT/Tools/component_compare_move.sh -rundir $RUNDIR -testcase $CASE -suffix inst2mod"  >>& $TESTSTATUS_LOG 
echo "" >>& $TESTSTATUS_LOG

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

#======================================================================
# Check test status for all relevant component history files
#======================================================================

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

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

$SCRIPTSROOT/Tools/component_compare_test.sh -rundir $RUNDIR -testcase $CASE -testcase_base $CASEBASEID -suffix1 "base" -suffix2 "multiinst" -msg "compare .base with .inst2mod" >>& $TESTSTATUS_OUT



