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

#-------------------------------------------------------------------------------
# Avoid recursive loops
#-------------------------------------------------------------------------------
[ -n "${EF_SGE_DO_NOT_RECURSE}" ] && exit 0
EF_SGE_DO_NOT_RECURSE=true
export EF_SGE_DO_NOT_RECURSE

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

EF_PLUGIN_NAME="EnginFrame SGE Plug-in"
export EF_PLUGIN_NAME

EF_PLUGIN_FILE="EFSGE"
export EF_PLUGIN_FILE

EF_PLUGIN_ROOT="${EF_ROOT}/plugins/sge"
export EF_PLUGIN_ROOT


#-------------------------------------------------------------------------------
# Load common EnginFrame functions and environment
#-------------------------------------------------------------------------------

. "${EF_ROOT}/plugins/ef/lib/functions"

ef_init_environment

#-------------------------------------------------------------------------------
# Load SGE Plug-in configuration
#-------------------------------------------------------------------------------

ef_source_conf sge "ef.sge.conf"
if [ -f "${SGE_PROFILE}" ] ; then
    . "${SGE_PROFILE}"
fi

if [ ! -d "${SGE_ROOT}" ] ; then
  ef_error "SGE_PROFILE is not correctly defined inside ef.sge.conf" \
           "EnginFrame GE Plugin Error" \
           "\${EF_ROOT}/plugins/sge/bin/common"
  exit 1
fi


#-------------------------------------------------------------------------------
# Check SGE is up and store cluster name
#-------------------------------------------------------------------------------

# $SGE_CLUSTER_NAME is defined by GridEngine itself
# The default GridEngine cluster name is p<master port>
# where master port = $SGE_QMASTER_PORT (default 6444)
if [ -n "${SGE_CLUSTER_NAME}" ]; then
    SGE_CLUSTER_ID="${SGE_CLUSTER_NAME}"
elif [ -n "${SGE_QMASTER_PORT}" ]; then
    SGE_CLUSTER_ID="p${SGE_QMASTER_PORT}"
else
    SGE_CLUSTER_ID="p6444"
fi
export SGE_CLUSTER_ID


# Define the architecture of the hosting machine
ARCH=`${SGE_ROOT}/util/arch`

# Define the gridEngine binary directory
SGE_BINDIR=${SGE_ROOT}/bin/${ARCH}

# Redefine the PATH.
PATH=${SGE_BINDIR}:${PATH}

export ARCH PATH SGE_BINDIR

if [ -n "${REQUEST_URL}" ]; then
    REQUEST_URL_FIXED="/`echo ${REQUEST_URL} | cut -d/ -f4-`"
    export REQUEST_URL_FIXED
fi


#-------------------------------------------------------------------------------
 # Load common SGE Plugin functions
#-------------------------------------------------------------------------------
. "${EF_ROOT}/plugins/sge/bin/functions.lib.sh"

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

