#!/bin/bash
help() {
    echo "Usage: khiops_env [-h, --help] [--env]"
    echo "khiops_env is an internal script intended to be used by Khiops tool and Khiops wrappers only."
    echo "It sets all the environment variables required by the Khiops to run correctly (Java, MPI, etc)."
    echo "Options:"
    echo "   -h, --help show this help message and exit"
    echo "   -env show the environment list and exit"
    echo
    echo "The following variables are used to set the path and classpath for the prerequisite of Khiops."
    echo
    echo "KHIOPS_PATH: full path of Khiops executable"
    echo "KHIOPS_COCLUSTERING_PATH: full path of Khiops coclustering executable"
    echo "KHIOPS_MPI_COMMAND: MPI command to call the Khiops tool"
    echo "KHIOPS_JAVA_PATH: path of Java tool, to add in path"
    echo "KHIOPS_CLASSPATH: Khiops java libraries, to add in classpath"
    echo "KHIOPS_DRIVERS_PATH: search path of the drivers (by default /usr/bin if not defined)"
    echo
    echo "If they are not already defined, the following variables used by Khiops are set:"
    echo
    echo "KHIOPS_LAST_RUN_DIR: directory where Khiops writes output command file and log"
    echo "  (when not defined with -e and -o)"
    echo "KHIOPS_PROC_NUMBER: processes number launched by Khiops (it's default value corresponds to the"
    echo "  number of physical cores of the computer)"
    echo
    echo "The following variables are not defined by default and can be used to change some default"
    echo " properties of Khiops:"
    echo
    echo "KHIOPS_TMP_DIR: Khiops temporary directory location (default: the system default)."
    echo "  This location can be modified from the tool as well."
    echo "KHIOPS_MEMORY_LIMIT: Khiops memory limit in MB (default: the system memory limit)."
    echo "  The minimum value is 100 MB; this setting is ignored if it is above the system's memory limit."
    echo "  It can only be reduced from the tool."
    echo "KHIOPS_RAW_GUI: graphical user interface for file name selection"
    echo "  . default behavior if not set: depending on the file drivers available for Khiops"
    echo "  . set to 'true' to allow file name selection with uri schemas"
    echo "  . set to 'false' to allow local file name selection only with a file selection dialog"
    echo "KHIOPS_MPI_VERBOSE: true (default) or false, print messages from mpi (OpenMPI only)."
    echo "KHIOPS_MPI_EXTRA_FLAGS: extra flags added to the mpi command line"
    echo
    echo "In case of configuration problems, the variables KHIOPS_JAVA_ERROR and KHIOPS_MPI_ERROR contain error messages."
}

[[ $# == 1 && $1 == "-h" ]] && help && exit 0
[[ $# == 1 && $1 == "--help" ]] && help && exit 0

if [[ -z $KHIOPS_LAST_RUN_DIR ]]; then
    export KHIOPS_LAST_RUN_DIR=/tmp/khiops/$USER
fi


get_script_dir() {
    SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
    echo "$SCRIPT_DIR/"
}


# Inside conda environment
export _IS_CONDA=true

# Drivers search path
export KHIOPS_DRIVERS_PATH=$(dirname $(get_script_dir))/lib

if [ -z "$KHIOPS_MPI_VERBOSE" ]; then
    KHIOPS_MPI_VERBOSE="true"
fi

if ! [ "$KHIOPS_MPI_VERBOSE" == "true" ]; then
    # Mute mpi by adding flags (OpenMPI only)
    _MPI_EXTRA_FLAGS=""
fi

KHIOPS_PATH=$(get_script_dir)MODL
KHIOPS_COCLUSTERING_PATH=$(get_script_dir)MODL_Coclustering

if command -v mpiexec &>/dev/null; then
    KHIOPS_MPI_ERROR=""
    _MPIEXEC=$(readlink -f "$(type -P mpiexec)")
else
    # Fallback for Conda-based environments where `mpiexec` is not in PATH,
    # because $CONDA_PREFIX/bin is not in PATH
    _MPIEXEC=$(get_script_dir)mpiexec
    if command -v $_MPIEXEC &>/dev/null; then
        KHIOPS_MPI_ERROR=""
    else
        KHIOPS_MPI_ERROR="We didn't find mpiexec in the expected paths. Parallel computation is unavailable: Khiops is launched in serial"
    fi

fi

if [[ -z $KHIOPS_MPI_ERROR ]]; then
    KHIOPS_MPI_COMMAND="$_MPIEXEC $_MPI_EXTRA_FLAGS -host localhost $KHIOPS_MPI_EXTRA_FLAGS"
    
    # Number of processes in use (must be set according to the physical cores number)
    if [[ -z $KHIOPS_PROC_NUMBER ]]; then
        KHIOPS_PROC_NUMBER=$("$(get_script_dir)"_khiopsgetprocnumber | head -n 1)
    fi

    if [[ -n $KHIOPS_PROC_NUMBER ]]; then
        KHIOPS_MPI_COMMAND="$KHIOPS_MPI_COMMAND -n $KHIOPS_PROC_NUMBER"
    fi

    # without more than 2 procs, we use the serial khiops
    if [[ -n $KHIOPS_PROC_NUMBER && $KHIOPS_PROC_NUMBER -le 2 ]]; then
        KHIOPS_MPI_COMMAND=""
    fi
fi
unset _MPIEXEC
unset _MPI_EXTRA_FLAGS

if [ "$1" = "--env" ]; then
    echo KHIOPS_PATH "$KHIOPS_PATH"
    echo KHIOPS_COCLUSTERING_PATH "$KHIOPS_COCLUSTERING_PATH"
    echo KHIOPS_MPI_COMMAND "$KHIOPS_MPI_COMMAND"
    echo KHIOPS_JAVA_PATH "$KHIOPS_JAVA_PATH"
    echo KHIOPS_CLASSPATH "$KHIOPS_CLASSPATH"
    echo KHIOPS_LAST_RUN_DIR "$KHIOPS_LAST_RUN_DIR"
    echo KHIOPS_PROC_NUMBER "$KHIOPS_PROC_NUMBER"
    echo KHIOPS_TMP_DIR "$KHIOPS_TMP_DIR"
    echo KHIOPS_MEMORY_LIMIT "$KHIOPS_MEMORY_LIMIT"
    echo KHIOPS_API_MODE "$KHIOPS_API_MODE"
    echo KHIOPS_RAW_GUI "$KHIOPS_RAW_GUI"
    echo KHIOPS_DRIVERS_PATH "$KHIOPS_DRIVERS_PATH"
    echo KHIOPS_JAVA_ERROR "$KHIOPS_JAVA_ERROR"
    echo KHIOPS_MPI_ERROR "$KHIOPS_MPI_ERROR"
    echo KHIOPS_MPI_VERBOSE "$KHIOPS_MPI_VERBOSE"
    echo KHIOPS_MPI_EXTRA_FLAGS "$KHIOPS_MPI_EXTRA_FLAGS"
    
fi
