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

#######################################
# Argument Parser
# Globals:
#   VAR_DEFAULT_LOG_LEVEL
#   VAR_AUTO_INSTALL
#   VAR_IN_DIALOG_WR
#   VAR_PRIORITY
#   VAR_REIONICE_CLASS
#   VAR_REIONICE_PRIORITY
# Arguments:
#   None
#######################################
arg_parser() {
  while [[ $# -gt 0 ]]; do
    declare argument="${1}"
      case "${argument,,}" in

        -a | --autoinstall)
          if [[ -n "${2-}" && "${2}" != -* ]]; then arg_mismatch "--autoinstall MUST NOT be followed by an argument."; fi
          shift 1
          ;;

        -c | --contact)
          if [[ -n "${2-}" && "${2}" != -* ]]; then arg_mismatch "--contact MUST NOT be followed by an argument."; fi
          shift 1
          ;;

        -d | --debug)
          shift 1
          while [[ $# -gt 0 && ! "$1" =~ ^- ]]; do
            shift 1
          done
          ;;

        -h | --help)
          if [[ -n "${2-}" && "${2}" != -* ]]; then arg_mismatch "--help MUST NOT be followed by an argument."; fi
          shift 1
          ;;

        -l | --log)
          case "${2,,}" in
            debug|info|notice|warn|error|critical|fatal|emergency) declare -gx VAR_DEFAULT_LOG_LEVEL="$2"; shift 2 ;;
            *)
              if [[ "${VAR_AUTO_INSTALL}" == "false" && "${VAR_IN_DIALOG_WR}" == true ]]; then dialog_box_cleaner; fi
              usage
              ;;
            esac
          ;;

        -v | --version)
          if [[ -n "${2-}" && "${2}" != -* ]]; then arg_mismatch "--version MUST NOT be followed by an argument."; fi
          shift 1
          ;;

        --renice-priority)
          if [[ -n ${2-} && ${2} =~ ^-?[0-9]+$ && ${2} -ge -19 && ${2} -le 19 ]]; then
            # shellcheck disable=SC2034
            VAR_PRIORITY="${2}"
            shift 2
          else
            arg_mismatch "--renice-priority MUST be an integer between '-19' and '19'."
          fi
          ;;

        --reionice-priority)
          if [[ -z "${2-}" ]]; then
            arg_mismatch "--reionice-priority no values provided."
          else
            if [[ "${2}" =~ ^[1-3]$ ]]; then
              # shellcheck disable=SC2034
              VAR_REIONICE_CLASS="${2}"
              if [[ -z "${3-}" ]]; then
                :
              else
                if [[ "${3}" =~ ^[0-7]$ ]]; then
                  VAR_REIONICE_PRIORITY="${3}"
                else
                  arg_mismatch "--reionice-priority PRIORITY MUST be an integer between '0' and '7'."
                fi
              fi
            else
              arg_mismatch "--reionice-priority CLASS MUST be an integer between '1' and '3'."
            fi
          fi
          if [[ -n ${VAR_REIONICE_PRIORITY} ]]; then
            shift 3
          else
            shift 2
          fi
          ;;

        *)
          if [[ "${VAR_AUTO_INSTALL}" == "false" && "${VAR_IN_DIALOG_WR}" == "box" ]]; then
            dialog_box_cleaner
          elif [[ "${VAR_AUTO_INSTALL}" == "false" && "${VAR_IN_DIALOG_WR}" == "gauge" ]]; then
            dialog_gauge_cleaner
          fi
          usage
          ;;
    esac
  done
}
# vim: number et ts=2 sw=2 sts=2 ai tw=128 ft=sh
