#!/bin/sh
# initialise user dependent variable
cmd=$0 ; cmdargs=$@
SETTE_DIR=$(cd $(dirname "$0"); pwd)
MAIN_DIR=$(dirname $SETTE_DIR)
export CMPL_CORES=8            # Number of threads to use for compiling
dry_run=0
#
# controls for some common namelist, run-time options:
#
export USING_TIMING='yes'      # Default: yes => set ln_timing=.true.   ; use -T to disable
export USING_ICEBERGS='yes'    # Default: yes => set ln_icebergs=.true. ; use -i to disable
export USING_EXTRA_HALO='yes'  # Default: yes => set nn_hls=2           ; use -e to set nn_hls=1
export USING_COLLECTIVES='yes' # Default: yes => set nn_comm=2          ; use -C to set nn_comm=1
export USING_TILING='yes'      # Default: yes => set ln_tile=.true.     ; use -t to disable
                               #    Note: yes also ensures nn_hls=2 but -t will not alter nn_hls 
#
# controls for some common compile-time keys:
#
export USING_NOSIGNED0='yes'   # Default: yes => add key_nosignedzero   ; use -z to delete key_nosignedzero
export USING_QCO='yes'         # Default: yes => add key_qco            ; use -q to delete key_qco
export USING_RK3='no'          # Default: yes => add key_RK3 & key_qco  ; use -Q to delete key_RK3
export USING_LOOP_FUSION='yes' # Default: yes => add key_loop_fusion    ; use -F to delete key_loop_fusion
export USING_XIOS='yes'        # Default: yes => add key_xios           ; use -X to delete key_xios
                               #    Note: changing USING_XIOS may require a change in arch file
#
# controls for some common batch-script, run-time options:
#
export USING_MPMD='yes'        # Default: yes => run with detached XIOS servers ; use -A to run in attached (SPMD) mode
                               #    Note: yes also ensures key_xios but -A will not remove it
export SETTE_SUB_VAL=""

# Parse command-line arguments
if [ $# -gt 0 ]; then
  while getopts n:x:v:cdshTzqQteiACFX option; do 
     case $option in
        c) export SETTE_CLEAN_CONFIGS='yes'
           export SETTE_SYNC_CONFIGS='yes'
           echo "-c: Configuration $SETTE_TEST_CONFIGS will be cleaned; this option enforces also synchronisation"
           echo "";;
        d) dry_run=1
           echo "";;
        s) export SETTE_SYNC_CONFIGS='yes'
           echo "-s: MY_SRC and EXP00 in $SETTE_TEST_CONFIGS will be synchronised with the MY_SRC and EXPREF from the reference configuration"
           echo "";;
        n) export SETTE_TEST_CONFIGS=($OPTARG)
           echo "=================================="
           if [ ${#SETTE_TEST_CONFIGS[@]} -gt 1 ]; then
             echo "-n: Configurations ${SETTE_TEST_CONFIGS[@]} will be tested if they are available"
           else
             echo "-n: Configuration ${SETTE_TEST_CONFIGS[@]} will be tested if it is available"
           fi
           echo "";;
        x) export SETTE_TEST_TYPES=($OPTARG)
           echo "-x: $SETTE_TEST_TYPES tests requested"
           echo "";;
        v) export SETTE_SUB_VAL=($OPTARG)
           echo "-v: $SETTE_SUB_VAL validation sub-directory requested"
           echo "";;
        T) export USING_TIMING='no'
           echo "-T: ln_timing will be set to false"
           echo "";;
        t) export USING_TILING='no'
           echo "-t: ln_tile will be set to false"
           echo "";;
        e) export USING_EXTRA_HALO='no'
           echo "-e: nn_hls will be set to 1"
           echo "";;
        i) export USING_ICEBERGS='no'
           echo "-i: ln_icebergs will be set to false"
           echo "";;
        C) export USING_COLLECTIVES='no'
           echo "-C: nn_comm will be set to 1"
           echo "";;
        z) export USING_NOSIGNED0='no'
           echo "-z: key_nosignedzero will NOT be activated"
           echo "";;
        q) export USING_QCO='no'
           echo "-q: key_qco and key_linssh will NOT be activated"
           echo "";;
        Q) export USING_RK3='no'
           echo "-Q: key_qco and key_RK3 will not be activated"
           echo "    This is the curent default for now since RK3 is not ready"
           echo "";;
        F) export USING_LOOP_FUSION='no'
           echo "-F: key_loop_fusion will not be activated"
           echo "";;
        X) export USING_XIOS='no'
           echo "-X: key_xios will not be activated"
           echo "";;
        A) export USING_MPMD='no'
           echo "-A: Tasks will be run in attached (SPMD) mode"
           echo "";;
        h | *) echo 'sette.sh with no arguments (in this case all configuration will be tested with default options)'
               echo '-T to set ln_timing false for all non-AGRIF configurations (default: true)'
               echo '-t set ln_tile false in all tests that support it (default: true)'
               echo '-e set nn_hls=1 (default: nn_hls=2)'
               echo '-i set ln_icebergs false (default: true)'
               echo '-C set nn_comm=1 (default: nn_comm=2 ==> use MPI3 collective comms)'
               echo '-z to remove the key_nosignedzero key (default: added)'
               echo '-q to remove the key_qco key (default: added)'
               echo '-X to remove the key_xios key (default: added)'
               echo '-F to remove the key_loop_fusion key (default: added)'
               echo '-Q to remove the key_RK3 key (currently a null-op since key_RK3 is not used)'
               echo '-A to run tests in attached (SPMD) mode (default: MPMD with key_xios)'
               echo '-n "CFG1_to_test CFG2_to_test ..." to test some specific configurations'
               echo '-x "TEST_type TEST_type ..." to specify particular types of test (RESTART is mandatory)'
               echo '-v "subdir" optional validation record subdirectory to be created below NEMO_VALIDATION_DIR'
               echo '-d to perform a dryrun to simply report what settings will be used'
               echo '-c to clean each configuration'
               echo '-s to synchronise the sette MY_SRC and EXP00 with the reference MY_SRC and EXPREF'; exit 42 ;;
     esac
  done
  shift $((OPTIND - 1))
fi
#
# Option dependency tests
#
if [ ${USING_TILING} == "yes" ] ; then export USING_EXTRA_HALO="yes" ; fi

#
# Get SETTE parameters
. ./param.cfg

#
# Set the common compile keys to add or delete based on command-line arguments:
#
export ADD_KEYS="" ; export DEL_KEYS=""
if [ ${USING_XIOS} == "yes" ] ; then export ADD_KEYS="${ADD_KEYS}key_xios " ; fi
if [ ${USING_XIOS} == "no" ]  ; then export DEL_KEYS="${DEL_KEYS}key_xios " ; fi
#
if [ ${USING_NOSIGNED0} == "yes" ] ; then export ADD_KEYS="${ADD_KEYS}key_nosignedzero " ; fi
if [ ${USING_NOSIGNED0} == "no" ]  ; then export DEL_KEYS="${DEL_KEYS}key_nosignedzero " ; fi
#
if [ ${USING_LOOP_FUSION} == "yes" ] ; then export ADD_KEYS="${ADD_KEYS}key_loop_fusion " ; fi
if [ ${USING_LOOP_FUSION} == "no" ]  ; then export DEL_KEYS="${DEL_KEYS}key_loop_fusion " ; fi
#
if [ ${USING_QCO} == "yes" ] ; then export ADD_KEYS="${ADD_KEYS}key_qco " ; fi
if [ ${USING_QCO} == "no" ]  ; then export DEL_KEYS="${DEL_KEYS}key_qco key_linssh " ; fi
#
if [ ${USING_RK3} == "yes" ] ; then export ADD_KEYS="${ADD_KEYS}key_qco key_RK3 " ; fi
if [ ${USING_RK3} == "no" ]  ; then export DEL_KEYS="${DEL_KEYS}key_RK3 " ; fi

#
# Set validation record sub-directories (if required)
#
if [ ! -d $NEMO_VALIDATION_DIR ] ; then
  while true; do
      read -p "$NEMO_VALIDATION_DIR does not exist. Do you wish to create it? " yn
      case $yn in
          [Yy]* ) mkdir $NEMO_VALIDATION_DIR; break;;
          [Nn]* ) exit 42;;
          * ) echo "Please answer yes or no.";;
      esac
  done
fi
if [ ! -z $SETTE_SUB_VAL ] ; then
 if [ ! -d $NEMO_VALIDATION_DIR/$SETTE_SUB_VAL ] ; then
    mkdir $NEMO_VALIDATION_DIR/$SETTE_SUB_VAL
 fi
 export NEMO_VALIDATION_DIR=$NEMO_VALIDATION_DIR/$SETTE_SUB_VAL
fi

if [ ${#SETTE_TEST_CONFIGS[@]} -eq 0 ]; then
   echo "=================================="
   echo "Configurations $TEST_CONFIGS will be tested if they are available"
fi
echo "Carrying out the following tests  : ${TEST_TYPES[@]}"
echo "requested by the command          : "$cmd $cmdargs
printf "%-33s : %s\n" USING_TIMING $USING_TIMING
printf "%-33s : %s\n" USING_ICEBERGS $USING_ICEBERGS
printf "%-33s : %s\n" USING_EXTRA_HALO $USING_EXTRA_HALO
printf "%-33s : %s\n" USING_TILING $USING_TILING
printf "%-33s : %s\n" USING_COLLECTIVES $USING_COLLECTIVES
printf "%-33s : %s\n" USING_NOSIGNED0 $USING_NOSIGNED0
printf "%-33s : %s\n" USING_QCO $USING_QCO
printf "%-33s : %s\n" USING_LOOP_FUSION $USING_LOOP_FUSION
printf "%-33s : %s\n" USING_XIOS $USING_XIOS
printf "%-33s : %s\n" USING_MPMD $USING_MPMD
printf "%-33s : %s\n" USING_RK3 $USING_RK3
printf "%-33s : %s\n" "Common compile keys to be added" "$ADD_KEYS"
printf "%-33s : %s\n" "Common compile keys to be deleted" "$DEL_KEYS"
echo "Validation records to appear under: "$NEMO_VALIDATION_DIR
echo "=================================="
echo ""
#
# Option compatibility tests
#
if [ ${USING_MPMD} == "yes" ] && [ ${USING_XIOS} == "no" ] ; then echo "Incompatible choices. MPMD mode requires the XIOS server" ; exit ; fi

if [ ${dry_run} -eq 1 ] ; then echo "dryrun only: no tests performed" ; exit ; fi

# run sette on reference configuration
. ./sette_reference-configurations.sh
if [[ $? != 0 ]]; then
   echo ""
   echo "--------------------------------------------------------------"
   echo "./sette_cfg-ref.sh didn't finish properly, need investigations"
   echo "--------------------------------------------------------------"
   echo ""
   exit 42
fi

# run sette on test cases
. ./sette_test-cases.sh
if [[ $? != 0 ]]; then
   echo ""
   echo "-----------------------------------------------------------------"
   echo "./sette_test-cases.sh didn't finish properly, need investigations"
   echo "-----------------------------------------------------------------"
   echo ""
   exit 42
fi

# run sette report
echo ""
echo "-------------------------------------------------------------"
echo "./sette_rpt.sh (script will wait all nemo_sette run are done)"
echo "-------------------------------------------------------------"
echo ""
NRUN=999
NIT=0
while [[ $NRUN -ne 0 && $nit -le 1080 ]]; do
   nit=$((nit+1))
   NRUN=$( ${BATCH_STAT} | grep ${BATCH_NAME} | wc -l ) 
   if [[ $NRUN -ne 0 ]]; then 
      printf "%-3d %s\r" $NRUN 'nemo_sette runs still in queue or running ...';
   else
      printf "%-50s\n" " "
      . ./sette_rpt.sh
      exit
   fi
   sleep 10
done
printf "\n"
echo ""
echo "Something wrong happened, it tooks more than 3 hours to run all the sette tests"
echo ""
