
#======================================================================
# Test setup:
#======================================================================

cd $CASEROOT

touch cice_perf.out

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

cat >! user_nl_cice.mhT <<EOF
maskhalo_bound = .true.
maskhalo_dyn = .true.
maskhalo_remap = .true.
maskhalo_stress = .true.
EOF

cat >! user_nl_cice.mhF <<EOF
maskhalo_bound = .false.
maskhalo_dyn = .false.
maskhalo_remap = .false.
maskhalo_stress = .false.
EOF

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

set cntmax = 1000
set cnt = 0
set found = 1

echo " start..... " >> cice_perf.out
while ($cnt < $cntmax && $found == 1)
  @ cnt = $cnt + 1
  set acnt = $cnt
  if ($cnt < 1000) set acnt = "0${cnt}"
  if ($cnt < 100 ) set acnt = "00${cnt}"
  if ($cnt < 10  ) set acnt = "000${cnt}"

  if (-e env_build.xml.${acnt}) then
    cp -f env_build.xml.${acnt} env_build.xml
    cp -f env_build.xml LockedFiles/env_build.xml.locked
    cp -f $EXEROOT/cesm.exe.${acnt} $EXEROOT/cesm.exe

    foreach maskhalo (T F)
      cd $CASEROOT
      cp -f user_nl_cice.mh${maskhalo} user_nl_cice

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

      set ICE_GRID		= `./xmlquery ICE_GRID			-value`
      set CICE_BLCKX		= `./xmlquery CICE_BLCKX		-value`
      set CICE_BLCKY		= `./xmlquery CICE_BLCKY		-value`
      set CICE_MXBLCKS	        = `./xmlquery CICE_MXBLCKS		-value`
      set CICE_DECOMPTYPE	= `./xmlquery CICE_DECOMPTYPE		-value `  
      set CICE_DECOMPSETTING	= `./xmlquery CICE_DECOMPSETTING	-value `  
      set NTASKS_ICE		= `./xmlquery NTASKS_ICE		-value`
      set NTHRDS_ICE		= `./xmlquery NTHRDS_ICE		-value`

      set LID = `ls -1t $RUNDIR/cpl.log* | head -1 | sed 's/.*cpl.log.//' | sed 's/.gz//' `
      @ ice_pes = ${NTASKS_ICE} * ${NTHRDS_ICE}
      if ($LID != "") then
         set ice_time = `grep -i "ice run time" timing/*$LID | cut -c 40-70`
         echo "$LID $ICE_GRID ${ice_pes} $NTASKS_ICE $NTHRDS_ICE $CICE_BLCKX $CICE_BLCKY $CICE_MXBLCKS $CICE_DECOMPTYPE $CICE_DECOMPSETTING $maskhalo ${ice_time}" >> cice_perf.out
         mv ${RUNDIR} ${RUNDIR}.${LID}
      else
         echo " error..... $ICE_GRID ${ice_pes} $NTASKS_ICE $NTHRDS_ICE $CICE_BLCKX $CICE_BLCKY $CICE_MXBLCKS $CICE_DECOMPTYPE $CICE_DECOMPSETTING $maskhalo " >> cice_perf.out
         mv ${RUNDIR} ${RUNDIR}.error
      endif
    end # maskhalo
  else
    set found = 0
  endif
end  # while
echo "  done..... " >> cice_perf.out

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

#======================================================================
# Test status check:
#======================================================================

set CPLLOG = $CplLogFile

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