#!/bin/bash
# SPDX-Version: 3.0
# SPDX-CreationInfo: 2025-06-17; WEIDNER, Marc S.; <msw@coresecret.dev>
# SPDX-ExternalRef: GIT https://git.coresecret.dev/msw/CISS.debian.installer.git
# SPDX-FileContributor: WEIDNER, Marc S.; Centurion Intelligence Consulting Agency
# SPDX-FileCopyrightText: 2024-2025; WEIDNER, Marc S.; <msw@coresecret.dev>
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: EUPL-1.2 OR LicenseRef-CCLA-1.0
# SPDX-LicenseComment: This file is part of the CISS.debian.installer.secure framework.
# SPDX-PackageName: CISS.debian.installer
# SPDX-Security-Contact: security@coresecret.eu

guard_sourcing

declare -g VAR_LAST_CMD=""
declare -g __preexec_invoke=""
# shellcheck disable=SC2155
declare -grx LOG_DEBUG="/tmp/ciss_debian_installer_$$_debug.log"
### Generates empty LOG_DEBUG
touch "${LOG_DEBUG}" && chmod 0600 "${LOG_DEBUG}"
### Write Debug Log Header https://www.gnu.org/software/bash/manual/html_node/Bash-Variables
debug_trace_header "$#" "$*"
### Define patterns and mask replacements
declare -Ag MASK_PATTERNS=(
  ["API_KEY"]="********"
  ["api_key"]="********"
  ["password"]="********"
  ["priv_key"]="********"
  ["secret"]="********"
  ["token"]="********"
)

#######################################
# Wrapper to write to DEBUG Logfile.
# Globals:
#   BASH_LINENO
#   BASH_SOURCE
#   FUNCNAME
#   LOG_DEBUG
# Arguments:
#  None
#######################################
debug_trap_logger() {
  declare var_rc="$1"; shift
  declare var_msg="$*"
  # shellcheck disable=SC2155
  declare var_ts="$(date -u '+%Y-%m-%dT%H:%M:%S.%4N%z')"
  declare -i i=0
  for (( i=0; i<${#FUNCNAME[@]}; i++ )); do
    if [[ ${FUNCNAME[i]} != "debug_trap_logger" && \
          ${FUNCNAME[i]} != "debug_trap" ]]; then
      break
    fi
  done
  declare var_func="${FUNCNAME[i]:-MAIN}"
  declare var_line="${BASH_LINENO[i-1]:-?}"
  declare var_script="${BASH_SOURCE[i]:-${BASH_SOURCE[0]}}"
  declare var_script_rel="${var_script#"${PWD}"/}"
  printf '%s [%s:%s] %s() |%s| RC:%s \n' "${var_ts}" "${var_script_rel}" "${var_line}" "${var_func}" "${var_msg}" "${var_rc}">> "${LOG_DEBUG}"
}

#######################################
# Trap function to be called on 'DEBUG'.
# Globals:
#   BASH_COMMAND
#   MASK_PATTERNS
#   VAR_LAST_CMD
#   __preexec_invoke
# Arguments:
#   None
# Returns:
#   0: In all cases
#######################################
debug_trap() {
  declare rc="$?" var_cmd="${BASH_COMMAND}"

  ### Skip any xtrace output or explicit set -x/+x calls
  if [[ "${var_cmd}" == +* ]] || [[ "${var_cmd}" == "set -x" ]] || [[ "${var_cmd}" == "set +x" ]]; then
    return 0
  fi

  ### Mask sensitive substrings
  declare key
  for key in "${!MASK_PATTERNS[@]}"; do
    #if [[ "${var_cmd}" =~ ${key} ]]; then
    #  var_cmd="${var_cmd//${key}/${MASK_PATTERNS[${key}]}}"
    #fi
    case "${var_cmd}" in
      *"${key}"*) var_cmd="${var_cmd//${key}/${MASK_PATTERNS[${key}]}}" ;;
    esac
  done

  if [[ -n "${__preexec_invoke:-}" ]]; then
    debug_trap_logger "${rc}" "${VAR_LAST_CMD}"
    declare -g __preexec_invoke=""
  fi

  declare -g __preexec_invoke=1
  declare -g VAR_LAST_CMD="${var_cmd}"
  return 0
}
# vim: number et ts=2 sw=2 sts=2 ai tw=128 ft=sh
