#!/bin/bash --posix
#----------------------------------------------------------------------
#
# Lightweight bash shell script to execute CESM testlists for testing CLM.
#
# This script is intended to operate similar to the previous test_driver.sh script.
#
#----------------------------------------------------------------------
# Usage subroutine
usage() {
  echo ""
  echo "**********************"
  echo "usage:"
  echo "./test_system <arguments>"
  echo ""
  echo "valid arguments: "
  echo "  -b directory [or --baseline]  baseline directory"
  echo "  -c version   [or --compare]   version to compare to (generate must already have been run to create these)"
  echo "  -d                            debug usage -- display tests that will run -- but do NOT actually execute them"
  echo "  -g version   [or --generate]  name of this version to generate version as"
  echo "  -h           [or --help]      displays help"
  echo "  -i                            interactive usage"
  echo "  -l list      [or --list]      input test list to use instead of default (path relative to this directory)"
  echo "  -o options   [or --options]   options to pass to create_test_suite"
  echo "  -p compiler  [or --compiler]  compiler to use instead of default"
  echo "  -s           [or --shortlist] use the short test list"
  echo "**********************"
}

testdir=`pwd`
cesmscriptdir="../../../../../scripts/"


#----------------------------------------------------------------------
# Process input arguments
#----------------------------------------------------------------------

interactive="no"
debug="no"
options=""

while [ $# -gt 0 ]; do
   case $1 in
       -i)
           interactive="YES"
           options="$options -nobatch on"
           ;;
        -b|--baseline )
            baselineroot="$2"
            shift
            ;;
       -d)
           debug="YES"
           options="$options -debug"
           ;;
       -g|--generate )
           options="$options -generate $2"
           shift
           ;;
       -c|--compare )
           options="$options -compare $2"
           shift
           ;;
       -p|--compiler )
           compiler="$2"
           shift
           ;;
       -l|--list )
           if [ ! -z "$inter_list" ]; then
              echo "ERROR:: can NOT set both -s and -l options"
              usage
              exit 1
           fi
           inter_list="$2"
           batch_list="$2"
           shift
           ;;
       -s|--shortlist )
           if [ ! -z "$inter_list" ]; then
              echo "ERROR:: can NOT set both -s and -l options"
              usage
              exit 1
           fi
           inter_list="shortlist.interactive"
           batch_list="shortlist.batch"
           ;;
       -o|--options )
           options="$options $2"
           shift
           ;;
       -h|--help )
           usage
           exit 0
           ;;
       * )
           echo "ERROR:: invalid argument sent in: $2"
           usage
           exit 1
           ;;
   esac
   shift
done

#will attach timestamp onto end of script name to prevent overwriting
cur_time=`date '+%H:%M:%S'`

#----------------------------------------------------------------------
# Figure out what test list to use for this machine
#----------------------------------------------------------------------

hostname=`hostname`
case $hostname in

    ##yellowstone
    ys* )
       machine=yellowstone
       defaultbaselineroot="$CESMDATAROOT/clm_cesm_baselines"
       options="$options -mach $machine"
       defaultcompiler="intel"
       defaultinter_testlist="yellowstone.interactive"
       defaultbatch_testlist="yellowstone.batch"
    ;;

    ##bluefire
    be* )
       machine=bluefire
       defaultbaselineroot="/glade/proj2/cgd/tss/clm_cesm_baselines"
       options="$options -mach $machine"
       defaultcompiler="ibm"
       defaultinter_testlist="bluefire.interactive"
       defaultbatch_testlist="bluefire.batch"
    ;;

    ##mirage
    mirage* )
       machine=userdefined
       options="$options -mach $machine"
       defaultbaselineroot="/glade/proj2/cgd/tss/clm_cesm_baselines"
       defaultcompiler="intel"
       defaultinter_testlist="mirage.interactive"
       if [ "$interactive" = "no" ]; then
          echo "ERROR:: mirage can only be run with interactive mode"
          exit 1
       fi
    ;;

    ## lynx
    lynx* | l0*)
       machine=lynx
       defaultbaselineroot="/glade/proj2/cgd/tss/clm_cesm_baselines"
       options="$options -mach $machine"
       defaultcompiler="pgi"
       defaultinter_testlist="lynx.interactive"
       defaultbatch_testlist="lynx.batch"
    ;;

    ## Default
    * )
       echo "WARNING:: unknown machine host to run on: $hostname"
       if [   "$inter_list"  = "" ]; then
          echo "ERROR:: list option MUST be provided for an unknown machine"
          exit 1
       fi
       ;;
esac
#
# Check values and settings
#
if [   -z "$compiler" ]; then
   if [ ! -z "$defaultcompiler" ]; then
      compiler=$defaultcompiler
   else
      compiler="pgi"
   fi
fi
if [ ! -z "$inter_list" ]; then
   defaultinter_testlist="$inter_list"
   defaultbatch_testlist="$batch_list"
fi
testlid=`date +%H%M%S`
if [ "$interactive" = "YES" ]; then
   testlist="$testdir/$defaultinter_testlist"
else
   testlist="$testdir/$defaultbatch_testlist"
fi
if [ ! -f "$testlist" ]; then
   echo "ERROR:: input test list does NOT exist: $testlist"
   exit 1
fi
options="$options -testid $testlid"
#
# Change to scripts directory and run
#
echo "change directory to: $cesmscriptdir"
cd $cesmscriptdir
options="$options -compiler $compiler"
if [ ! -z "$baselineroot" ]; then
   options="$options -baselineroot $baselineroot"
elif [ ! -z "$defaultbaselineroot" ]; then
   options="$options -baselineroot $defaultbaselineroot"
fi
echo "Test list to use:               $testlist"
echo "Create test suite with options: $options"
./create_test_suite -input_list $testlist -compset_file $testdir/config_files/config_CLMtestCompsets.xml $options
if [ "$interactive" = "YES" ]; then
  ./cs.status.$testlid.$machine | tee status.$testlid.log
  echo "Expected fail results"
  num_tests=`wc -w < $testlist` 
  ../../bld/unit_testers/xFail/wrapClmTests.pl -statusFile status.$testlid.log -numberOfTests "$num_tests" -callingScript "test_driver.sh-i"
fi
echo "status/submit scripts are in the directory: $cesmscriptdir"
echo "submit jobs with:   cs.submit.$testlid.$machine"
echo "check results with: cs.status.$testlid.$machine"

