#!/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/awsbatch/bin/common"
. "${EF_ROOT}/plugins/awsbatch/bin/aws.utils"

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
}

# parse awsbsub output to get job id
ef_getjobid() {
    [ -n "${1}" ] && echo "${1}" | sed -n 's/^Job \([^ ]*\) .*$/\1/p'
}

# 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

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

_awsbsub_out="$(call_aws_ef_error awsbsub --cluster "${AWSBATCH_CLUSTER_ID}" --env all "$@" 2>&1)"

_exit_code=$?
_jobid="$(ef_getjobid "${_awsbsub_out}")"

if [ "${_exit_code}" = '0' -a -n "${_jobid}" ]; then
    if [ -n "${EF_SPOOLER_URI}" ]; then
        echo "${_jobid}" >> .awsjobs
        emit_show_spooler
    else
        echo "<ef:output ${EF_XMLNS_ef}>"
        echo "${_awsbsub_out}" | 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 "awsbatch" \
        --cluster "${AWSBATCH_CLUSTER_ID}" \
        --spooler-uri "${EF_SPOOLER_URI}"

    if [ -n "${EF_SPOOLER_URI}" ]; then

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

    # create S3 remote spooler
    _s3_spooler_url="$(awsbstat --cluster "${AWSBATCH_CLUSTER_ID}" -d "${_jobid}" | sed -n 's/s3FolderUrl *: \(.*\)/\1/p' | head -n 1)"
    _s3_bucket="$(echo "${_s3_spooler_url}" | sed -n 's#s3://\([^/]*\)/.*/#\1#p')"
    _s3_folder="$(echo "${_s3_spooler_url}" | sed -n 's#s3://\([^/]*\)/\(.*\)/#\2#p')"

    "${EF_ROOT}/plugins/rspooler/bin/ef.rspooler.add.sh" -t s3 -h "${_s3_bucket}" -p "/${_s3_folder}" -l "Remote S3"
fi
else
    echo ${_awsbsub_out}
fi

exit "${_exit_code}"

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