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

#######################################
# Check for required Deb Packages to run the script.
# Globals:
#   VAR_AUTO_INSTALL
# Arguments:
#   None
#######################################
check_pkgs() {
  apt-get update -y > /dev/null 2>&1

  # TODO: Only activate in case CISS.debian.live.builder does not include the following packages as per default.
  ### Define HashMap: command -> package
  # shellcheck disable=SC2154
  #declare -A hmp_command_packages=(
  #  [apt-transport-https]=apt-transport-https
  #  [bzip2]=bzip2
  #  [ca-certificates]=ca-certificates
  #  [curl]=curl
  #  [expect]=expect
  #  [fdisk]=fdisk
  #  [gdisk]=gdisk
  #  [git]=git
  #  [gpg]=gnupg
  #  [lsb_release]=lsb-release
  #  [mkfs.btrfs]=btrfs-progs
  #  [mkfs.ext4]=e2fsprogs
  #  [mkfs.fat]=dosfstools
  #  [mkswap]=util-linux
  #  [mkfs.xfs]=xfsprogs
  #  [parted]=parted
  #  [pwgen]=pwgen
  #  [tar]=tar
  #  [wget]=wget
  #  [whois]=whois
  #  [xz]=xz-utils
  #  [yq]=yq
  #)

  ### Iterate over HashMap
  #declare var_cmd var_pkg
  #for var_cmd in "${!hmp_command_packages[@]}"; do
  #  var_pkg="${hmp_command_packages[${var_cmd}]}"
  #  if ! command -v "${var_cmd}" &>/dev/null; then
  #    apt-get install -y --no-install-recommends "${var_pkg}"
  #    do_log "info" "file_only" "Installing ${var_pkg} done."
  #  else
  #    do_log "info" "file_only" "${var_cmd} already installed."
  #  fi
  #done

  if [[ -z "$(command -v debootstrap || true)" ]]; then
    if grep -RqsE '^[[:space:]]*deb .*backports' /etc/apt/sources.list /etc/apt/sources.list.d; then
      # shellcheck disable=SC2155
      declare codename=$(lsb_release -sc)
      apt-get install -y -t "${codename}-backports" debootstrap
    else
      apt-get install -y debootstrap
    fi
  fi

  if [[ -z "$(command -v dialog || true)" ]]; then
    if ! "${VAR_AUTO_INSTALL}"; then apt-get install -y --no-install-recommends dialog; fi
  fi

  return 0
}
# vim: number et ts=2 sw=2 sts=2 ai tw=128 ft=sh
