
#======================================================================
# Clear coupler log files for validation test
#======================================================================
unsetenv CPLLOG_INIT

#======================================================================
# Create instance modification
#======================================================================
cd $CASEROOT
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_0001 user_nl_cice_0001.mod
cp -f user_nl_cice_0002 user_nl_cice_0002.base
cp -f user_nl_cice_0002 user_nl_cice_0002.mod
cat "ndte = 121" >> user_nl_cice_0001.mod
cat "ndte = 121" >> user_nl_cice_0002.mod

#======================================================================
# do an initial run test with NINST 1, with length taken from compset default
#======================================================================
cd $CASEROOT
echo "doing an initial test with mod to instance 1, with length taken from compset default" >>& $TESTSTATUS_LOG 

source ./Tools/ccsm_getenv || exit -3
./xmlchange -file env_run.xml -id CONTINUE_RUN -val FALSE
./xmlchange -file env_run.xml -id REST_OPTION  -val none

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

gunzip $RUNDIR/cpl.log*.gz
set CplLogFile = `ls -1t $RUNDIR/cpl.log* | head -1`
setenv CPLLOG_INIT1 $CplLogFile
setenv BASEGEN_FILE01 $CplLogFile
echo "First Test log is $CPLLOG_INIT1" >>& $TESTSTATUS_LOG 

if ( $?CPLLOG_INIT1 ) then
  if ($CPLLOG_INIT1 == "") then
    echo "CPLLOG_INIT1 empty"
    set basestatus = "FAIL "
    echo "$basestatus " >>& $TESTSTATUS_LOG
    echo "$basestatus ${CASEBASEID} " >&! $TESTSTATUS_OUT
    exit
  endif
  if (! -e $CPLLOG_INIT1) then
    echo "CPLLOG_INIT1 does not exist"
    set basestatus = "FAIL "
    echo "$basestatus " >>& $TESTSTATUS_LOG
    echo "$basestatus ${CASEBASEID} " >&! $TESTSTATUS_OUT
    exit
  endif
  echo "Checking successful completion in cpl log file1" >>& $TESTSTATUS_LOG
  set pass = `grep "SUCCESSFUL TERM" $CPLLOG_INIT1 | wc -l`
  if ( $pass != 0 ) then
    set basestatus = "PASS "
    echo "$basestatus " >>& $TESTSTATUS_LOG
  else
    set basestatus = "FAIL "
    echo "$basestatus " >>& $TESTSTATUS_LOG
    echo "$basestatus ${CASEBASEID} " >&! $TESTSTATUS_OUT
    exit
  endif
else
  echo "ERROR in ${0}:  initial coupler log file1 NOT set" >>& $TESTSTATUS_LOG
  set basestatus = "ERROR"
  echo "$basestatus " >>& $TESTSTATUS_LOG
  echo "$basestatus ${CASEBASEID} " >&! $TESTSTATUS_OUT
  exit
endif

#======================================================================
# do an initial run test with mod to instance 2
# want to run on same pe counts per instance and same cpl pe count
#======================================================================
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

gunzip $RUNDIR/cpl.log*.gz
set CplLogFile = `ls -1t $RUNDIR/cpl.log* | head -1`
setenv CPLLOG_INIT2 $CplLogFile
setenv BASEGEN_FILE02 $CplLogFile
echo "Second Test log is $CPLLOG_INIT2" >>& $TESTSTATUS_LOG 

#======================================================================
# Check case validation first
#======================================================================
setenv CPLLOG_GENCMP $CPLLOG_INIT2

set basestatus = "UNDEF"

if ( $?CPLLOG_INIT2 ) then
  if ($CPLLOG_INIT1 == "") then
    echo "CPLLOG_INIT1 empty"
    set basestatus = "FAIL "
    echo "$basestatus " >>& $TESTSTATUS_LOG
    echo "$basestatus ${CASEBASEID} " >&! $TESTSTATUS_OUT
    exit
  endif
  if (! -e $CPLLOG_INIT1) then
    echo "CPLLOG_INIT1 does not exist"
    set basestatus = "FAIL "
    echo "$basestatus " >>& $TESTSTATUS_LOG
    echo "$basestatus ${CASEBASEID} " >&! $TESTSTATUS_OUT
    exit
  endif
  if ($CPLLOG_INIT1 == $CPLLOG_INIT2) then
    echo "logs files are same name"
    set basestatus = "FAIL "
    echo "$basestatus " >>& $TESTSTATUS_LOG
    echo "$basestatus ${CASEBASEID} " >&! $TESTSTATUS_OUT
    exit
  endif
  echo "Checking successful completion in cpl log file2" >>& $TESTSTATUS_LOG
  set pass = `grep "SUCCESSFUL TERM" $CPLLOG_INIT2 | wc -l`
  if ( $pass != 0 ) then
    set basestatus = "PASS "
    echo "$basestatus " >>& $TESTSTATUS_LOG
  else
    set basestatus = "FAIL "
    echo "$basestatus " >>& $TESTSTATUS_LOG
    echo "$basestatus ${CASEBASEID} " >&! $TESTSTATUS_OUT
    exit
  endif
else
  echo "ERROR in ${0}:  initial coupler log file2 NOT set" >>& $TESTSTATUS_LOG
  set basestatus = "ERROR"
  echo "$basestatus " >>& $TESTSTATUS_LOG
  echo "$basestatus ${CASEBASEID} " >&! $TESTSTATUS_OUT
  exit
endif

# Compare the log files

echo "diff log files " >>& $TESTSTATUS_LOG
set basestatus = "PASS "

set cfile11 = $RUNDIR/commdiag11
set cfile12 = $RUNDIR/commdiag12
set cfile21 = $RUNDIR/commdiag21
set cfile22 = $RUNDIR/commdiag22

echo "cfile11 = $cfile11" >>& $TESTSTATUS_LOG
echo "cfile12 = $cfile12" >>& $TESTSTATUS_LOG
echo "cfile21 = $cfile21" >>& $TESTSTATUS_LOG
echo "cfile22 = $cfile22" >>& $TESTSTATUS_LOG

rm -f $cfile11
rm -f $cfile12
rm -f $cfile21
rm -f $cfile22

grep -i comm_diag $CPLLOG_INIT1 | sed 's/_0001//' | grep -iv _0002 >! $cfile11
grep -i comm_diag $CPLLOG_INIT1 | sed 's/_0002//' | grep -iv _0001 >! $cfile12
grep -i comm_diag $CPLLOG_INIT2 | sed 's/_0001//' | grep -iv _0002 >! $cfile21
grep -i comm_diag $CPLLOG_INIT2 | sed 's/_0002//' | grep -iv _0001 >! $cfile22

set pass = `diff cfile11 cfile22 | grep -i comm_diag | wc -l`
if ($pass != 0) set basestatus = "FAIL "
set pass = `diff cfile12 cfile21 | grep -i comm_diag | wc -l`
if ($pass != 0) set basestatus = "FAIL "

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

