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

#######################################
# Function that generates each partition on each device according to the chosen recipe string.
# Globals:
#   HMP_PATH_PARTUUID
#   VAR_RECIPE_STRING
#   VAR_RECIPE_TABLE
#   VAR_SETUP_PART
# Arguments:
#   None
# Returns:
#   ERR_PARTITIONTBL
#   ERR_PART_CREATE
#   ERR_PART_READ
#   ERR_TABLE_CREATE
#   ERR_TABLE_DELETE
#   0: on success
#######################################
partitioning() {
  ### Declare Arrays and Variables.
  declare -Agx HMP_PATH_PARTUUID
  declare var_dev var_part var_end_arg var_begin var_end var_fs var_boot var_pri var_uuid var_mount_path
  declare -a ary_devs ary_parts

  ### Iterate over all devices in the recipe.
  readarray -t ary_devs < <(yq e -r ".recipe.${VAR_RECIPE_STRING}.dev | keys | .[]" "${VAR_SETUP_PART}")
  for var_dev in "${ary_devs[@]}"; do

    ### All current data for the respective device will be deleted.
    if ! blkdiscard "/dev/${var_dev}"; then
      do_log "fatal" "false" "Partition table deletion '/dev/${var_dev}' failed."
      return "${ERR_TABLE_DELETE}"
    fi

    do_log "info" "false" "Partition table of '/dev/${var_dev}' discarded."

    case "${VAR_RECIPE_TABLE,,}" in

      gpt|mbr)
        if ! parted -s "/dev/${var_dev}" mklabel "${VAR_RECIPE_TABLE,,}"; then
          do_log "fatal" "false" "Partition table creation '/dev/${var_dev}' failed."
          return "${ERR_TABLE_CREATE}"
        fi
        do_log "info" "false" "Partition table '${VAR_RECIPE_TABLE}' of '/dev/${var_dev}' generated."
        ;;
      *)
        do_log "fatal" "false" "No valid partition table chosen. String was '${VAR_RECIPE_TABLE}'."
        return "${ERR_PARTITIONTBL}"
        ;;

    esac

    ### Iterate over all partitions for this device.
    readarray -t ary_parts < <(yq e -r ".recipe.${VAR_RECIPE_STRING}.dev.${var_dev} | keys | .[]" "${VAR_SETUP_PART}")
    for var_part in "${ary_parts[@]}"; do

      ### Extract parameters from YAML.
      var_begin=$(yq_val ".recipe.${VAR_RECIPE_STRING}.dev.${var_dev}.${var_part}.begin"           "${VAR_SETUP_PART}")
      var_end=$(yq_val ".recipe.${VAR_RECIPE_STRING}.dev.${var_dev}.${var_part}.end"               "${VAR_SETUP_PART}")
      var_fs=$(yq_val ".recipe.${VAR_RECIPE_STRING}.dev.${var_dev}.${var_part}.filesystem.version" "${VAR_SETUP_PART}")
      var_boot=$(yq_val ".recipe.${VAR_RECIPE_STRING}.dev.${var_dev}.${var_part}.bootable"         "${VAR_SETUP_PART}")
      var_pri=$(yq_val ".recipe.${VAR_RECIPE_STRING}.dev.${var_dev}.${var_part}.primary"           "${VAR_SETUP_PART}")
      var_mount_path=$(yq_val ".recipe.${VAR_RECIPE_STRING}.dev.${var_dev}.${var_part}.mount.path" "${VAR_SETUP_PART}")

      ### Generate partition.
      if [[ "${var_end}" == "-1" ]]; then
        var_end_arg="100%"
      else
        var_end_arg="${var_end}"
      fi

      if ! parted -s "/dev/${var_dev}" mkpart "${var_pri}" "${var_fs}" "${var_begin}" "${var_end_arg}"; then
        do_log "fatal" "false" "Partition creation '/dev/${var_dev}${var_part}' failed."
        return "${ERR_PART_CREATE}"
      fi

      do_log "info" "false" "Partition generated: '${var_part}' | on device '/dev/${var_dev}' | begin: '${var_begin}' | end: '${var_end_arg}'."

      ### Set the bootable flag if necessary.
      if [[ "${var_boot,,}" == "true" ]]; then
        parted -s "/dev/${var_dev}" set "${var_part}" boot on
        do_log "info" "false" "Partition: '/dev/${var_dev}${var_part}' marked as bootable."
      fi

      ### Store UUID of the partition.
      var_uuid=$(blkid -s UUID -o value "/dev/${var_dev}${var_part}")
      if [[ -z "${var_uuid}" ]]; then
        do_log "fatal" "false" "WARNING: could not read UUID for '/dev/${var_dev}${var_part}'."
        return "${ERR_PART_READ}"
      else
        HMP_PATH_PARTUUID["UUID_${var_mount_path}"]="${var_uuid}"
      fi

    done

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