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


. "${EF_ROOT}/plugins/sge/bin/common"

_get_jobid_from_qsub_out() {
    local _qsub_out=$1

    local _jobid=$(cat "${_qsub_out}" | sed -n 's/^Your job\(\-array\)\? *\([0-9]*\)\(\.[0-9:\-]*\)\? (.* has been submitted\.*$/\2/p')
    echo "${_jobid}"
}

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

TMP="/tmp/qsub-out$$.ef"

echo "<ef:output ${EF_XMLNS_ef}>"

"${SGE_BINDIR}/qsub" -ac "EF_SPOOLER=${EF_SPOOLER}" -cwd "$@" > "${TMP}" 2>&1

RESULT=$?
JOBID=""

_TMPVAR=`cat "${TMP}" | grep 'has been submitted'`

# SoGE job submission RESULT == 1 when job goes in pending state (qw).
if [ "${RESULT}" = "1" ]; then
    if [ "$(get_sge_flavor)" == "SGE" ] ; then
        JOBID=$(_get_jobid_from_qsub_out "${TMP}")
        _jobstatus=$(get_jobstatus "${JOBID}")
        [ "${_jobstatus}" == "qw" ] && RESULT=0
    fi
fi

if [ "${RESULT}" = "0" -a -n "${_TMPVAR}" ]; then
    if [ -n "${EF_SPOOLER_URI}" ]; then
        [ -z "${JOBID}" ] && JOBID=$(_get_jobid_from_qsub_out "${TMP}")
        echo ${JOBID} >> .sgejobs
        emit_show_spooler
    else
        cat "${TMP}" | ef_xml_escape -p
    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 "sge" \
        --cluster "${SGE_CLUSTER_ID}" \
        --spooler-uri "${EF_SPOOLER_URI}"

else
    cat << EOF
<ef:error ${EF_XMLNS_ef}>
  <ef:title>Submission failed</ef:title>
  <ef:command>$(echo qsub "$@" | ef_xml_escape -p)</ef:command>
  <ef:message>$(cat "${TMP}" | ef_xml_escape -p)</ef:message>
</ef:error>
EOF
fi

rm -f ${TMP}

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

echo "</ef:output>"

exit ${RESULT}


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

