#======================================================================
# All things common to all tests that need to be done at the beginning 
# of each test should be put here to avoid duplication.  
#======================================================================

#======================================================================
# Test status files
#======================================================================
# test logs

setenv TESTSTATUS_OUT   $CASEROOT/TestStatus
setenv TESTSTATUS_LOG   $CASEROOT/TestStatus.out
setenv TESTSTATUS_OUT_NLCOMP $CASEROOT/TestStatus.nlcomp
if( "$COMPARE_BASELINE" == "TRUE" ) then
  setenv BASECMP_CPLLOGFILE  $BASECMP_DIR/cpl.log
  setenv COMPARE_NAMELISTS   TRUE
endif
if( "$GENERATE_BASELINE" == "TRUE" ) then
  setenv BASEGEN_CPLLOGFILE  $BASEGEN_DIR/cpl.log
endif



# valid test output states are:  
#   PASS      Test passed
#   FAIL      Test failed
#   BFAIL     Base Results do not exist
#   GEN       Case generated
#   ERROR     Error underfined, test may or may not have passed
#   UNDEF     Undefined
#   BUILT     Build completed                           NOT YET IMPLEMENTED
#   PEND      Test submitted
#   RUN       Test started running, it may or may not have completed

#======================================================================
# (Don't) Remove test status files! create_test puts the namelist comparision 
# status and output into these files, respectively.  
#======================================================================
#
set basestatus = "RUN  "
echo "$basestatus ${CASE} " >&! $TESTSTATUS_OUT
touch $TESTSTATUS_LOG

set sdate = `date +"%Y-%m-%d %H:%M:%S"`
echo "test started $sdate" >>& CaseStatus

#======================================================================
# IOP test, run baseline
#======================================================================

if ( ${CASE} =~ *_IOP*) then
  set iopbase_argv = `echo ${TEST_ARGV} |  sed 's/_IOP[A-Z4cp]*//'`
  set iopbase_case = `echo ${CASE} |  sed 's/_IOP[A-Z4cp]*//'`
  set iopflags = `echo $CASE | sed 's/^.*_IOP\([A-Z4cp]*\).*/\1/'`
  echo "running create_test ${iopbase_argv}  -clean off -testid ${TEST_TESTID}" >>& $TESTSTATUS_LOG
  echo "iopflags = ${iopflags}" >>& $TESTSTATUS_LOG

  #--- run IOP base test ---
  set caseroot0 = ${CASEROOT}
  cd $SCRIPTSROOT
  ./create_test ${iopbase_argv} -clean off -testid ${TEST_TESTID}
  cd $CASEROOT  || exit -9
  ./xmlchange  HIST_OPTION='$STOP_OPTION'
  ./xmlchange  HIST_N='$STOP_N'
  cd ../${iopbase_case}  || exit -9
  cp $CASEROOT/env_build.xml* .
  cp $CASEROOT/env_run.xml* .
  foreach file (env_build.xml*)
    ./xmlchange -file $file -id EXEROOT        -val ${EXEROOT}
    ./xmlchange -file $file -id BUILD_COMPLETE -val TRUE
  end
  ./*.test
  cd $caseroot0

  #--- modify this IOP case ---
  if ( ${iopflags} == '' ) then
    ./xmlchange -file env_run.xml -id PIO_TYPENAME -val 'pnetcdf'
  endif
  if ( ${iopflags} == '4p' ) then
    ./xmlchange -file env_run.xml -id PIO_TYPENAME -val 'netcdf4p'
  endif
  if ( ${iopflags} == '4c' ) then
    ./xmlchange -file env_run.xml -id PIO_TYPENAME -val 'netcdf4c'
  endif

  set pref = ( ATM CPL OCN WAV GLC ICE ROF LND )
  set mod = ( A C O W G I R L )
  set i = 0
  while ( $i < 8 ) 
    @ i ++
    if ( ${iopflags} =~ *$mod[$i]4p*) then
      ./xmlchange -file env_run.xml -id $pref[$i]_PIO_TYPENAME -val 'netcdf4p'
    else if ( ${iopflags} =~ *$mod[$i]4c* ) then
      ./xmlchange -file env_run.xml -id $pref[$i]_PIO_TYPENAME -val 'netcdf4c'
    else if ( ${iopflags} =~ *$mod[$i]* ) then
      ./xmlchange -file env_run.xml -id $pref[$i]_PIO_TYPENAME -val 'pnetcdf'
    endif
  end





endif

