#!/bin/bash
# SPDX-Version: 3.0
# SPDX-CreationInfo: 2025-05-05; WEIDNER, Marc S.; <msw@coresecret.dev>
# SPDX-ExternalRef: GIT https://git.coresecret.dev/msw/CISS.debian.live.builder.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.live.builder
# SPDX-Security-Contact: security@coresecret.eu

guard_sourcing

#######################################
# Trap function to be called on 'EXIT'.
# Handles also Errors like unbound variables, as they are not caught by Trap on 'ERR'.
# Globals:
#   VAR_LAST_CMD
#   __preexec_invoke
# Arguments:
#   1: "$?"
#   2: "${BASH_SOURCE[0]}"
#   3: "${LINENO}"
#   4: "${FUNCNAME[0]:-main}"
#   5: "${BASH_COMMAND}"
#######################################
trap_exit() {
  declare -r var_exit_code="$1"
  declare -r var_exit_scrt="$2"
  declare -r var_exit_line="$3"
  declare -r var_exit_func="$4"
  declare -r var_exit_cmmd="$5"

  trap - DEBUG ERR EXIT INT TERM

  if [[ -n "${__preexec_invoke:-}" ]]; then
    debug_trap_logger "${var_exit_code}" "${VAR_LAST_CMD}"
    unset __preexec_invoke
  fi

  if (( var_exit_code == 0 )); then
    trap_exit_zero "${var_exit_code}"
  else
    trap_exit_non_zero "${var_exit_code}" "${var_exit_scrt}" "${var_exit_line}" "${var_exit_func}" "${var_exit_cmmd}"
  fi
}

#######################################
# Trap on Exit Handler for '0' Exit-Code.
# Globals:
#   GRE
#   LOG_DBG
#   LOG_TRC
#   LOG_VAR
#   MAG
#   NL
#   RES
#   VAR_DEBUG_TRACE
#   VAR_DEBUG_TRAP
#   VAR_SCRIPT_RUNTIME
#   VAR_SCRIPT_SUCCESS
# Arguments:
#   1: "$?" passed through by trap_exit()
#######################################
trap_exit_zero() {
  declare -r var_trap_exit_zero_code="$1"

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

  calculate_runtime

  if [[ "${VAR_SCRIPT_SUCCESS}" == "true" ]]; then
    printf "%b" "${NL}"
    printf "%b✅ CISS.debian.installer Script successful. %s%s" "${GRE}" "${RES}" "${NL}"
    printf "%b✅ Exited with Status : %s %b%b" "${GRE}" "${var_trap_exit_zero_code}" "${RES}" "${NL}"
    printf "%b✅ Script Runtime     : %s %b%b" "${GRE}" "${VAR_SCRIPT_RUNTIME}" "${RES}" "${NL}"
    printf "%b" "${NL}"
    if [[ "${VAR_DEBUG_TRACE}" == "true" || "${VAR_DEBUG_TRAP}" == "true" ]]; then
      printf "%b✅ Vars Dump saved at : %s %b%b" "${GRE}" "${LOG_VAR}" "${RES}" "${NL}"
      printf "%b✅                  cat %s %b%b" "${GRE}" "${LOG_VAR}" "${RES}" "${NL}"
      printf "%b✅              less -R %s %b%b" "${GRE}" "${LOG_VAR}" "${RES}" "${NL}"
    fi
    if [[ "${VAR_DEBUG_TRAP}" == "true" ]]; then
      printf "%b✅ DEBUG Log saved at : %s %b%b" "${GRE}" "${LOG_DBG}" "${RES}" "${NL}"
      printf "%b✅                  cat %s %b%b" "${GRE}" "${LOG_DBG}" "${RES}" "${NL}"
      printf "%b✅              less -R %s %b%b" "${GRE}" "${LOG_DBG}" "${RES}" "${NL}"
    fi
    if [[ "${VAR_DEBUG_TRACE}" == "true" ]]; then
      printf "%b✅ TRACE Log saved at : %s %b%b" "${GRE}" "${LOG_TRC}" "${RES}" "${NL}"
      printf "%b✅                  cat %s %b%b" "${GRE}" "${LOG_TRC}" "${RES}" "${NL}"
      printf "%b✅              less -R %s %b%b" "${GRE}" "${LOG_TRC}" "${RES}" "${NL}"
    fi
    printf "%b" "${NL}"
    printf "%b💷 Please consider donating to my work at: %b%b" "${MAG}" "${RES}" "${NL}"
    printf "%b🔗 https://coresecret.eu/spenden/          %b%b" "${MAG}" "${RES}" "${NL}"
    printf "%b" "${NL}"
  fi
  exit "${var_trap_exit_zero_code}"
}

#######################################
# Trap on Exit Handler for 'Non-0' Exit-Code.
# Globals:
#   BASHOPTS
#   BASH_VERSINFO
#   EPOCHREALTIME
#   ERRTRAP
#   EUID
#   HOSTNAME
#   LOG_DBG
#   LOG_EXT
#   LOG_TRC
#   LOG_VAR
#   NL
#   RED
#   RES
#   SHELLOPTS
#   UID
#   VAR_ARG_SANITIZED
#   VAR_DEBUG_TRACE
#   VAR_DEBUG_TRAP
#   VAR_GIT_REL
#   VAR_IN_DIALOG_WR
#   VAR_PARAM_COUNT
#   VAR_PARAM_STRNG
#   VAR_RESOURCES
#   VAR_SCRIPT_RUNTIME
#   VAR_SYSTEM
#   VAR_VERSION
# Arguments:
#   1: "$?" passed through by trap_exit()
#   2: "${BASH_SOURCE[0]}" passed through by trap_exit()
#   3: "${LINENO}" passed through by trap_exit()
#   4: "${FUNCNAME[0]:-main}" passed through by trap_exit()
#   5: "${BASH_COMMAND}" passed through by trap_exit()
#######################################
trap_exit_non_zero() {
  declare -r var_code="$1"
  declare -r var_scrt="$2"
  declare -r var_line="$3"
  declare -r var_func="$4"
  declare -r var_cmmd="$5"

  clean_up "${var_code}"

  if [[ "${ERRTRAP}" == "false" ]]; then

    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

    printf "%b❌ Trap on 'EXIT'      : CISS.debian.installer Script failed. %b%b" "${RED}" "${RES}" "${NL}" | tee -a "${LOG_EXT}"
    printf "%b❌                     : This was most probably caused by an unbound variable. %b%b" "${RED}" "${RES}" "${NL}" | tee -a "${LOG_EXT}"
    printf "%b❌ GIT Commit          : %s %b%b" "${RED}" "${VAR_GIT_REL}" "${RES}" "${NL}" | tee -a "${LOG_EXT}"
    printf "%b❌ Version             : %s %b%b" "${RED}" "${VAR_VERSION}" "${RES}" "${NL}" | tee -a "${LOG_EXT}"
    printf "%b❌ Epoch               : %s %b%b" "${RED}" "${EPOCHREALTIME}" "${RES}" "${NL}" | tee -a "${LOG_EXT}"
    printf "%b❌ Bash MAJ Release    : %s %b%b" "${RED}" "${BASH_VERSINFO[0]}" "${RES}" "${NL}" | tee -a "${LOG_EXT}"
    printf "%b❌ Bash MIN Version    : %s %b%b" "${RED}" "${BASH_VERSINFO[1]}" "${RES}" "${NL}" | tee -a "${LOG_EXT}"
    printf "%b❌ Bash Patch Level    : %s %b%b" "${RED}" "${BASH_VERSINFO[2]}" "${RES}" "${NL}" | tee -a "${LOG_EXT}"
    printf "%b❌ UID                 : %s %b%b" "${RED}" "${UID}" "${RES}" "${NL}" | tee -a "${LOG_EXT}"
    printf "%b❌ EUID                : %s %b%b" "${RED}" "${EUID}" "${RES}" "${NL}" | tee -a "${LOG_EXT}"
    printf "%b❌ Hostname            : %s %b%b" "${RED}" "${HOSTNAME}" "${RES}" "${NL}" | tee -a "${LOG_EXT}"
    printf "%b❌ Hostsystem          : %s %b%b" "${RED}" "${VAR_SYSTEM}" "${RES}" "${NL}" | tee -a "${LOG_EXT}"
    printf "%b❌ Error               : %s %b%b" "${RED}" "${var_code}" "${RES}" "${NL}" | tee -a "${LOG_EXT}"
    printf "%b❌ Line                : %s %b%b" "${RED}" "${var_line}" "${RES}" "${NL}" | tee -a "${LOG_EXT}"
    printf "%b❌ Script              : %s %b%b" "${RED}" "${var_scrt}" "${RES}" "${NL}" | tee -a "${LOG_EXT}"
    printf "%b❌ Function            : %s %b%b" "${RED}" "${var_func}" "${RES}" "${NL}" | tee -a "${LOG_EXT}"
    printf "%b❌ Command             : %s %b%b" "${RED}" "${var_cmmd}" "${RES}" "${NL}" | tee -a "${LOG_EXT}"
    printf "%b❌ Script PID          : %s %b%b" "${RED}" "${$}" "${RES}" "${NL}" | tee -a "${LOG_EXT}"
    printf "%b❌ Script Runtime      : %s %b%b" "${RED}" "${VAR_SCRIPT_RUNTIME}" "${RES}" "${NL}" | tee -a "${LOG_EXT}"
    printf "%b❌ System Resources    : %s %b%b" "${RED}" "${VAR_RESOURCES}" "${RES}" "${NL}" | tee -a "${LOG_EXT}"
    printf "%b❌ Arguments Counter   : %s %b%b" "${RED}" "${VAR_PARAM_COUNT}" "${RES}" "${NL}" | tee -a "${LOG_EXT}"
    printf "%b❌ Arguments Original  : %s %b%b" "${RED}" "${VAR_PARAM_STRNG}" "${RES}" "${NL}" | tee -a "${LOG_EXT}"
    printf "%b❌ Arguments Sanitized : %s %b%b" "${RED}" "${VAR_ARG_SANITIZED}" "${RES}" "${NL}" | tee -a "${LOG_EXT}"
    printf "%b❌ BASHOPTS            : %s %b%b" "${RED}" "${BASHOPTS}" "${RES}" "${NL}" >> "${LOG_EXT}"
    printf "%b❌ SHELLOPTS           : %s %b%b" "${RED}" "${SHELLOPTS}" "${RES}" "${NL}" | tee -a "${LOG_EXT}"
    printf "%b❌ Error Log saved at  : %s %b%b" "${RED}" "${LOG_EXT}" "${RES}" "${NL}" | tee -a "${LOG_EXT}"
    printf "%b❌                   cat %s %b%b" "${RED}" "${LOG_EXT}" "${RES}" "${NL}" | tee -a "${LOG_EXT}"
    if [[ "${VAR_DEBUG_TRACE}" == "true" || "${VAR_DEBUG_TRAP}" == "true" ]]; then
      printf "%b❌ Vars Dump saved at  : %s %b%b" "${RED}" "${LOG_VAR}" "${RES}" "${NL}" | tee -a "${LOG_EXT}"
      printf "%b❌               less -R %s %b%b" "${RED}" "${LOG_VAR}" "${RES}" "${NL}" | tee -a "${LOG_EXT}"
    fi
    if [[ "${VAR_DEBUG_TRAP}" == "true" ]]; then
      printf "%b❌ Debug Log saved at  : %s %b%b" "${RED}" "${LOG_DBG}" "${RES}" "${NL}" | tee -a "${LOG_EXT}"
      printf "%b❌               less -R %s %b%b" "${RED}" "${LOG_DBG}" "${RES}" "${NL}" | tee -a "${LOG_EXT}"
    fi
    if [[ "${VAR_DEBUG_TRACE}" == "true" ]]; then
      printf "%b❌ Trace Log saved at  : %s %b%b" "${RED}" "${LOG_TRC}" "${RES}" "${NL}" | tee -a "${LOG_EXT}"
      printf "%b❌               less -R %s %b%b" "${RED}" "${LOG_TRC}" "${RES}" "${NL}" | tee -a "${LOG_EXT}"
    fi
    print_stacktrace
  fi

  exit "${var_code}"
}
# vim: number et ts=2 sw=2 sts=2 ai tw=128 ft=sh
