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

#######################################
# Print Error Message for Trap on 'ERR' in '${ERROR_LOG}'.
# Globals:
#   ARGUMENTS_COUNT
#   ARG_STR_ORG_INPUT
#   ERRCMMD
#   ERRCODE
#   ERRFUNC
#   ERRLINE
#   ERRSCRT
#   LOG_DEBUG
#   LOG_ERROR
#   LOG_TRACE
#   LOG_VAR
#   SECONDS
#   VAR_ARG_SANITIZED
#   VAR_DEBUG_TRACE
#   VAR_DEBUG_TRAP
#   VAR_GIT_HEAD
#   VAR_SYSTEM
#   VAR_VERSION
# Arguments:
#   None
#######################################
print_file_err() {
  {
    printf "❌ CISS.debian.installer Script failed. \n"
    printf "❌ GIT Commit          : %s \n" "${VAR_GIT_HEAD}"
    printf "❌ Version             : %s \n" "${VAR_VERSION}"
    printf "❌ Hostsystem          : %s \n" "${VAR_SYSTEM}"
    printf "❌ Error               : %s \n" "${ERRCODE}"
    printf "❌ Line                : %s \n" "${ERRLINE}"
    printf "❌ Script              : %s \n" "${ERRSCRT}"
    printf "❌ Function            : %s \n" "${ERRFUNC}"
    printf "❌ Command             : %s \n" "${ERRCMMD}"
    printf "❌ Script PID          : %s \n" "${$}"
    printf "❌ Script Runtime      : %s \n" "${SECONDS}"
    printf "❌ Arguments Counter   : %s \n" "${ARGUMENTS_COUNT}"
    printf "❌ Arguments Original  : %s \n" "${ARG_STR_ORG_INPUT}"
    printf "❌ Arguments Sanitized : %s \n" "${VAR_ARG_SANITIZED}"
    if [[ "${VAR_DEBUG_TRACE}" || "${VAR_DEBUG_TRAP}" ]]; then
      printf "❌ Vars Dump saved at  : %s \n" "${LOG_VAR}"
    fi
    if "${VAR_DEBUG_TRAP}"; then
      printf "❌ DEBUG Log saved at  : %s \n" "${LOG_DEBUG}"
      printf "❌                   cat %s \n" "${LOG_DEBUG}"
    fi
    if "${VAR_DEBUG_TRACE}"; then
      printf "❌ TRACE Log saved at  : %s \n" "${LOG_TRACE}"
      printf "❌                   cat %s \n" "${LOG_TRACE}"
    fi
    printf "\n"
  } >> "${LOG_ERROR}"
}

#######################################
# Print Error Message for Trap on 'ERR' on Terminal.
# Globals:
#   ARGUMENTS_COUNT
#   ARG_STR_ORG_INPUT
#   ERRCMMD
#   ERRCODE
#   ERRFUNC
#   ERRLINE
#   ERRSCRT
#   LOG_DEBUG
#   LOG_ERROR
#   LOG_TRACE
#   LOG_VAR
#   SECONDS
#   VAR_ARG_SANITIZED
#   VAR_DEBUG_TRACE
#   VAR_DEBUG_TRAP
#   VAR_GIT_HEAD
#   VAR_SYSTEM
#   VAR_VERSION
# Arguments:
#  None
#######################################
print_scr_err() {
  printf "\e[91m❌ CISS.debian.installer Script failed. \e[0m\n" >&2
  printf "\e[91m❌ GIT Commit          : %s \e[0m\n" "${VAR_GIT_HEAD}" >&2
  printf "\e[91m❌ Version             : %s \e[0m\n" "${VAR_VERSION}" >&2
  printf "\e[91m❌ Hostsystem          : %s \e[0m\n" "${VAR_SYSTEM}" >&2
  printf "\e[91m❌ Error               : %s \e[0m\n" "${ERRCODE}" >&2
  printf "\e[91m❌ Line                : %s \e[0m\n" "${ERRLINE}" >&2
  printf "\e[91m❌ Script              : %s \e[0m\n" "${ERRSCRT}" >&2
  printf "\e[91m❌ Function            : %s \e[0m\n" "${ERRFUNC}" >&2
  printf "\e[91m❌ Command             : %s \e[0m\n" "${ERRCMMD}" >&2
  printf "\e[91m❌ Script PID          : %s \e[0m\n" "${$}" >&2
  printf "\e[91m❌ Script Runtime      : %s \e[0m\n" "${SECONDS}" >&2
  printf "\e[91m❌ Arguments Counter   : %s \e[0m\n" "${ARGUMENTS_COUNT}" >&2
  printf "\e[91m❌ Arguments Original  : %s \e[0m\n" "${ARG_STR_ORG_INPUT}" >&2
  printf "\e[91m❌ Arguments Sanitized : %s \e[0m\n" "${VAR_ARG_SANITIZED}" >&2
  printf "\e[91m❌ Error Log saved at  : %s \e[0m\n" "${LOG_ERROR}" >&2
  printf "\e[91m❌                   cat %s \e[0m\n" "${LOG_ERROR}" >&2
  if [[ "${VAR_DEBUG_TRACE}" || "${VAR_DEBUG_TRAP}" ]]; then
    printf "\e[91m❌ Vars Dump saved at  : %s \e[0m\n" "${LOG_VAR}" >&2
  fi
  if "${VAR_DEBUG_TRAP}"; then
    printf "\e[91m❌ Debug Log saved at  : %s \e[0m\n" "${LOG_DEBUG}" >&2
    printf "\e[91m❌                   cat %s \e[0m\n" "${LOG_DEBUG}" >&2
  fi
  if "${VAR_DEBUG_TRACE}"; then
    printf "\e[91m❌ Debug Log saved at  : %s \e[0m\n" "${LOG_TRACE}" >&2
    printf "\e[91m❌                   cat %s \e[0m\n" "${LOG_TRACE}" >&2
  fi
  printf "\n"
}

#######################################
# Trap function to be called on 'ERR'.
# Globals:
#   ERRCMMD
#   ERRCODE
#   ERRFUNC
#   ERRLINE
#   ERRSCRT
#   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() {
  trap - DEBUG ERR INT
  declare -g ERRCODE="$1"
  declare -g ERRSCRT="$2"
  declare -g ERRLINE="$3"
  declare -g ERRFUNC="$4"
  declare -g ERRCMMD="$5"
  if [[ "${VAR_DEBUG_TRACE}" || "${VAR_DEBUG_TRAP}" ]]; then dump_vars_exiting; fi
  case "${VAR_IN_DIALOG_WR}" in
    box   ) dialog_box_cleaner   ;;
    gauge ) dialog_gauge_cleaner ;;
  esac
  print_file_err
  print_scr_err
}
# vim: number et ts=2 sw=2 sts=2 ai tw=128 ft=sh
