#!/bin/sh

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

#-------------------------------------------------------------------------------
# This is a library of common functions used by the file processing scripts
#-------------------------------------------------------------------------------


# Source utility function libraries
. "${EF_ROOT}/plugins/ef/lib/utils"


# --------------------------------------------------------------------------- #
# EF_FILES_init
# =============
# Perform initialization of FILES library
#
# Arguments: none
# --------------------------------------------------------------------------- #

ef_files_init() {
    # Check for EF_FILES_EVAL_PATH (enable unless explicit disabled).
    if [ -z "${EF_FILES_EVAL_PATH}" ] ; then
        EF_FILES_EVAL_PATH="y"
    fi
}


# --------------------------------------------------------------------------- #
# EF_FILES_eval_path
# ==================
# Perform variable expansion in path through 'eval' function.
#
# Arguments:
# *) arguments to escape and evaluate
# --------------------------------------------------------------------------- #

ef_files_eval_path() {
    _safe_path=`ef_sh_sanitize "$@"`
    _expanded_path=`eval echo "$_safe_path"`
    echo "${_expanded_path}"
}


# --------------------------------------------------------------------------- #
# EF_FILES_list
# =============
# List content of current directory.
#
# Arguments: optional directory to whose contents to list
#
# Environment:
#            - _filter: regxp passed to egrep to filter output
#            - _target: file, directory, both, file@ignored, directory@ignored
#                       lists only files, only directories or both
#            - list_files: true/false, has precedence on _target
#            - listing_options_suffix: appended to file listing command
# --------------------------------------------------------------------------- #

ef_files_list() {
    # The 'l' option is used to list output; the 'L' option is used to dereference symbolic links
    _listing_options="-lLn${listing_options_suffix}"

    if [ -n "${_filter}" ] ; then
        _filter_shsafe=`ef_sh_sanitize_single "${_filter}"`
        _regex=" | egrep '${_filter_shsafe}'"
    fi

    _cmd="ls ${_listing_options}${_regex}"

    if [ -n "${use_iso_timestamp}" -a "${use_iso_timestamp}" = "true" ]
    then
        CURRENT_YEAR=`date '+%Y'`
        CURRENT_MONTHDAY=`date '+%m%d'`
        TIMEZONE=`date '+%z'`
    fi

    # ---[ ASCII Character ]---
    # Oct=001, Dec=1, Hex=01 - Char="SOH (start of heading)" - ctrl="^A"
    _ascii_001=`printf "\001"`

    # ---[ ASCII Character ]---
    # Oct=010, Dec=8, Hex=08 - Char="(BS  ‚Äö√Ñ√¥\b‚Äö√Ñ√¥ (backspace)" - ctrl="^H"
    _backspace=`printf "\b"`

    _my_awk=`ef_find_awk`

    (
    eval "${_cmd}" \
    | sed "s/[${_backspace}${_ascii_001}]//g" \
    | "${_my_awk}" \
        -v MYTIMEZONE="${TIMEZONE}" \
        -v CURRENT_YEAR="${CURRENT_YEAR}" \
        -v CURRENT_MONTHDAY="${CURRENT_MONTHDAY}" \
        -v list_files="${list_files}" \
        -v use_iso_timestamp="${use_iso_timestamp}" \
        -f "${EF_ROOT}/plugins/ef/lib/awk/utils.awk" \
        -f "${EF_ROOT}/plugins/ef/lib/files.awk"
    ) 2>/dev/null
}


# --------------------------------------------------------------------------- #
# EF_FILES_get_path
# =================
# Get and fix path, in this way:
# 1. read $1 from command line in _files_dir
# 2. call routing for variable expansion in _files_dir
# 3. if $_files_dir is defined and not a directory: try to use dirname
# 4. if $_files_dir is not a directory, try to use ${HOME}
#
# Arguments:
# 1) the initial path
# --------------------------------------------------------------------------- #

ef_files_get_path() {
    _files_dir="$1"
    # Perform variable expansion in path through 'eval'
    if [ "${EF_FILES_EVAL_PATH}" = 'y' ] ; then
        _files_dir=`ef_files_eval_path "$_files_dir"`
    fi

    # Check if $_files_dir is defined and it's a directory.
    if [ -n "${_files_dir}" -a ! -d "${_files_dir}" ] ; then
        _files_dir=`dirname "${_files_dir}"`
    fi

    # Check that $_files_dir now exists
    if [ ! -d "${_files_dir}" ] ; then
        _files_dir="$HOME"
    fi

    echo "$_files_dir"
}
