#!/bin/bash

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

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


# --------------------------------------------------------------------------- #
# This function sets or deletes an EnginFrame session variable.
#
# Usage1: ef_session [--persistent] --name <name> [--xml|--text|--cdata (deprecated)] <content>
# Usage2: ef_session [--persistent] --action delete --name <name>
#
# Options:
# --action <value>:
#     Value of the action to perform. It could be one of these values:
#     * set: set the session variable <name> to <content> (in this case,
#       content must not be empty)
#     * delete | unset: unset the session variable <name>
#     * greedy: set the value of <name> to <content>
#       if content is empty or not defined, it acts like unset/delete action
# -n | --name <name>:
#     The name of the EF Session
# -p | --persistent:
#     Session variable will be saved in a persistent store
# -x | --xml <content>:
#     Option content is well formed XML, so it could be used as is.
# -t | --text <content>:
#     Option content is text plain, so it will be XML-escaped (default)
# --cdata <content>:
#     (DEPRECATED: the value is XML-escaped by default)
#     Option content will be enclosed in a CDATA section
# --------------------------------------------------------------------------- #

ef_session() {
    _input='text'
    _id=''
    _content=''
    _is_persistent='false'
    _action='greedy'

    while [ -n "$1" ]; do
        case "$1" in

            --action)
                _action="$2"
                shift
                ;;

            --cdata)
                _input='cdata'
                _content="$2"
                shift
                ;;

            -t|--text)
                _input='text'
                _content="$2"
                shift
                ;;

            -x|--xml)
                _input='xml'
                _content="$2"
                shift
                ;;

            -p|--persistent)
                _is_persistent='true'
                ;;

            -n|--name)
                _id="$2"
                shift
                ;;

            *)
                _content=$1
            ;;

        esac
        shift
    done

    # ID must exist
    if [ -z "$_id" ]; then
        echo "ef_session - missing ID" >&2
        exit 1
    fi

    # Action must be valid
    case "$_action" in
        greedy|set|delete|unset)
            ;;
        *)
            echo "ef_session - action '$_action' not recognized" >&2
            exit 1
            ;;
    esac

    if [ "$_action" = 'set' ]; then
        if [ -z "$_content" ]; then
            echo "ef_session - you must specify a content when you set a session variable" >&2
            exit 0
        fi
    fi


    # Modify content (if necessary)
    if [ -n "$_content" ]; then
        case "$_input" in
            cdata)
                _enc_content="<![CDATA[${_content}]]>"
                ;;
            xml)
                _enc_content="${_content}"
                ;;
            text|*)
                _enc_content="$(ef_xml_escape_content -i "${_content}")"
                ;;
        esac
    fi

    _enc_id="$(ef_xml_escape_attribute -i "${_id}")"
    echo "<ef:session xmlns:ef=\"${EF_XMLNS}\" persistent=\"${_is_persistent}\">"
    case "$_action" in
        set|greedy)
            echo "  <ef:option id=\"${_enc_id}\">${_enc_content}</ef:option>"
            ;;
        unset|delete)
            echo "  <ef:option id=\"${_enc_id}\"></ef:option>"
            ;;
        *)
            ;;
    esac
    echo '</ef:session>'
}
