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

# Authors: Roberto Meda, Karsten Gaier
# common: PBS plugin common functions


#
# Examples of PBS commands output
#
# plain qstat
#
#Job id           Name             User             Time Use S Queue
#---------------- ---------------- ---------------- -------- - -----
#93940.nfs1       ..._fca3_21r_hf2 utl170           618:23:3 R special
#94442.nfs1       ..._fca3_21r_hf3 utl170           550:47:1 R special
#
#
# qstat -u $EF_USER
#
#Job ID           Username  Queue     Jobname      SessID NDS TSK Memory Time   S Time
#---------------  --------  --------  ----------   ------ --- --- ------ -----  - -----
#58129.nfs1.osc.  com0358   parallel  L28746.lsd   --     2  --   --     24:00  Q   --
#
#
# qstat -f $job_id
#
#Job Id: 65.ping
#    Job_Name = prova2.sh
#    Job_Owner = robby@ping
#    resources_used.cpupercent = 0
#    resources_used.cput = 00:00:00
#    resources_used.mem = 3252kb
#    resources_used.ncpus = 1
#    resources_used.vmem = 6516kb
#    resources_used.walltime = 00:32:33
#    job_state = R
#    queue = workq
#    server = ping
#    Checkpoint = u
#    ctime = Thu Oct 27 12:47:04 2005
#    Error_Path = ping:/home/robby/prova2.sh.e65
#    exec_host = ping:ncpus=1
#    Hold_Types = n
#    Join_Path = n
#    Keep_Files = n
#    Mail_Points = a
#    mtime = Thu Oct 27 12:48:43 2005
#    Output_Path = ping:/home/robby/prova2.sh.o65
#    Priority = 0
#    qtime = Thu Oct 27 12:47:04 2005
#    Rerunable = True
#    Resource_List.ncpus = 1
#    Resource_List.place = pack:group=host
#    Resource_List.select = 1:ncpus=1
#    stime = 1130410123
#    session_id = 29723
#    Variable_List = PBS_O_HOME=/home/robby,PBS_O_LANG=en_US.iso885915,
#        PBS_O_LOGNAME=robby,
#        PBS_O_PATH=/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/opt/j2sdk1.4.2
#        _09/bin:/home/robby/bin:/usr/pbs/bin,PBS_O_MAIL=/var/spool/mail/robby,
#        PBS_O_SHELL=/bin/bash,PBS_O_HOST=ping,PBS_O_WORKDIR=/home/robby,
#        PBS_O_SYSTEM=Linux,PBS_O_QUEUE=workq
#    comment = Job run at Thu Oct 27 at 12:48 on ping:ncpus=1
#    etime = Thu Oct 27 12:47:04 2005
#


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

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

ef_init_environment "$@"


#-------------------------------------------------------------------------------
# Load Torque Plug-in configuration
#-------------------------------------------------------------------------------

ef_source_conf torque "ef.torque.conf"
if [ -d "${PBS_BINDIR}" ] ; then
  PATH="${PBS_BINDIR}:${PATH}"
fi

which qstat >/dev/null 2>&1
if [ $? -ne 0 ] ; then
  ef_error "qstat not found" \
    "Torque binary directory is missing"
  exit 1
fi


_qstat_out=$(qstat -B -f 2>&1)
_exit_code="$?"
if [ "${_exit_code}" != "0" ]; then
  ef_error "${_qstat_out} - exit code (${_exit_code})" \
    "EnginFrame Torque Plugin Error" \
    "qstat -B -f"
  exit ${_exit_code}
fi

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

TORQUE_CLUSTER_ID=$(echo "${_qstat_out}" | sed '/^Server: /!d;s/^Server[:] *//g')
[ -z "${TORQUE_CLUSTER_ID}" ] && TORQUE_CLUSTER_ID="default"
export TORQUE_CLUSTER_ID


#
# Some plugin variables initialization
#
EF_AGENT=com.enginframe.torque
[ -z "$EF_USER" ] && EF_USER=$USER

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


#-------------------------------------------------------------------------------
# Load common Torque Plugin functions
#-------------------------------------------------------------------------------

. "${EF_ROOT}/plugins/torque/bin/functions.lib.sh"

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

