#! /bin/bash
# vim: ts=4 sw=4 et

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

################################################################################
# SVN $Id: functions 40780 2016-12-19 08:45:15Z luca $
################################################################################


#-------------------------------------------------------------------------------
# Source some common configuration
#-------------------------------------------------------------------------------
. "${EF_ROOT}/plugins/ef/conf/ef.xmlns.conf"


#-------------------------------------------------------------------------------
# Source some common functions from these libraries
#-------------------------------------------------------------------------------
. "${EF_ROOT}/plugins/ef/lib/logs"
. "${EF_ROOT}/plugins/ef/lib/plugins"
. "${EF_ROOT}/plugins/ef/lib/sessions"
. "${EF_ROOT}/plugins/ef/lib/spoolers"
. "${EF_ROOT}/plugins/ef/lib/utils"


#-------------------------------------------------------------------------------
# Fixes the passed in request URL. If the first input is missing,
# then REQUEST_URL environment variable is used as URL to fix.
#-------------------------------------------------------------------------------
fix_request_url() {
  _url_to_fix="$1"
  [ -z "${_url_to_fix}" ] && _url_to_fix="$REQUEST_URL"

  if [ -n "${_url_to_fix}" ]
  then
    REQUEST_URL_FIXED="/`echo ${_url_to_fix} | cut -d/ -f4-`"
    export REQUEST_URL_FIXED
    return 0
  else
    return 1
  fi
}



#-------------------------------------------------------------------------------
# List the environment variables starting with '_selection_'
#-------------------------------------------------------------------------------
list_selection_file_names() {
  env | grep '^_selection_*[0-9]*=' | sed "s%^_selection_*[0-9]*=%'%;s%$%'%"
}



# --------------------------------------------------------------------------- #
# EF_init_environment
# ===================
# Inizialize environment by exporting some useful variables:
# - define $PATH adding some standard path
# - define $URI
# - define $EF_AWK and $AWK
# - define $EF_HASH
# - define plugins variables
#
# Arguments: none
# --------------------------------------------------------------------------- #

ef_init_environment() {
    # Be sure to have some useful commands in path
    PATH=/bin:/usr/bin:/usr/sbin:$PATH
    export PATH


    # ---[ AWK ]---

    unset EF_AWK

    EF_AWK=`ef_find_awk`

    if [ -z "${EF_AWK}" ] ; then
        echo '<ef:error>'
        echo '  <ef:title>EF Portal Plug-in Error</ef:title>'
        echo '  <ef:message><![CDATA[Unable to find a valid version of AWK]]></ef:message>'
        echo "  <ef:command><![CDATA[${0//${EF_ROOT}/EF_ROOT}]]></ef:command>"
        echo '</ef:error>'
        exit 1
    fi

    AWK="${EF_AWK}"
    export EF_AWK AWK


    # ---[ HASH ]---

    unset EF_HASH

    EF_HASH=`ef_find_hash`

    if [ -z "${EF_HASH}" ] ; then
        echo '<ef:error>'
        echo '  <ef:title>EF Portal Plug-in Error</ef:title>'
        echo '  <ef:message><![CDATA[Unable to find a valid digest program]]></ef:message>'
        echo "  <ef:command><![CDATA[${0//${EF_ROOT}/EF_ROOT}]]></ef:command>"
        echo '</ef:error>'
        exit 1
    fi

    export EF_HASH


    # Load and fix REQUEST_URL
    fix_request_url
}
