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

#######################################
# Capture an initial snapshot of all variables (excluding '^(BASH|_).*').
# Globals:
#   VAR_DUMP_VARS_INITIAL
# Arguments:
#   None
#######################################
dump_vars_initial() {
  # shellcheck disable=SC2312
  {
    declare var
    while IFS= read -r var; do
      declare -p "${var}" 2> /dev/null
    done < <(compgen -v | grep -Ev '^(BASH|_).*')
  } | sort >| "${VAR_DUMP_VARS_INITIAL}"
}

#######################################
# Capture the final snapshot of all variables (excluding '^(BASH|_).*').
# Globals:
#   LOG_VAR
#   VAR_DUMP_VARS_FINAL
#   VAR_DUMP_VARS_INITIAL
#   VAR_VERSION
# Arguments:
#   None
#######################################
dump_vars_exiting() {
  set +x
  # shellcheck disable=SC2312
  {
    declare var
    while IFS= read -r var; do
      declare -p "${var}" 2>/dev/null
    done < <(compgen -v | grep -Ev '^(BASH|_).*')
  } | sort >| "${VAR_DUMP_VARS_FINAL}"
  set -x

  {
    printf "✅ CISS.debian.installer Config Variable Dump. \n"
    printf "✅ Version             : %s \n" "${VAR_VERSION}"
    printf "\n"
    printf "===== Initial VAR Environment ===== \n"
  } >> "${LOG_VAR}"

  set +x
  comm -23 "${VAR_DUMP_VARS_INITIAL}" "${VAR_DUMP_VARS_FINAL}" >> "${LOG_VAR}"  || true
  set -x

  {
    printf "\n"
    printf "===== Final VAR Environment ===== \n"
  } >> "${LOG_VAR}"

  set +x
  comm -13 "${VAR_DUMP_VARS_INITIAL}" "${VAR_DUMP_VARS_FINAL}" >> "${LOG_VAR}" || true
  set -x

  rm -f "${VAR_DUMP_VARS_INITIAL}" "${VAR_DUMP_VARS_FINAL}"
}
# vim: number et ts=2 sw=2 sts=2 ai tw=128 ft=sh
