#!/bin/bash
set -o posix
#########################################################################################
#
# LOAD param value
. ./param.cfg

# define DEL_KEY here as done in current sette_test_cases.sh script (ADD_KEY is useless as it concern only key_signedzero which should be define in the arch file)

# LOAD function
. ./all_functions.sh

# CREATE job submission template
cp BATCH_TEMPLATE/${JOB_PREFIX}-${COMPILER} job_batch_template || exit

# RUN all CONFIGuration in TEST_CONFIG list
for CONFIG in ${SETTE_TEST_CONFIGS}  # SETTE_TEST_CONFIG defined in cfgs/test (not in param.cfg)
do
    # CREATE scripts (step needed if we want a script run_sette_${CONFIG}.sh to run compilation and submit job)
    ./prepare_run_sette.sh # => master script that compile then submit all the individual jobs for one CONFIGuration

    # RUN main script
    $PATH_TO_SCRIPT/run_sette_${CONFIG}.sh  # could be run by using BATCH submission (with/without --wait option or similar) if needed
                                            # could easily be switch to parrallel run as soon as makenemo allow multiple CONFIG to be compile at the same time
         cd $SETTE_DIR

         # LOAD input data
         . $PATH_TO_INPUT/input_${CONFIG} # in cfgs/.sette_cfgs or in cfgs/config (but not all of them exist) ?
                 
         # LOAD param value
         . ./param.cfg
            # To add in param: 
            NJ=8 # => use to define number of tread used for compilation

         # LOAD function
         . ./all_functions.sh

         # CREATE validation directory
         set_valid_dir

         # COMPILE NEMO
         cd $NEMO_DIR
         . ./makenemo -m ${CMP_NAM} -n ${CONFIG}_ST -a ${CFG_REF} -j ${NJ} add_key ${ADD_KEYS} del_key ${DEL_KEYS}

         # SETUP REF/MPP/RST run directory (copy EXP00 + REF/RST/MPP + mv old files)
         for TEST_NAME in 'REF MPP RST'; do
            cd $SETTE_DIR

            # set EXE_DIR and move old files
            . ./prepare_exe_dir.sh       # set $EXE_DIR
  
            # set XIOS
            set_xios                     # set_xio_using_server iodef.xml ${USING_MPMD}

            # set namelist
            set_namelist_common          # set common namelist
            set_namelist_${TEST_NAME}    # set specific namelist
            
            NPROC=$((NPROC$TEST_NAME))

            # create run_job.sh file for REF/MPP/RST
            . ./prepare_run_job.sh input_${CONFIG}.cfg ${NPROC} ${TEST_NAME} ${MPIRUN_FLAG} run_job.sh ${NUM_XIOSERVERS} ${NEMO_VALID}
         done

         # link restart for RST simulation
         link_restart

         # RUN Simulation REF/MPP/RST (report script will need to be updated)
         # option to wait REF is done defined in param ('-W depend=afterany:' for SGE)
         . ./fcm_job.sh $NPROCREF ${MAIN_DIR}/cfgs/${CONFIG}_ST/REF           ; REFid=${JOBID} # JOBID comes from fcm_job.sh
         . ./fcm_job.sh $NPROCMPP ${MAIN_DIR}/cfgs/${CONFIG}_ST/MPP           ; MPPid=${JOBID} #
         . ./fcm_job.sh $NPROCRST ${MAIN_DIR}/cfgs/${CONFIG}_ST/RST -w $REFid ; RSTid=${JOBID} # wait REFid to finish to start

done
