#!/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/ef/lib/utils"

#-------------------------------------------------------------------------------
# parses ouput of ls (or a command providing similar data) given on stdin and
# produces the corresponding xml
#-------------------------------------------------------------------------------

while [ $# -gt 0 ]; do
  case "$1" in
  --vroot|--path|--target|--sortby|--filter|--sorted|--filtered)
    _var=`echo $1 | sed 's/^--//'`
    eval "${_var}=\"\$2\""
    shift; shift
    ;;
  *)
    shift
    ;;
  esac
done

CURRENT_YEAR=`date '+%Y'`
CURRENT_MONTHDAY=`date '+%m%d'`

# must be in the format +NN:MM but date '+%:z' is not portable
TIMEZONE=`date '+%z' | sed 's/\([0-9]\{2\}$\)/:\1/'`

# ---[ 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"`

echo '<fm:items'
echo '  xmlns:fm="http://www.enginframe.com/2009/fm"'
echo "  vroot=\"$(ef_xml_escape_attribute -i "${vroot}")\""
echo "  path=\"$(ef_xml_escape_attribute -i "${path}")\""
if [ -n "${sortby}" ]; then
    echo "  sort-by=\"$(ef_xml_escape_attribute -i "${sortby}")\""
fi
if [ -n "${filter}" ]; then
    echo "  filter=\"$(ef_xml_escape_attribute -i "${filter}")\""
fi
echo "  sorted=\"$(ef_xml_escape_attribute -i "${sorted}")\""
echo "  filtered=\"$(ef_xml_escape_attribute -i "${filtered}")\""
echo '  >'

if [ "${FM_VROOT_SCHEME}" = "s3" ]; then
    while read -r modified itemSize key; do
        [[ ${key} == */ ]] && itemType='folder' || itemType='file'
        # Exclude the folder we are browsing looking for its specific signature
        [[ ${itemType} == folder ]] && [[ ${modified:-None} != None ]] && [[ ${itemSize} -eq 0 ]] && continue
        if [[ ${itemType} == folder ]] || [[ ${target} == both ]] || [[ ${target} == file ]]; then
            itemName="$(ef_xml_escape_attribute -i "$(basename "${key}")")"
            if [[ ${modified:-None} == None ]]; then
                itemDate=''
            else
                itemDate="$(date --iso-8601=seconds --date "${modified}")"
            fi
            printf '<fm:item type="%s" modified="%s" size="%s" name="%s"/>\n' \
                   "${itemType}" \
                   "${itemDate}" \
                   "${itemSize}" \
                   "${itemName}"
        fi
    done
else
    _my_awk=$(ef_find_awk)

    (
    cat - | \
    sed "s/[${_backspace}${_ascii_001}]//g" | \
    "${_my_awk}" \
        -v MYTIMEZONE="${TIMEZONE}" \
        -v CURRENT_YEAR="${CURRENT_YEAR}" \
        -v CURRENT_MONTHDAY="${CURRENT_MONTHDAY}" \
        -v target="${target}" \
        -f "${EF_ROOT}/plugins/ef/lib/awk/utils.awk" \
        -f "${EF_ROOT}/plugins/fm/lib/ls2xml.awk"
    )
fi

echo '</fm:items>'


# --------------------------------------------------------------------------- #
# vi: ts=4 sw=4 et syntax=sh :
# --------------------------------------------------------------------------- #

