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

### Options in "GRUB_CMDLINE_LINUX" are always effective.
### Options in "GRUB_CMDLINE_LINUX_DEFAULT" are effective ONLY during normal boot (NOT during recovery mode).

guard_sourcing

#######################################
# Helper module to extract the current GRUB CMDLINE strings.
# Globals:
#   TARGET
#   VAR_ORIG_GRUB_CMDLINE_LINUX
#   VAR_ORIG_GRUB_CMDLINE_LINUX_DEFAULT
#   VK_GRUB_CMDLINE_LINUX
#   VK_GRUB_CMDLINE_LINUX_DEFAULT
#   VV_GRUB_CMDLINE_LINUX
#   VV_GRUB_CMDLINE_LINUX_DEFAULT
# Arguments:
#   None
# Returns:
#   0: on success
#######################################
grub_extract_current_string() {
  ### Declare Arrays, HashMaps, and Variables.
  declare -gx VAR_ORIG_GRUB_CMDLINE_LINUX="" VAR_ORIG_GRUB_CMDLINE_LINUX_DEFAULT="" \
              VK_GRUB_CMDLINE_LINUX='' VK_GRUB_CMDLINE_LINUX_DEFAULT='' \
              VV_GRUB_CMDLINE_LINUX="" VV_GRUB_CMDLINE_LINUX_DEFAULT=""
  declare     vv_raw_lin="" vv_raw_lin_def=""

  ### Original lines (retained, strictly anchored).
  VAR_ORIG_GRUB_CMDLINE_LINUX=$(grep -E '^GRUB_CMDLINE_LINUX=' "${TARGET}/etc/default/grub")
  VAR_ORIG_GRUB_CMDLINE_LINUX_DEFAULT=$(grep -E '^GRUB_CMDLINE_LINUX_DEFAULT=' "${TARGET}/etc/default/grub")

  do_log "debug" "file_only" "1081() VAR_ORIG_GRUB_CMDLINE_LINUX: [${VAR_ORIG_GRUB_CMDLINE_LINUX}]."
  do_log "debug" "file_only" "1081() VAR_ORIG_GRUB_CMDLINE_LINUX_DEFAULT: [${VAR_ORIG_GRUB_CMDLINE_LINUX_DEFAULT}]."

  ### Keys (fixed prefixes).
  VK_GRUB_CMDLINE_LINUX='GRUB_CMDLINE_LINUX='
  VK_GRUB_CMDLINE_LINUX_DEFAULT='GRUB_CMDLINE_LINUX_DEFAULT='

  ### Raw values (with quotes if necessary).
  # shellcheck disable=SC2295
  vv_raw_lin="${VAR_ORIG_GRUB_CMDLINE_LINUX#${VK_GRUB_CMDLINE_LINUX}}"
  # shellcheck disable=SC2295
  vv_raw_lin_def="${VAR_ORIG_GRUB_CMDLINE_LINUX_DEFAULT#${VK_GRUB_CMDLINE_LINUX_DEFAULT}}"

  ### Remove outer quotes symmetrically.
  if [[ -n ${vv_raw_lin} ]]; then

    declare q="${vv_raw_lin:0:1}"

    if [[ ${q} == "'" || ${q} == '"' ]]; then

      [[ ${vv_raw_lin: -1} == "${q}" ]] && vv_raw_lin=${vv_raw_lin:1:-1} || vv_raw_lin=${vv_raw_lin:1}

    fi
  fi

  if [[ -n ${vv_raw_lin_def} ]]; then

    declare q="${vv_raw_lin_def:0:1}"

    if [[ ${q} == "'" || ${q} == '"' ]]; then

      [[ ${vv_raw_lin_def: -1} == "${q}" ]] && vv_raw_lin_def=${vv_raw_lin_def:1:-1} || vv_raw_lin_def=${vv_raw_lin_def:1}

    fi

  fi

  ### Trim leading and trailing whitespace (including TAB, CR, and LF).
  ### Front:  remove the longest prefix from whitespace.
  vv_raw_lin="${vv_raw_lin#"${vv_raw_lin%%[![:space:]]*}"}"
  vv_raw_lin_def="${vv_raw_lin_def#"${vv_raw_lin_def%%[![:space:]]*}"}"

  ### Rear: remove the longest suffix from whitespaces.
  vv_raw_lin="${vv_raw_lin%"${vv_raw_lin##*[![:space:]]}"}"
  vv_raw_lin_def="${vv_raw_lin_def%"${vv_raw_lin_def##*[![:space:]]}"}"

  ### Expose end values (only value, without quotes, trimmed)
  # shellcheck disable=SC2034
  VV_GRUB_CMDLINE_LINUX="${vv_raw_lin}"
  # shellcheck disable=SC2034
  VV_GRUB_CMDLINE_LINUX_DEFAULT="${vv_raw_lin_def}"

  do_log "debug" "file_only" "1081() VV_GRUB_CMDLINE_LINUX: [${VV_GRUB_CMDLINE_LINUX}]."
  do_log "debug" "file_only" "1081() VV_GRUB_CMDLINE_LINUX_DEFAULT: [${VV_GRUB_CMDLINE_LINUX_DEFAULT}]."

  return 0
}

#######################################
# Helper module to finish the modified GRUB CMDLINE strings.
# Globals:
#   TARGET
#   VAR_GRUB_CMDLINE_LINUX
#   VAR_GRUB_CMDLINE_LINUX_DEFAULT
#   VK_GRUB_CMDLINE_LINUX
#   VK_GRUB_CMDLINE_LINUX_DEFAULT
#   VV_GRUB_CMDLINE_LINUX
#   VV_GRUB_CMDLINE_LINUX_DEFAULT
# Arguments:
#   None
# Returns:
#   0: on success
#######################################
grub_finalize_string() {
  ### Declare Arrays, HashMaps, and Variables.
  declare     var_file="${TARGET}/etc/default/grub.d/99-ciss-cmdline.cfg"
  declare     var_linux="${VV_GRUB_CMDLINE_LINUX//\'/\'\"\'\"\'}"
  declare     var_linux_default="${VV_GRUB_CMDLINE_LINUX_DEFAULT//\'/\'\"\'\"\'}"
  declare -gx VAR_GRUB_CMDLINE_LINUX="${VK_GRUB_CMDLINE_LINUX}'${VV_GRUB_CMDLINE_LINUX}'"
  declare -gx VAR_GRUB_CMDLINE_LINUX_DEFAULT="${VK_GRUB_CMDLINE_LINUX_DEFAULT}'${VV_GRUB_CMDLINE_LINUX_DEFAULT}'"

  mkdir -p "${TARGET}/etc/default/grub.d"

  cat << EOF >| "${var_file}"
# 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

### Options in "GRUB_CMDLINE_LINUX" are always effective.
### Options in "GRUB_CMDLINE_LINUX_DEFAULT" are effective ONLY during normal boot (NOT during recovery mode).

EOF

  insert_comments "${TARGET}/etc/default/grub.d/99-ciss-cmdline.cfg"

  umask 0022
  {
    printf "GRUB_CMDLINE_LINUX='%s'\n" "${var_linux}"
    printf "\n"
    printf "GRUB_CMDLINE_LINUX_DEFAULT='%s'\n" "${var_linux_default}"
    printf "\n"
    printf "# vim: number et ts=2 sw=2 sts=2 ai tw=128 ft=sh\n"
  } >> "${var_file}"

  do_log "debug" "file_only" "1081() [${VAR_GRUB_CMDLINE_LINUX}]."
  do_log "debug" "file_only" "1081() [${VAR_GRUB_CMDLINE_LINUX_DEFAULT}]."

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