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

# parse the output of awsbhosts
_ef_awsbhosts_2xml () {
    "${EF_AWK}" \
        -v CLUSTER_ID="${AWSBATCH_CLUSTER_ID}" \
        -v EF_XMLNS_grid="${EF_XMLNS_grid}" \
        -v "statusmap=${EF_ROOT}/plugins/awsbatch/conf/grid.host.status.mapping" \
        -f "${EF_ROOT}/plugins/ef/lib/awk/utils.awk" \
        -f "${EF_ROOT}/plugins/awsbatch/lib/awsbhosts_2xml.awk"
}

_awsbhosts_out="$(call_aws_ef_error awsbhosts --cluster "${AWSBATCH_CLUSTER_ID}" --details ${host} 2>&1)"
_exit_code="$?"

if [ "${_exit_code}" != "0" ]; then
    echo "${_awsbhosts_out}"
    exit ${_exit_code}
fi

# sort settings thru session variables
if [ "x${cluster_sort_order}" = "xascending" -a "x${_csortby}" = "x${cluster_sortby}" ]; then
    cluster_sort_order="descending"
else
    cluster_sort_order="ascending"
fi

[ -z "${_csortby}" ] && _csortby="name"

echo "<ef:session ${EF_XMLNS_ef}>"
echo "  <ef:option id=\"cluster_sort_order\">$cluster_sort_order</ef:option>"
echo "</ef:session>"
echo "<ef:session ${EF_XMLNS_ef}>"
echo "  <ef:option id=\"cluster_sortby\">$(ef_xml_escape_content -i "$_csortby")</ef:option>"
echo "</ef:session>"

# print output
echo "${_awsbhosts_out}" | _ef_awsbhosts_2xml

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