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

#######################################
# Installation of the specified kernel.
# 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
#   VAR_KERNEL
#   image
# Arguments:
#   None
# Returns:
#   0: on success
#######################################
installation_initramfs() {
  ### Declare Arrays, HashMaps, and Variables.
  declare     var_modules=""

  ### Install the script to be called by 'update-initramfs' for installing the necessary modules to load into initramfs environment.
  install -D -m 0644 -o root -g root "${VAR_SETUP_PATH}/includes/target/etc/initramfs-tools/modules" \
    "${TARGET}/etc/initramfs-tools/"

  insert_comments "${TARGET}/etc/initramfs-tools/modules"

  var_modules=$(grep_nic_driver_modules)

  cat << EOF >> "${TARGET}/etc/initramfs-tools/modules"

### Custom NIC driver
${var_modules}

# vim: number et ts=2 sw=2 sts=2 ai tw=128 ft=sh
EOF

  ### MODULES: [ most | netboot | dep | list ]
  ## 'most' - Add most filesystem and all hard-drive drivers.
  ## 'dep'  - Try and guess the modules to load.

  cat << EOF >| "${TARGET}/etc/initramfs-tools/conf.d/driver-policy"
# 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

# Static file system information: /etc/initramfs-tools/conf.d/driver-policy
# Generated by CISS.debian.installer ${VAR_VERSION}
# Architecture: ${VAR_ARCHITECTURE}
# Distribution: ${VAR_CODENAME}

# Driver inclusion policy selected during installation.
# Note: This setting overrides the value set in the file '/etc/initramfs-tools/initramfs.conf'.

MODULES=dep

# vim: number et ts=2 sw=2 sts=2 ai tw=128 ft=sh
EOF

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