#!/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

#######################################
# Print Error Message for Trap on 'ERR' in '${ERROR_LOG}'.
# Globals:
#   BASHOPTS
#   BASH_VERSINFO
#   EPOCHREALTIME
#   ERRCMMD
#   ERRCODE
#   ERRFUNC
#   ERRLINE
#   ERRSCRT
#   EUID
#   HOSTNAME
#   LOG_DBG
#   LOG_ERR
#   LOG_TRC
#   LOG_VAR
#   NL
#   SHELLOPTS
#   UID
#   VAR_ARG_SANITIZED
#   VAR_DEBUG_TRACE
#   VAR_DEBUG_TRAP
#   VAR_GIT_REL
#   VAR_PARAM_COUNT
#   VAR_PARAM_STRNG
#   VAR_RESOURCES
#   VAR_SCRIPT_RUNTIME
#   VAR_SYSTEM
#   VAR_VERSION
# Arguments:
#   None
#######################################
print_file_err() {
  {
    printf "❌ Trap on 'ERR'       : CISS.debian.installer Script failed. %b" "${NL}"
    printf "❌ GIT Commit          : %s %b" "${VAR_GIT_REL}" "${NL}"
    printf "❌ Version             : %s %b" "${VAR_VERSION}" "${NL}"
    printf "❌ Epoch               : %s %b" "${EPOCHREALTIME}" "${NL}"
    printf "❌ Bash MAJ Release    : %s %b" "${BASH_VERSINFO[0]}" "${NL}"
    printf "❌ Bash MIN Version    : %s %b" "${BASH_VERSINFO[1]}" "${NL}"
    printf "❌ Bash Patch Level    : %s %b" "${BASH_VERSINFO[2]}" "${NL}"
    printf "❌ Bash Build Version  : %s %b" "${BASH_VERSINFO[3]}" "${NL}"
    printf "❌ Bash Release        : %s %b" "${BASH_VERSINFO[4]}" "${NL}"
    printf "❌ UID                 : %s %b" "${UID}" "${NL}"
    printf "❌ EUID                : %s %b" "${EUID}" "${NL}"
    printf "❌ Hostname            : %s %b" "${HOSTNAME}" "${NL}"
    printf "❌ Hostsystem          : %s %b" "${VAR_SYSTEM}" "${NL}"
    printf "❌ Error               : %s %b" "${ERRCODE}" "${NL}"
    printf "❌ Line                : %s %b" "${ERRLINE}" "${NL}"
    printf "❌ Script              : %s %b" "${ERRSCRT}" "${NL}"
    printf "❌ Function            : %s %b" "${ERRFUNC}" "${NL}"
    printf "❌ Command             : %s %b" "${ERRCMMD}" "${NL}"
    printf "❌ Script PID          : %s %b" "${$}" "${NL}"
    printf "❌ Script Runtime      : %s %b" "${VAR_SCRIPT_RUNTIME}" "${NL}"
    printf "❌ System Resources    : %s %b" "${VAR_RESOURCES}" "${NL}"
    printf "❌ Arguments Counter   : %s %b" "${VAR_PARAM_COUNT}" "${NL}"
    printf "❌ Arguments Original  : %s %b" "${VAR_PARAM_STRNG}" "${NL}"
    printf "❌ Arguments Sanitized : %s %b" "${VAR_ARG_SANITIZED}" "${NL}"
    printf "❌ BASHOPTS            : %s %b" "${BASHOPTS}" "${NL}"
    printf "❌ SHELLOPTS           : %s %b" "${SHELLOPTS}" "${NL}"
    if [[ "${VAR_DEBUG_TRACE}" == "true" || "${VAR_DEBUG_TRAP}" == "true" ]]; then
      printf "❌ Vars Dump saved at  : %s %b" "${LOG_VAR}" "${NL}"
      printf "❌               less -R %s %b" "${LOG_VAR}" "${NL}"
    fi
    if [[ "${VAR_DEBUG_TRAP}" == "true" ]]; then
      printf "❌ DEBUG Log saved at  : %s %b" "${LOG_DBG}" "${NL}"
      printf "❌               less -R %s %b" "${LOG_DBG}" "${NL}"
    fi
    if [[ "${VAR_DEBUG_TRACE}" == "true" ]]; then
      printf "❌ TRACE Log saved at  : %s %b" "${LOG_TRC}" "${NL}"
      printf "❌               less -R %s %b" "${LOG_TRC}" "${NL}"
    fi
    printf "%b" "${NL}"
  } >> "${LOG_ERR}"
}

#######################################
# Print Error Message for Trap on 'ERR' on Terminal.
# Globals:
#   BASH_VERSINFO
#   EPOCHREALTIME
#   ERRCMMD
#   ERRCODE
#   ERRFUNC
#   ERRLINE
#   ERRSCRT
#   EUID
#   HOSTNAME
#   LOG_DBG
#   LOG_ERR
#   LOG_TRC
#   LOG_VAR
#   NL
#   RED
#   RES
#   SHELLOPTS
#   UID
#   VAR_ARG_SANITIZED
#   VAR_DEBUG_TRACE
#   VAR_DEBUG_TRAP
#   VAR_GIT_REL
#   VAR_PARAM_COUNT
#   VAR_PARAM_STRNG
#   VAR_RESOURCES
#   VAR_SCRIPT_RUNTIME
#   VAR_SYSTEM
#   VAR_VERSION
# Arguments:
#   None
#######################################
print_scr_err() {
  printf "%b❌ Trap on 'ERR'       : CISS.debian.installer Script failed. %b%b" "${RED}" "${RES}" "${NL}"
  printf "%b❌ GIT Commit          : %s %b%b" "${RED}" "${VAR_GIT_REL}" "${RES}" "${NL}"
  printf "%b❌ Version             : %s %b%b" "${RED}" "${VAR_VERSION}" "${RES}" "${NL}"
  printf "%b❌ Epoch               : %s %b%b" "${RED}" "${EPOCHREALTIME}" "${RES}" "${NL}"
  printf "%b❌ Bash MAJ Release    : %s %b%b" "${RED}" "${BASH_VERSINFO[0]}" "${RES}" "${NL}"
  printf "%b❌ Bash MIN Version    : %s %b%b" "${RED}" "${BASH_VERSINFO[1]}" "${RES}" "${NL}"
  printf "%b❌ Bash Patch Level    : %s %b%b" "${RED}" "${BASH_VERSINFO[2]}" "${RES}" "${NL}"
  printf "%b❌ Bash Build Version  : %s %b%b" "${RED}" "${BASH_VERSINFO[3]}" "${RES}" "${NL}"
  printf "%b❌ Bash Release        : %s %b%b" "${RED}" "${BASH_VERSINFO[4]}" "${RES}" "${NL}"
  printf "%b❌ UID                 : %s %b%b" "${RED}" "${UID}" "${RES}" "${NL}"
  printf "%b❌ EUID                : %s %b%b" "${RED}" "${EUID}" "${RES}" "${NL}"
  printf "%b❌ Hostname            : %s %b%b" "${RED}" "${HOSTNAME}" "${RES}" "${NL}"
  printf "%b❌ Hostsystem          : %s %b%b" "${RED}" "${VAR_SYSTEM}" "${RES}" "${NL}"
  printf "%b❌ Error               : %s %b%b" "${RED}" "${ERRCODE}" "${RES}" "${NL}"
  printf "%b❌ Line                : %s %b%b" "${RED}" "${ERRLINE}" "${RES}" "${NL}"
  printf "%b❌ Script              : %s %b%b" "${RED}" "${ERRSCRT}" "${RES}" "${NL}"
  printf "%b❌ Function            : %s %b%b" "${RED}" "${ERRFUNC}" "${RES}" "${NL}"
  printf "%b❌ Command             : %s %b%b" "${RED}" "${ERRCMMD}" "${RES}" "${NL}"
  printf "%b❌ Script PID          : %s %b%b" "${RED}" "${$}" "${RES}" "${NL}"
  printf "%b❌ Script Runtime      : %s %b%b" "${RED}" "${VAR_SCRIPT_RUNTIME}" "${RES}" "${NL}"
  printf "%b❌ System Resources    : %s %b%b" "${RED}" "${VAR_RESOURCES}" "${RES}" "${NL}"
  printf "%b❌ Arguments Counter   : %s %b%b" "${RED}" "${VAR_PARAM_COUNT}" "${RES}" "${NL}"
  printf "%b❌ Arguments Original  : %s %b%b" "${RED}" "${VAR_PARAM_STRNG}" "${RES}" "${NL}"
  printf "%b❌ Arguments Sanitized : %s %b%b" "${RED}" "${VAR_ARG_SANITIZED}" "${RES}" "${NL}"
  printf "%b❌ SHELLOPTS           : %s %b%b" "${RED}" "${SHELLOPTS}" "${RES}" "${NL}"
  printf "%b❌ Error Log saved at  : %s %b%b" "${RED}" "${LOG_ERR}" "${RES}" "${NL}"
  printf "%b❌                   cat %s %b%b" "${RED}" "${LOG_ERR}" "${RES}" "${NL}"
  if [[ "${VAR_DEBUG_TRACE}" == "true" || "${VAR_DEBUG_TRAP}" == "true" ]]; then
    printf "%b❌ Vars Dump saved at  : %s %b%b" "${RED}" "${LOG_VAR}" "${RES}" "${NL}"
    printf "%b❌               less -R %s %b%b" "${RED}" "${LOG_VAR}" "${RES}" "${NL}"
  fi
  if [[ "${VAR_DEBUG_TRAP}" == "true" ]]; then
    printf "%b❌ Debug Log saved at  : %s %b%b" "${RED}" "${LOG_DBG}" "${RES}" "${NL}"
    printf "%b❌               less -R %s %b%b" "${RED}" "${LOG_DBG}" "${RES}" "${NL}"
  fi
  if [[ "${VAR_DEBUG_TRACE}" == "true" ]]; then
    printf "%b❌ Trace Log saved at  : %s %b%b" "${RED}" "${LOG_TRC}" "${RES}" "${NL}"
    printf "%b❌               less -R %s %b%b" "${RED}" "${LOG_TRC}" "${RES}" "${NL}"
  fi
  print_stacktrace
  printf "%b" "${NL}"
}

#######################################
# Print function() stacktrace.
# Globals:
#   BASH_LINENO
#   BASH_SOURCE
#   FUNCNAME
#   NL
#   RED
#   RES
# Arguments:
#   None
#######################################
print_stacktrace() {
  declare -i i=0
  printf "%b❌ %b%b" "${RED}" "${RES}" "${NL}"
  printf "%b❌ Raw Stacktrace      : %b%b" "${RED}" "${RES}" "${NL}"
  for ((i=0; i<${#FUNCNAME[@]}-1; i++)); do
    if (( i == 0 )); then
      printf "%b❌   ➥ %s() at: [%s:%s] %b%b" "${RED}" "${FUNCNAME[i]}" "${BASH_SOURCE[i]}" "${LINENO:-?}" "${RES}" "${NL}"
    else
      printf "%b❌   ➥ %s() at: [%s:%s] %b%b" "${RED}" "${FUNCNAME[i]}" "${BASH_SOURCE[i]}" "${BASH_LINENO[i-1]:-?}" "${RES}" "${NL}"
    fi
  done
  printf "%b" "${NL}"
}

#######################################
# Trap function to be called on 'ERR'.
# Globals:
#   ERRCMMD
#   ERRCODE
#   ERRFUNC
#   ERRLINE
#   ERRSCRT
#   ERRTRAP
#   VAR_DEBUG_TRACE
#   VAR_DEBUG_TRAP
#   VAR_IN_DIALOG_WR
# Arguments:
#   1: "$?"
#   2: "${BASH_SOURCE[0]}"
#   3: "${LINENO}"
#   4: "${FUNCNAME[0]:-main}"
#   5: "${BASH_COMMAND}"
#######################################
trap_err() {
  declare -g ERRCODE="$1"
  declare -g ERRSCRT="$2"
  declare -g ERRLINE="$3"
  declare -g ERRFUNC="$4"
  declare -g ERRCMMD="$5"
  # shellcheck disable=SC2034
  declare -g ERRTRAP="true"

  trap - DEBUG ERR INT TERM

  if [[ "${VAR_DEBUG_TRACE}" == "true" || "${VAR_DEBUG_TRAP}" == "true" ]]; then dump_vars_exiting; fi

  case "${VAR_IN_DIALOG_WR}" in
    box   ) dialog_box_cleaner   ;;
    gauge ) dialog_gauge_cleaner ;;
    text )  dialog_text_cleaner  ;;
  esac

  calculate_runtime
  print_file_err
  print_scr_err
}
# vim: number et ts=2 sw=2 sts=2 ai tw=128 ft=sh
