#!/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.
################################################################################
################################################################################


. `dirname "$0"`/common


trim_spaces() {
    echo "$1" | sed 's/^ *//;s/ *$//'
}


emit_show_spooler() {
    case "${EF_OPT}" in
        -noredirect)
            "${EF_ROOT}/plugins/ef/bin/ef.show.spooler" "${EF_SPOOLER_URI}"
            ;;
        -silent)
            ;;
        *)
            echo "  <ef:redirect>$(ef_xml_escape_content -i "${REQUEST_URL_FIXED}?_uri=//com.enginframe.system/show.spooler&_spooler=$(ef_escape_uri_component "${EF_SPOOLER_URI}")")</ef:redirect>"
            ;;
    esac
}


# _ef_submit_cluster set in grid/bin/grid.submit
echo "${_ef_submit_cluster}" > .cluster

# Cleanup EF_OPT and provide default value if missing
EF_OPT=`trim_spaces "${EF_OPT}"`
if [ -z "${EF_OPT}" ]; then
    if [ -n "${EF_WEBSERVICE_REQUEST}" ]; then
        EF_OPT='-noredirect'
    else
        EF_OPT='-redirect'
    fi
fi


# Check if we must use project to distinguish users
if [ "${USE_PROJECT_FOR_USER}" = 'true' ]; then
    BSUBOUT=`"${EF_LSF_BSUB}" -P "${EF_USER}" "$@" 2>&1`
else
    BSUBOUT=`"${EF_LSF_BSUB}" "$@" 2>&1`
fi

RESULT=$?
JOBID=`ef_getjobid "${BSUBOUT}"`

if [ "${RESULT}" = '0' -a -n "${JOBID}" ]; then
    if [ -n "${EF_SPOOLER_URI}" ]; then
        echo "${JOBID}" >> .lsfjobs
        TMP="/tmp/bsub-out$$.ef"
        echo "${EF_SPOOLER_URI}" > ${TMP}
        POSTFILE="${TMP}"
        if [ -n "${EF_LSF_BPOST}" ]; then
          "${EF_LSF_BPOST}" -a "${POSTFILE}" -d "EF_SPOOLER_URI" "${JOBID}" 2>/dev/null
        fi
        rm -f "${TMP}"
        emit_show_spooler
    else
        echo "<ef:output ${EF_XMLNS_ef}>"
        echo "${BSUBOUT}" | ef_xml_escape -p
        echo "</ef:output>"
    fi

    # Call service to fill the job cache with the new job(s)
    "${EF_ROOT}/plugins/grid/bin/grid.jobcache.add.update.jobs" \
        --jobids "${JOBID}" \
        --grid "lsf" \
        --cluster "${LSF_CLUSTER_ID}" \
        --spooler-uri "${EF_SPOOLER_URI}"

    _exit_code=0
else
    _command=`echo "bsub ${EF_PARAMS}" | ef_xml_escape -p`
    _message=`echo "${BSUBOUT}" | ef_xml_escape -p`

    echo "<ef:error ${EF_XMLNS_ef}>"
    echo "  <ef:title>Submission failed</ef:title>"
    echo "  <ef:command>${_command}</ef:command>"
    echo "  <ef:message>${_message}</ef:message>"
    echo "</ef:error>"
    _exit_code=1
fi

if [ -n "${EF_SPOOLER_NAME}" -a -n "${EF_SPOOLER_URI}" ]; then
    "${EF_ROOT}/plugins/ef/bin/ef.spooler.info" "${EF_SPOOLER_URI}" "${EF_SPOOLER_NAME}"
fi

exit "${_exit_code}"


# --------------------------------------------------------------------------- #
# vi: ts=4 sw=4 et syntax=sh :
# --------------------------------------------------------------------------- #

