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

#######################################
# Restart Dialog Wrapper in case of unintentional SIGINT.
# Globals:
#   VAR_DEBUG_TRAP
# Arguments:
#   1: var_helper_dialog
#######################################
restart_dialog() {
  [[ "${VAR_DEBUG_TRAP}" == "true" ]] && trap 'debug_trap' DEBUG
  trap 'trap_int' INT TERM
  trap 'trap_err "$?" "${BASH_SOURCE[0]}" "${LINENO}" "${FUNCNAME[0]:-main}" "${BASH_COMMAND}"' ERR
  case "$1" in
    box   ) dialog_box    ;;
    gauge ) dialog_gauge  ;;
    text  ) dialog_kernel ;;
    *     ) ;;
  esac
}

#######################################
# Trap function to be called on 'SIGINT'.
# Globals:
#   ERR_TRAPPED_SIG_INT
#   GRE
#   NL
#   RED
#   RES
#   VAR_IN_DIALOG_WR
# Arguments:
#   None
# Returns:
#   0: In case of unintentional SIGINT.
#######################################
trap_int() {
  case "${VAR_IN_DIALOG_WR}" in
    box   ) dialog_box_cleaner;   declare var_helper_dialog="box"   ;;
    gauge ) dialog_gauge_cleaner; declare var_helper_dialog="gauge" ;;
    text )  dialog_text_cleaner;  declare var_helper_dialog="text"  ;;
    *     ) declare var_helper_dialog="false" ;;
  esac

  trap - DEBUG ERR INT TERM

  declare answer
  if ! read -r -t 16 -p $'\n\e[93mCISS.debian.installer caught an INT.\e[0m \e[92mDo you want to abort the Installer? (y/N) \e[0m' answer; then
    printf "%bCISS.debian.installer caught an INT. No User confirmation after 16 seconds. Proceeding with Installer. %b%b" "${GRE}" "${RES}" "${NL}" >&2
    if [[ "${var_helper_dialog}" == "box" ]]; then
      restart_dialog "${var_helper_dialog}"
      return 0
    elif [[ "${var_helper_dialog}" == "gauge" ]]; then
      restart_dialog "${var_helper_dialog}"
      return 0
    elif [[ "${var_helper_dialog}" == "text" ]]; then
      restart_dialog "${var_helper_dialog}"
      return 0
    else
      restart_dialog "${var_helper_dialog}"
      return 0
    fi
  fi

  case "${answer,,}" in
    y|yes)
      printf "%bCISS.debian.installer caught an INT. SIGINT confirmed by User, exiting Installer. %b%b" "${RED}" "${RES}" "${NL}" >&2
      exit "${ERR_TRAPPED_SIG_INT}"
      ;;
    *)
      printf "%bCISS.debian.installer caught an INT. SIGINT NOT confirmed by User, proceeding with Installer. %b%b" "${GRE}" "${RES}" "${NL}" >&2
      if [[ "${var_helper_dialog}" == box ]]; then
        restart_dialog "${var_helper_dialog}"
        return 0
      elif [[ "${var_helper_dialog}" == gauge ]]; then
        restart_dialog "${var_helper_dialog}"
        return 0
      else
        restart_dialog "${var_helper_dialog}"
        return 0
      fi
      ;;
  esac
}
# vim: number et ts=2 sw=2 sts=2 ai tw=128 ft=sh
