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

#######################################
# Terminal cleaner for Dialog Wrappers.
# Arguments:
#   None
#######################################
clean_screen() {
  tput cnorm > /dev/tty # Cursor visible
  tput sgr0  > /dev/tty # Attributes off
  stty sane  < /dev/tty # Sane modes
  tput rmcup > /dev/tty # Back to the main buffer
  clear      > /dev/tty # Clear residual
  #lines=$(tput lines)
  #tput cup $((lines-1)) 0 > /dev/tty
  #printf "\n" > /dev/tty
}

#######################################
# Wrapper for Dialog Gauge Bar.
# Globals:
#   PID_DIALOG_GAUGE
#   PIPE_DIALOG_GAUGE
#   VAR_IN_DIALOG_WR
# Arguments:
#   None
#######################################
dialog_gauge() {
  clear
  declare -gx VAR_IN_DIALOG_WR="gauge"
  declare -gx PIPE_DIALOG_GAUGE="/tmp/dialog_gauge_$$.fifo"
  if [[ -e "${PIPE_DIALOG_GAUGE}" && ! -p "${PIPE_DIALOG_GAUGE}" ]]; then
    rm -f "${PIPE_DIALOG_GAUGE}"
  fi
  [[ -p "${PIPE_DIALOG_GAUGE}" ]] || mkfifo "${PIPE_DIALOG_GAUGE}"

  setsid dialog --no-collapse \
                --ascii-lines \
                --keep-tite \
                --title "CISS.debian.installer powered by CoreSecret.eu" \
                --gauge "Starting installation..." \
                10 70 0 \
                < "${PIPE_DIALOG_GAUGE}" &
  declare -gx PID_DIALOG_GAUGE="$!"
  exec {FD_GAUGE}> "${PIPE_DIALOG_GAUGE}"
}

#######################################
# Wrapper for Dialog Program Box.
# Globals:
#   COLS_USE
#   PID_DIALOG_BOX
#   PIPE_DIALOG_BOX
#   ROWS_USE
#   VAR_IN_DIALOG_WR
# Arguments:
#   None
#######################################
dialog_box() {
  ### Save original stdout
  exec {FD_SAVE_OUT}>&1
  clear
  declare -gx VAR_IN_DIALOG_WR="box"
  declare -gx PIPE_DIALOG_BOX="/tmp/dialog_box_$$.fifo"
  if [[ -e "${PIPE_DIALOG_BOX}" && ! -p "${PIPE_DIALOG_BOX}" ]]; then
    rm -f "${PIPE_DIALOG_BOX}"
  fi
  [[ -p "${PIPE_DIALOG_BOX}" ]] || mkfifo "${PIPE_DIALOG_BOX}"

  dialog --create-rc ~/.dialogrc

  sed -i 's/screen_color = (CYAN,BLUE,ON)/screen_color = (WHITE,RED,ON)/' ~/.dialogrc
  sed -i 's/title_color = (BLUE,WHITE,ON)/title_color = (BLACK,WHITE,ON,ON)/' ~/.dialogrc
  sed -i 's/use_scrollbar = OFF/use_scrollbar = ON/' ~/.dialogrc
  sed -i 's/dialog_color = (BLACK,WHITE,OFF)/dialog_color = (WHITE,BLACK,OFF)/' ~/.dialogrc
  sed -i 's/border2_color = dialog_color/border2_color = (BLACK,WHITE,OFF)/' ~/.dialogrc
  sed -i 's/inputbox_border_color = dialog_color/inputbox_border_color = (BLACK,WHITE,OFF)/' ~/.dialogrc
  sed -i 's/inputbox_border2_color = dialog_color/inputbox_border2_color = (BLACK,WHITE,OFF)/' ~/.dialogrc
  sed -i 's/menubox_border_color = border_color/menubox_border_color = (BLACK,WHITE,OFF)/' ~/.dialogrc
  sed -i 's/menubox_border2_color = dialog_color/menubox_border2_color = (BLACK,WHITE,OFF)/' ~/.dialogrc
  sed -i 's/searchbox_border2_color = dialog_color/searchbox_border2_color = (BLACK,WHITE,OFF)/' ~/.dialogrc

  chmod 0440 ~/.dialogrc

  setsid dialog --colors \
                --ascii-lines \
                --title "Installation Progress" \
                --backtitle "CISS.debian.installer powered by CoreSecret.eu" \
                --programbox "${ROWS_USE}" "${COLS_USE}" \
                < "${PIPE_DIALOG_BOX}" &
  declare -gx PID_DIALOG_BOX="$!"
  ### Redirect both stdout and stderr into FIFO
  exec 1> "${PIPE_DIALOG_BOX}" 2>&1
}

#######################################
# Dialog Program Box Cleaner.
# Globals:
#   FD_SAVE_OUT
#   PID_DIALOG_BOX
#   PIPE_DIALOG_BOX
#   VAR_IN_DIALOG_WR
# Arguments:
#   None
#######################################
dialog_box_cleaner() {
  [[ -n "${FD_SAVE_OUT-}" ]] && exec 1>&"${FD_SAVE_OUT}" 2>&"${FD_SAVE_OUT}"
  [[ -n "${FD_SAVE_OUT-}" ]] && exec {FD_SAVE_OUT}>&-
  [[ -n "${FD_SAVE_OUT-}" ]] && unset FD_SAVE_OUT
  kill -TERM -- -"${PID_DIALOG_BOX}" 2>/dev/null || true
  wait "${PID_DIALOG_BOX}" 2>/dev/null || true
  unset PID_DIALOG_BOX
  rm -f "${PIPE_DIALOG_BOX}"
  unset PIPE_DIALOG_BOX
  rm -f ~/.dialogrc
  declare -gx VAR_IN_DIALOG_WR="false"
  clean_screen
  sleep 1
}

#######################################
# Dialog Gauge Bar Cleaner.
# Globals:
#   FD_GAUGE
#   PID_DIALOG_GAUGE
#   PIPE_DIALOG_GAUGE
#   VAR_IN_DIALOG_WR
# Arguments:
#   None
#######################################
dialog_gauge_cleaner() {
  if [[ -n "${FD_GAUGE-}" ]]; then
    exec {FD_GAUGE}>&-
    unset FD_GAUGE
  fi
  kill -TERM -- -"${PID_DIALOG_GAUGE}" 2>/dev/null || true
  wait "${PID_DIALOG_GAUGE}" 2>/dev/null || true
  unset PID_DIALOG_GAUGE
  rm -f "${PIPE_DIALOG_GAUGE}"
  unset PIPE_DIALOG_GAUGE
  declare -gx VAR_IN_DIALOG_WR="false"
  clean_screen
  sleep 1
}

#######################################
# Dialog Gauge Bar Cleaner.
# Globals:
#   VAR_IN_DIALOG_WR
# Arguments:
#   None
#######################################
dialog_text_cleaner() {
  declare -gx VAR_IN_DIALOG_WR="false"
  clean_screen
  sleep 1
}
# vim: number et ts=2 sw=2 sts=2 ai tw=128 ft=sh
