#!/bin/sh
# initialise user dependent variable
SETTE_DIR=$(cd $(dirname "$0"); pwd)
MAIN_DIR=$(dirname $SETTE_DIR)
export SETTE_TIMING='no'
export NOT_USING_QCO='no'
export USING_RK3='no'
export USING_ICEBERGS='yes'
export USING_EXTRA_HALO='no'
export USING_TILING='no'

# Parse command-line arguments
if [ $# -gt 0 ]; then
  while getopts n:x:cshTqQtei option; do 
     case $option in
        c) export SETTE_CLEAN_CONFIGS='yes'
           export SETTE_SYNC_CONFIGS='yes'
           echo ""
           echo "Configuration $SETTE_TEST_CONFIGS will be cleaned; this option enforces also synchronisation"
           echo "";;
        s) export SETTE_SYNC_CONFIGS='yes'
           echo ""
           echo "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 "Configurations ${SETTE_TEST_CONFIGS[@]} will be tested if they are available"
           else
             echo "Configuration ${SETTE_TEST_CONFIGS[@]} will be tested if it is available"
           fi
           echo "" ;;
        x) export SETTE_TEST_TYPES=($OPTARG)
           ;;
        T) export SETTE_TIMING='yes'
           echo ""
           echo "ln_timing will be set to true"
           echo "";;
	q) export NOT_USING_QCO='yes'
           echo ""
           echo "key_qco and key_linssh will NOT be activated"
           echo "";;
	Q) export USING_RK3='no'
           echo ""
           echo "key_qco and key_RK3 will be activated"
           echo "..when they are ready; disabled for now: USING_RK3='no'"
           echo "";;
        t) export USING_TILING='yes'
           echo ""
           echo "ln_tile will be set to true AND nn_hls will be set to 2"
           echo "";;
        e) export USING_EXTRA_HALO='yes'
           echo ""
           echo "nn_hls will be set to 2"
           echo "";;
        i) export USING_ICEBERGS='no'
           echo ""
           echo "ln_icebergs will be set to false"
           echo "";;
        h | *) echo 'sette.sh with no arguments (in this case all configuration will be tested)'
               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 '-T to set ln_timing true for all non-AGRIF configurations'
               echo '-c to clean each configuration'
               echo '-q run without qco environment'
               echo '-Q run with key_qco AND key_RK3 (currently disabled)'
               echo '-t activate ln_tile in all tests that support it; also forces nn_hls=2 (default: off)'
               echo '-e activate extended halo (nn_hls=2) in all tests that support it (default: nn_hls=1)'
               echo '-i run without icebergs even in tests that support them (default: on)'
               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

. ./param.cfg

if [ ${#SETTE_TEST_CONFIGS[@]} -eq 0 ]; then
   echo ""
   echo "Configurations $TEST_CONFIGS will be tested if they are available"
   echo ""
fi
echo ""
echo "Carrying out the following tests: ${TEST_TYPES[@]}"
echo ""

# 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 ""
