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


EXIT_CODE="$?"

if [ "$EXIT_CODE" != "0" ] ; then
    exit $EXIT_CODE
fi

unset PARAM
unset EXTRA
while [ -n "${1}" ]; do
    [ -n "${PARAM}" ] && EXTRA="${EXTRA} $1" || EXTRA="$1"
    PARAM="-h"
    shift
done

# Create a table containing the host's raw data
ef_qhost_table() {
    "${EF_AWK}" \
        -v "statusmap=${EF_ROOT}/plugins/sge/conf/grid.arch.mapping" \
        -f "${EF_ROOT}/plugins/ef/lib/awk/utils.awk" \
        -f "${EF_ROOT}/plugins/sge/bin/qhost_table.awk"
}


# sort records according to the selected sorting method
ef_qhost_sort() {
    _cat="cat"
    if [ "x$cluster_sort_order" = "xdescending" ]; then
        _reverse="-r"
        _cat="tac"
    fi

    case $_sortby in
        name)  sort $_reverse -t';'    -k1  ;;
        njobs) sort $_reverse -t';' -n -k16 ;;
        r1m)   sort $_reverse -t';' -n -k8  ;;
        ut)    sort $_reverse -t';' -n -k13 ;;
        swp)   sort $_reverse -t';' -n -k10 ;;
        mem)   sort $_reverse -t';' -n -k12 ;;
        *)     "${_cat}"                    ;;
    esac
}


# Transform each table record into xml
ef_qhost_xml() {

    "${EF_AWK}" \
        -F';' \
        -v SGE_CLUSTER_ID="${SGE_CLUSTER_ID}" \
        -v EF_XMLNS_grid="${EF_XMLNS_grid}" \
        -f "${EF_ROOT}/plugins/ef/lib/awk/utils.awk" \
        -f <( echo '
        BEGIN {
            print "<grid:host-list type=\"sge\" cluster=\"" SGE_CLUSTER_ID "\" " EF_XMLNS_grid ">"
        }

        END {
            print "</grid:host-list>"
        }

        {
            print  "  <grid:host type=\"sge\" name=\""escapeXmlAttribute($1)"\" model=\""$2"\" arch=\""$3"\" arch-family=\"" $4 "\"  ncpus=\""$5"\">"
            print  "    <grid:status batch=\""$6"\" ef=\""$6"\" grid=\""$6"\"/>"
            print  "    <grid:resource name=\"swp\" max=\""$7"\">"$8"</grid:resource>"
            print  "    <grid:resource name=\"mem\" max=\""$9"\">"$10"</grid:resource>"
            printf "    <grid:resource name=\"ut\">%.2f</grid:resource>\n", $11
            printf "    <grid:resource name=\"r1m\">%.2f</grid:resource>\n", $12
            printf "    <grid:resource name=\"r5m\" >%.2f</grid:resource>\n", $13
            printf "    <grid:resource name=\"r15m\">%.2f</grid:resource>\n", $14
            print  "    <grid:usage running-jobs=\"" $15 "\"/>";
            print  "  </grid:host>"
        }
  ')
}

_qhost_out=$("${SGE_BINDIR}/qhost" -F -j ${PARAM} ${EXTRA} 2>&1)
_exit_code="$?"
if [ "${_exit_code}" != "0" ]; then
  ef_error "${_qhost_out} - exit code (${_exit_code})" \
    "EnginFrame GE Plugin Error" \
    "${SGE_BINDIR}/qhost -F -j ${PARAM} ${EXTRA}"
  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>"
echo "  <ef:option id=\"cluster_sort_order\">$cluster_sort_order</ef:option>"
echo "</ef:session>"
echo "<ef:session>"
echo "  <ef:option id=\"cluster_sortby\">$(ef_xml_escape_content -i "$_csortby")</ef:option>"
echo "</ef:session>"

# print output
if [ "${_exit_code}" = "0" ]; then
    echo "${_qhost_out}" \
    | grep -v "^global " \
    | ef_qhost_table \
    | ef_qhost_sort \
    | ef_qhost_xml
fi
