#!/bin/bash

################################################################################
################################################################################
# Copyright 2023-2025 by NI SP Software GmbH, All rights reserved.
# Copyright 1999-2023 by Nice, srl., All rights reserved.
#
# This software includes confidential and proprietary information
# of NI SP Software GmbH ("Confidential Information").
# You shall not disclose such Confidential Information
# and shall use it only in accordance with the terms of
# the license agreement you entered into with NI SP Software.
################################################################################
################################################################################

#-------------------------------------------------------------------------------
# Load common EnginFrame functions and environment
#-------------------------------------------------------------------------------
EF_FUNCTIONS="${EF_ROOT}/plugins/ef/lib/functions"
if [ ! -f "${EF_FUNCTIONS}" ]; then
    echo '<ef:error>'
    echo "  <ef:title><![CDATA[File Manager Plugin Error]]></ef:title>"
    echo "  <ef:message><![CDATA[${EF_FUNCTIONS} is missing]></ef:message>"
    echo "  <ef:command><![CDATA[EF_ROOT${0#${EF_ROOT}}]></ef:command>"
    echo '</ef:error>'
    exit 1
fi

. "${EF_FUNCTIONS}"

EF_UTILS="${EF_ROOT}/plugins/ef/lib/utils"
if [ ! -f "${EF_UTILS}" ]; then
    echo '<ef:error>'
    echo "  <ef:title><![CDATA[File Manager Plugin Error]]></ef:title>"
    echo "  <ef:message><![CDATA[${EF_UTILS} is missing]></ef:message>"
    echo "  <ef:command><![CDATA[EF_ROOT${0#${EF_ROOT}}]></ef:command>"
    echo '</ef:error>'
    exit 1
fi

. "${EF_UTILS}"

EF_FM_COMMON="${EF_ROOT}/plugins/fm/fm/backends/common"
if [ ! -f "${EF_FM_COMMON}" ]; then
    echo '<ef:error>'
    echo "  <ef:title><![CDATA[File Manager Plugin Error]]></ef:title>"
    echo "  <ef:message><![CDATA[${EF_FM_COMMON} is missing]></ef:message>"
    echo "  <ef:command><![CDATA[EF_ROOT${0#${EF_ROOT}}]></ef:command>"
    echo '</ef:error>'
    exit 1
fi

. "${EF_FM_COMMON}"

# Print the given arguments in a format that can be reused as shell input.
_sh_escape_args() {
    printf '%q ' "$@"
}

# Execute lsrun command with the given parameters
#
# Required environment variables:
#   FM_LSRUN_OPTIONS
#   FM_VROOT_HOST
_lsrun_wrapper() {
    lsrun ${FM_LSRUN_OPTIONS} -m "${FM_VROOT_HOST}" /bin/bash -u -c "${*/%/;}"
}

# Execute lsrun command with the given parameters
#
# Required environment variables:
#   _master
_lsrun_wrapper_on_master() {
    lsrun -m "${_master}" /bin/bash -u -c "${*/%/;}"
}

lsrun_function() {
    local _lsrun_func_name="$1"; shift
    local _lsrun_func_vars=$([[ $# -ne 0 ]] && declare -p "$@" || echo ':')
    local _lsrun_func=$(declare -f "${_lsrun_func_name}")
    _lsrun_wrapper "${_lsrun_func_vars}" "${_lsrun_func}" "${_lsrun_func_name}"
}

lsrun_command() {
    local _lsrun_cmd=$(_sh_escape_args "$@")
    _lsrun_wrapper "${_lsrun_cmd}"
}

lsrun_command_on_master() {
    local _lsrun_cmd=$(_sh_escape_args "$@")
    _lsrun_wrapper_on_master "${_lsrun_cmd}"
}

trim() {
    echo "$@"
}

# Report a fatal system error
fatal() {
    fm_ef_error -fatal -message "$1" -secondary "$2" -exit 1
}


# Report a user error
error() {
    fm_ef_error -error -message "$1" -secondary "$2" -exit 1
}

fm_ef_error() {
    _fm_ef_error_level='Error'
    _fm_ef_error_code='1'
    _fm_ef_error_message=''
    _fm_ef_error_secondary_message=''

    while [ -n "$1" ]; do
        case "$1" in
            -fatal)
                _fm_ef_error_level='Fatal Error'
                ;;
            -error)
                _fm_ef_error_level='Error'
                ;;
            -msg|-message)
                _fm_ef_error_message="$2"
                shift
                ;;
            -secondary)
                _fm_ef_error_secondary_message="$2"
                shift
                ;;
            -exit)
                _fm_ef_error_code="$2"
                shift
                ;;
            *)
                ;;
        esac
        shift
    done
    _fm_ef_error_message=`ef_xml_escape -i "${_fm_ef_error_message}"`
    _fm_ef_plugin_name=`ef_xml_escape -i "${EF_PLUGIN_NAME}"`
    _fm_ef_command=$(ef_xml_escape -i "EF_ROOT${0#${EF_ROOT}}")

    echo '<ef:error>'
    echo "  <ef:title>${_fm_ef_plugin_name} ${_fm_ef_error_level}</ef:title>"
    echo "  <ef:message>${_fm_ef_error_message}</ef:message>"
    if [ -n "${_fm_ef_error_secondary_message}" ]; then
        _fm_ef_error_secondary_message=`ef_xml_escape -i "${_fm_ef_error_secondary_message}"`
        echo "  <ef:secondary-message>${_fm_ef_error_secondary_message}</ef:secondary-message>"
    fi
    echo '  <ef:apply-acl select="admin-only">'
    echo "    <ef:command>${_fm_ef_command}</ef:command>"
    echo '  </ef:apply-acl>'
    echo '</ef:error>'

    exit "${_fm_ef_error_code}"
}

main() {
    ef_init_environment "$@"

    # Source ef.lsf.conf to get the proper profile.lsf to use
    ef_source_conf lsf "ef.lsf.conf"
    _exit_code="$?"
    if [ "${_exit_code}" = "0" ]; then
        if [ -r "${LSF_PROFILE}" ]; then
            . "${LSF_PROFILE}"
        fi
    else
        if [ -z "${LSF_ENVDIR}" ]; then
            fatal "LSF profile not loaded" "Cannot read LSF profile from LSF plugin nor user environment"
            exit 1
        fi
    fi

    # In some multicluster environments, lsrun requests must pass through
    # the local master
    if [ "${LSF_MULTICLUSTER_THR_MASTER}" = "true" ]; then
        # first get local master
        _master=$(lsid | "${EF_AWK}" '/My master name is/ {print $5}')
        FM_LSRUN_OPTIONS="-m ${_master} lsrun"
    else
        FM_LSRUN_OPTIONS=""
    fi
}

main "@"
# ex:ts=2:et:
