#!/bin/bash
source ../classic/librunscript.sh
source ecconf.cfg

function mpirun()
{
    # This function overrides the mpi command to be able to use the native launch function
    :
}

function get_hosts()
{
    # This function overrides get_hosts from ecconf.cfg in order to use dummy testing nodes
    hosts=(node01 node02 node03 node04 node05 node06 node07 node08 node09 \
           node10 node11 node12 node13 node14 node15 node16 node17 node18 \
           node19 node20 node21 node22 node23 node24 node25 node26 node27) 
}

function machinefile_config()
{
    # This function overrides the user configuration to fix an environment

    if has_config ifs nemo pisces rnfmapper xios lpjg ; then
      if ! has_config tm5 ; then
            ifs_ppn=48 #ifs_numproc=384
            nem_ppn=42 #nem_numproc=331
            xio_ppn=5  #xio_numproc=5
            lpjg_ppn=6 #lpjg_numproc=42
        else
            ifs_ppn=44 #ifs_numproc=256
            nem_ppn=48 #nem_numproc=192
            xio_ppn=39 #xio_numproc=39
            lpjg_ppn=4 #lpjg_numproc=28
            tm5_ppn=4  #tm5_numproc=2x2
        fi
      else
        # Add any new exclusive binary here
        ifs_exc=TRUE
        nem_exc=TRUE
        xio_exc=TRUE
        lpjg_exc=TRUE
        tm5_exc=TRUE

        # Modify the allocation to each binary using more than one process here
        ifs_ppn=48
        nem_ppn=48
        xio_ppn=48
        lpjg_ppn=48
        tm5_ppn=45
    fi
}

function esm_launch()
{
    # Launch function call as it is on ece-esm.sh
    has_config nemo && \
    cmd="${xio_numproc} ${xio_exe_file} -- \
         ${nem_numproc} ${nem_exe_file} -- \
         ${ifs_numproc} ${ifs_exe_file} -v ecmwf -e ${exp_name}" || \
    cmd="${ifs_numproc} ${ifs_exe_file} -v ecmwf -e ${exp_name}"

    has_config lpjg && cmd=${cmd}" -- ${lpjg_numproc} ${lpjg_exe_file} guess.ins -parallel"
    has_config tm5  && cmd=${cmd}" -- ${tm5_numproc}  ${tm5_exe_file} tm5-run.rc"
    has_config amip && cmd=${cmd}" -- ${amip_numproc} ${amip_exe_file}"
    has_config nemo && cmd=${cmd}" -- ${rnf_numproc} ${rnf_exe_file}"

    launch $cmd
}

function nemo_launch()
{
    # Launch function call as it is on ece-nemo.sh
    launch ${xio_numproc} ${xio_exe_file} -- ${nem_numproc} ${nem_exe_file}
}

function test_machinefile()
{
    # Function that indirectly calls the launch function to then validate the output
    use_machinefile="true"
    launch_output=$(has_config ifs && esm_launch || nemo_launch)
    validate_machinefile "$launch_output"
}

function validate_machinefile()
{
    # Function that validates the test taking into account the expected output and the expected test result
    success="false"
    # Validate that both machine file and output (errors) are equal to the expected.
    # The machine is in a summarized format: group and count repeated consecutive lines, remove line breaks and squeeze repeated spaces. 
    [ "$(cat machinefile | uniq -c | paste -d" " -s | tr -s " ")" == "$expected_machinefile" ] && \
    [ "$launch_output" == "$expected_output" ] && success="true"
    rm machinefile
    [ "$success" == "true" ] && echo "TEST OK" || echo "TEST FAILED"
}

proc_per_node=48
ifs_exe_file="somedir/ifs"
nem_exe_file="somedir/nemo"
rnf_exe_file="somedir/runoff-mapper"
lpjg_exe_file="somedir/guess"
amip_exe_file="somedir/amip-forcing"
tm5_exe_file="somedir/tm5"
xio_exe_file="somedir/xios_server"

ifs_numproc=480
nem_numproc=229
rnf_numproc=1
lpjg_numproc=96
amip_numproc=1
tm5_numproc=45
xio_numproc=48

expected_output=""

#1 XIOS NEMO IFS RUNOFF_MAPPER
config='ifs nemo:elpin lim3 rnfmapper xios:detached oasis save_ic:end_leg'
expected_machinefile=' 48 node01 48 node02 48 node03 48 node04 48 node05 37 node06 48 node07 48 node08 48 node09 48 node10 48 node11 48 node12 48 node13 48 node14 48 node15 48 node16 1 node06'
test_machinefile

#2 XIOS NEMO IFS TM5 RUNOFF-MAPPER
ifs_numproc=40
nem_numproc=24
xio_numproc=10
config='ifs nemo lim3 rnfmapper xios oasis tm5'
expected_machinefile=' 10 node01 24 node02 40 node03 45 node04 1 node01'
test_machinefile

#3 IFS AMIP TM5
config='ifs amip oasis tm5'
expected_machinefile=' 40 node01 45 node02 1 node01'
test_machinefile

#4 IFS AMIP
ifs_numproc=479
config='ifs amip oasis'
expected_machinefile=' 48 node01 48 node02 48 node03 48 node04 48 node05 48 node06 48 node07 48 node08 48 node09 48 node10'
test_machinefile

#5 XIOS NEMO
nem_numproc=336
xio_numproc=96
config='nemo lim3 xios'
expected_machinefile=' 48 node01 48 node02 48 node03 48 node04 48 node05 48 node06 48 node07 48 node08 48 node09'
test_machinefile

#6 XIOS NEMO IFS LPJG RUNOFF-MAPPER
nem_numproc=331
ifs_numproc=384
xio_numproc=5
lpjg_numproc=42
config='ifs nemo pisces rnfmapper xios lpjg'
expected_machinefile=' 47 node01 42 node02 42 node03 42 node04 42 node05 42 node06 42 node07 37 node08 48 node09 48 node10 48 node11 48 node12 48 node13 48 node14 48 node15 48 node16 6 node02 6 node03 6 node04 6 node05 6 node06 6 node07 6 node08 1 node01'
test_machinefile

#7 XIOS NEMO IFS LPJG TM5 RUNOFF-MAPPER
ifs_numproc=256
nem_numproc=192
xio_numproc=39
lpjg_numproc=28
tm5_numproc=4
config='ifs nemo pisces rnfmapper xios lpjg tm5'
expected_machinefile=' 39 node01 48 node02 48 node03 48 node04 48 node05 44 node06 44 node07 44 node08 44 node09 44 node10 36 node11 4 node01 4 node06 4 node07 4 node08 4 node09 4 node10 4 node11 5 node01'
test_machinefile

#8 INVALID CONFIGURATION TEST: INSUFFICIENT RESOURCES
nem_numproc=9999
xio_numproc=48
config='ifs nemo lim3 rnfmapper xios oasis'
expected_output="Not enough computing nodes"
expected_machinefile=' 48 node01 48 node02 48 node03 48 node04 48 node05 48 node06 48 node07 48 node08 48 node09 48 node10 48 node11 48 node12 48 node13 48 node14 48 node15 48 node16 48 node17 48 node18 48 node19 48 node20 48 node21 48 node22 48 node23 48 node24 48 node25 48 node26 48 node27'
test_machinefile
