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

#######################################
# Returns standardized labels for the provided mount path depending on filesystem and art of label.
# Globals:
#   None
# Arguments:
#   1: Mount path (e.g., '/' '/boot' '/var/log/audit' 'SWAP' '/boot/efi')
#   2: Filesystem type: one of: [BIOS, btrfs, ext4, fat32, swap]
#   3: Desired return label: one of: [part, luks, file, sub, snap]
# Returns:
#   Prints resulting label to stdout.
#######################################
get_label() {
  declare var_path="${1,,}"
  declare var_file="${2,,}"
  declare var_kind="${3,,}"

  ### Normalize 'mount.path': remove leading slash and replace slashes with underscore.
  declare var_return_label=""
  declare var_normalized="${var_path#/}"                # Strip leading "/".
  var_normalized="${var_normalized//\//_}"              # Replace "/" with "_"
  [[ -z "${var_normalized}" ]] && var_normalized="root" # Edge case "/".

  case "${var_kind}" in

    part)

      case "${var_path}" in

        swap)                 var_return_label="part_ephem_swap" ;;
        /tmp)                 var_return_label="part_ephem_tmp" ;;
        /boot/efi)            var_return_label="part_esp" ;;
        /)                    var_return_label="part_root" ;;
        *)                    var_return_label="part_${var_normalized}" ;;

      esac
      ;;

    luks)

      case "${var_path}" in

        /tmp)                 var_return_label="crypt_ephem_tmp" ;;
        swap)                 var_return_label="crypt_ephem_swap" ;;
        /boot/efi)            var_return_label="" ;;
        /)                    var_return_label="crypt_root" ;;
        *)                    var_return_label="crypt_${var_normalized}" ;;

      esac
      ;;

    file)

      case "${var_path}:${var_file}" in

        swap:*)               var_return_label="host_swap" ;;
        /tmp:*)               var_return_label="host_tmp" ;;
        /boot/efi:*)          var_return_label="ESP" ;;
        /var/log/audit:ext4)  var_return_label="ext4_var_audit" ;;
        *:ext4)               var_return_label="ext4_${var_normalized}"
                              if (( ${#var_return_label} > 16 )); then
                                do_log "warn" "file_only" "3295() Label: '${var_return_label}' of type: '${var_kind}' created for: '${var_path}' exceeds 16 chars, truncating."
                                var_return_label="${var_return_label:0:16}"
                              fi
                              ;;
        *:btrfs)              var_return_label="btrfs_${var_normalized}" ;;
        *:*)                  var_return_label="${var_file}_${var_normalized}" ;;

      esac
      ;;

    sub)

      if [[ "${var_file}" == "btrfs" ]]; then
        var_return_label="@${var_normalized}"
      fi
      ;;

    snap)

      if [[ "${var_file}" == "btrfs" ]]; then
        var_return_label="@${var_normalized}_snap"
      fi
      ;;

    *)

      do_log "warn" "3295() File_only" "Unknown kind: ${var_kind}"
      return 0
      ;;

  esac

  if [[ -n "${var_return_label}" ]]; then

    do_log "info" "file_only" "3295() Label: '${var_return_label}' of type: '${var_kind}' created for: '${var_path}'."
    printf "%s\n" "${var_return_label}"
    return 0

  else

    do_log "warn" "file_only" "3295() Label not resolved: of type: '${var_kind}' for: '${var_path}' and FS: '${var_file}'."
    return 0

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