#!/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 and set up the minimum required tools for the next installation steps.
# Every 'apt-get install' command is invoked by adding 'export INITRD=No'
# to suppress the 'update-initramfs'-Kernel-Hooks, according to the initramfs-tools manpage:
# https://manpages.debian.org/testing/initramfs-tools-core/initramfs-tools.7.en.html
# Globals:
#   TARGET
# Arguments:
#   None
# Returns:
#   0: on success
#######################################
installation_toolset() {
  ### Declare Arrays, HashMaps, and Variables.
  # shellcheck disable=SC2154
  declare -A  hmp_tool_pkg=(
    [apt-show-versions]="apt-show-versions"
    [bc]="bc"
    [dig]="bind9-dnsutils"
    [host]="bind9-dnsutils"
    [hexdump]="bsdmainutils"
    [btrfs]="btrfs-progs"
    [bunzip2]="bzip2"
    [base64]="coreutils"
    [cat]="coreutils"
    [chmod]="coreutils"
    [chown]="coreutils"
    [cp]="coreutils"
    [echo]="coreutils"
    [ln]="coreutils"
    [mkdir]="coreutils"
    [curl]="curl"
    [dirmngr]="dirmngr"
    [dmsetup]="dmsetup"
    [fsck.vfat]="dosfstools"
    [mkfs.vfat]="dosfstools"
    [e2label]="e2fsprogs"
    [tune2fs]="e2fsprogs"
    [fsck]="e2fsprogs"
    [efibootmgr]="efibootmgr"
    [fdisk]="fdisk"
    [file]="file"
    [awk]="gawk"
    [gdisk]="gdisk"
    [gnupg]="gnupg"
    [grep]="grep"
    [gzip]="gzip"
    [haveged]="haveged"
    [update-initramfs]="initramfs-tools"
    [ip]="iproute2"
    [ping]="iputils-ping"
    [jq]="jq"
    [loadkeys]="kbd"
    [setfont]="kbd"
    [keyctl]="keyutils"
    [modprobe]="kmod"
    [libpam-pwquality]="libpam-pwquality"
    [logrotate]="logrotate"
    [lsb_release]="lsb-release"
    [parted]="parted"
    [chpasswd]="passwd"
    [chsh]="passwd"
    [lspci]="pciutils"
    [sysctl]="procps"
    [pwgen]="pwgen"
    [sed]="sed"
    [sudo]="sudo"
    [tar]="tar"
    [tree]="tree"
    [unzip]="unzip"
    [lsusb]="usbutils"
    [blkid]="util-linux"
    [dmesg]="util-linux"
    [lsblk]="util-linux"
    [findmnt]="util-linux"
    [mount]="util-linux"
    [umount]="util-linux"
    [xxd]="vim-common"
    [wget]="wget"
    [whois]="whois"
    [zsh]="zsh"
    [zstd]="zstd"
  )

  declare -a  ary_missing_pkgs=() ary_unique_pkgs=()
  declare -r  var_logfile="/root/.ciss/cdi/log/4130_installation_toolset.log"
  declare     var_bin=""

  touch "${TARGET}${var_logfile}" && chmod 0600 "${TARGET}${var_logfile}"

  ### Collecting missing binaries.
  for var_bin in "${!hmp_tool_pkg[@]}"; do

    if ! do_in_target_script "${TARGET}" "command -v ${var_bin} >/dev/null" "debug"; then
      ary_missing_pkgs+=("${hmp_tool_pkg[${var_bin}]}")
    fi

  done
  do_log "debug" "file_only" "4130() [ary_missing_pkgs]='${ary_missing_pkgs[*]}'."

  ### Installing unique list of packages.
  if ((${#ary_missing_pkgs[@]})); then

    # shellcheck disable=SC2312
    mapfile -t ary_unique_pkgs < <(printf '%s\n' "${ary_missing_pkgs[@]}" | sort -u)
    do_log "debug" "file_only" "4130() [ary_unique_pkgs]='${ary_unique_pkgs[*]}'."

    do_in_target_script "${TARGET}" "
      export INITRD=No
      apt-get install -y --no-install-recommends --no-install-suggests ${ary_unique_pkgs[*]} 2>&1 | tee -a ${var_logfile}
      echo ExitCode: \$? >> ${var_logfile}
    "

  fi

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