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

cd $CASEROOT

touch pop_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 POP_AUTO_DECOMP  -val false

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

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

  echo " start..... " >> pop_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

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

      set OCN_GRID	 = `./xmlquery OCN_GRID	      -value`
      set POP_BLCKX	 = `./xmlquery POP_BLCKX      -value`
      set POP_BLCKY	 = `./xmlquery POP_BLCKY      -value`
      set POP_MXBLCKS	 = `./xmlquery POP_MXBLCKS    -value`
      set POP_DECOMPTYPE = `./xmlquery POP_DECOMPTYPE -value`  
      set NTASKS_OCN	 = `./xmlquery NTASKS_OCN     -value`
      set NTHRDS_OCN	 = `./xmlquery NTHRDS_OCN     -value`

      set LID = `ls -1t $RUNDIR/cpl.log* | head -1 | sed 's/.*cpl.log.//' | sed 's/.gz//' `
      @ ocn_pes = ${NTASKS_OCN} * ${NTHRDS_OCN}
      if ($LID != "") then
         set ocn_time = `grep -i "ocn run time" timing/*$LID | cut -c 40-70`
         echo "$LID $OCN_GRID ${ocn_pes} $NTASKS_OCN $NTHRDS_OCN $POP_BLCKX $POP_BLCKY $POP_MXBLCKS $POP_DECOMPTYPE ${ocn_time}" >> pop_perf.out
         mv ${RUNDIR} ${RUNDIR}.${LID}
      else
         echo " error..... $OCN_GRID ${ocn_pes} $NTASKS_OCN $NTHRDS_OCN $POP_BLCKX $POP_BLCKY $POP_MXBLCKS $POP_DECOMPTYPE " >> pop_perf.out
         mv ${RUNDIR} ${RUNDIR}.error
      endif
    else
      set found = 0
    endif
  end  # while
  echo "  done..... " >> pop_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

