#!/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_HOME: value to set the HOME variable to if not already"
    echo "  defined in the environment, as required for OpenMPI >= 5."
    echo "  Unset if HOME is defined."
    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_API_MODE: standard or api mode for the management of output result files created by Khiops"
    echo "  In standard mode, the result files are stored relative to the train database directory and the report file"
    echo "  extension is automatically added if not specified."
    echo "  In api mode, the result files are stored relative to the current working directory and the report file name"
    echo "  is used as specified."
    echo "  In both modes, if an absolute path to the report file is specified, then the result files are stored in the"
    echo "  same directory as the report file."
    echo "  . default behavior if not set: standard mode"
    echo "  . set to 'false' to force standard mode"
    echo "  . set to 'true' to force api mode"

    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 "  It has no effect on OpenMPI 5 versions, where messages from mpi are"
    echo "  always printed."
    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/"
}

# Additional variables for MPI
export OMPI_MCA_btl_vader_single_copy_mechanism=none # issue on docker

# Set to true is a GUI is availble on the current environment
_GUI_SUPPORTED=OFF

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

# Export KHIOPS_DRIVERS_PATH so that the MODL_* binaries can use it to find the drivers.
export KHIOPS_DRIVERS_PATH

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

if ! [ "$KHIOPS_MPI_VERBOSE" == "true" ]; then
    # Mute mpi by adding flags when supported (OpenMPI <= 4 only; no extra flag
    # is added for OpenMPI 5, because there is none supported)
    _MPI_EXTRA_FLAGS=""
fi

KHIOPS_PATH=$(get_script_dir)MODL_openmpi
KHIOPS_COCLUSTERING_PATH=$(get_script_dir)MODL_Coclustering_openmpi

# Most of the time the mpiexec executable is in the same directory as the MODL
# executable; this applies to:
# - Debian and Ubuntu native installations
# - installations in Conda and Conda-based environments
_MPIEXEC=$(get_script_dir)mpiexec
if command -v $_MPIEXEC &>/dev/null; then
    KHIOPS_MPI_ERROR=""
# Fallback for Rocky Linux, where native OpenMPI is in a different directory
elif command -v mpiexec &>/dev/null; then
    KHIOPS_MPI_ERROR=""
    _MPIEXEC=$(readlink -f "$(type -P mpiexec)")
else
    KHIOPS_MPI_ERROR="We didn't find mpiexec in the expected paths. Parallel computation is unavailable: Khiops is launched in serial"
fi

# Set the KHIOPS_MPI_HOME variable if HOME is not already set
# KHIOPS_MPI_HOME is set to the temporary directory of the current user or to
# /tmp if not defined
# If HOME is not defined, then it must be set to $KHIOPS_MPI_HOME
# This is required by OpenMPI >= 5; see
# https://github.com/open-mpi/ompi/issues/10732
if [[ -z $HOME ]]; then
    KHIOPS_MPI_HOME=${TMPDIR:-${TEMP:-${TMP:-/tmp}}}
fi

if [[ -z $KHIOPS_MPI_ERROR ]]; then
    KHIOPS_MPI_COMMAND="$_MPIEXEC $_MPI_EXTRA_FLAGS --allow-run-as-root $KHIOPS_MPI_EXTRA_FLAGS"
    
    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_HOME "$KHIOPS_MPI_HOME"
    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"
    
    # Additional variables for MPI
    echo OMPI_MCA_btl_vader_single_copy_mechanism "$OMPI_MCA_btl_vader_single_copy_mechanism"
fi
