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

interactive_shared_dir() {
    interactive_sessionUri="$1"
    _host_shared_root="$2"

    _sessionId=`basename "${interactive_sessionUri}"`
    _relative_path="${EF_USER}/${_sessionId}"

    if [ -z "${_host_shared_root}" ]; then
        echo "${interactive_sharedRoot}/${_relative_path}"
    else
        echo "${_host_shared_root}/${_relative_path}"
    fi
}

interactive_generate_jobscript() {
    # Sanity check
    [ -n "${interactive_os}" -a -n "${interactive_remote}" ] || return 1
    [ -r "${EF_ROOT}/plugins/interactive/lib/remote/${interactive_os}.jobscript.functions" ] || return 1
    [ -x "${EF_ROOT}/plugins/interactive/lib/remote/${interactive_remote}/generate.jobscript.${interactive_os}" ] || return 1

    # First emit function library for the specified operating system
    cat "${EF_ROOT}/plugins/interactive/lib/remote/${interactive_os}.jobscript.functions"

    # Add env variables starting with INTERACTIVE_SESSION_ prefix into the job's environment
    # It is required to export configuration variables from interactive.session.job.conf
    _regex='^(INTERACTIVE_SESSION_.*)$'
    _env="${_env:-$(compgen -v)}"
    for _var in ${_env}; do
        if [[ ${_var} =~ ${_regex} ]]; then
            _val="${!_var}"
            echo ${_var}=$(printf '%q' "${_val}")
        fi
    done

    # If a command scripts is specified, embed it
    if [ -n "${interactive_commandfile}" ]; then
        [ -r "${interactive_commandfile}" ] || return 1

        echo "EMBEDDED_COMMANDFILE=true"
        echo "##### begin embedded commandfile script #####"
        sed 's/^/#COMMANDFILE#/' "${interactive_commandfile}"
        echo "" # Ensure we leave a new line at the end of the interactive_commandfile
        echo "##### end embedded commandile script #####"
    fi

    # Then let each remote add stuff
    "${EF_ROOT}/plugins/interactive/lib/remote/${interactive_remote}/generate.jobscript.${interactive_os}"
}

interactive_generate_passwdfile() {
    if [ -x "${EF_ROOT}/plugins/interactive/lib/remote/${interactive_remote}/update.credentials" ]; then
        "${EF_ROOT}/plugins/interactive/lib/remote/${interactive_remote}/update.credentials"
    fi
}

# ex:ts=4:sw=4:et:ft=sh:
