#!/usr/bin/env bash
# nomarchy-detect-hw — post-install re-probe (BACKLOG #58 / HARDWARE.md §8).
#
# Runs the same pure nomarchy_detect_hw protocol as the installer and
# prints suggested hardwareProfile + system.nix snippets. Never rewrites
# the flake (no --apply).
#
# Usage:
#   nomarchy-detect-hw           # human-readable report + snippets
#   nomarchy-detect-hw --raw     # MODULE / NOMARCHY / DETAIL lines only
set -euo pipefail

SHARE="${NOMARCHY_DETECT_HW_SHARE:?nomarchy-detect-hw: not run via the packaged wrapper}"
# shellcheck source=/dev/null
source "$SHARE/hardware-db.sh"

raw=false
case "${1:-}" in
  --raw|-r) raw=true ;;
  -h|--help)
    cat <<'EOF'
Usage: nomarchy-detect-hw [--raw]

Probe this machine the same way the live installer does and print
suggested flake hardwareProfile and system.nix hardware lines.

  --raw   emit only MODULE / NOMARCHY / NOMARCHY-NPU / DETAIL lines
          (installer protocol). Default is a human report + snippets.

Does not modify any files. Paste the snippets into ~/.nomarchy after review.
EOF
    exit 0
    ;;
  "") ;;
  *)
    echo "nomarchy-detect-hw: unknown option: $1 (try --help)" >&2
    exit 2
    ;;
esac

if $raw; then
  nomarchy_detect_hw
  exit 0
fi

profiles=()
nomarchy_lines=()
npu=""
details=()

while IFS= read -r line; do
  case "$line" in
    MODULE\ *)        profiles+=("${line#MODULE }") ;;
    NOMARCHY\ *)      nomarchy_lines+=("${line#NOMARCHY }") ;;
    NOMARCHY-NPU\ *)  npu="${line#NOMARCHY-NPU }" ;;
    DETAIL\ *)        details+=("${line#DETAIL }") ;;
  esac
done < <(nomarchy_detect_hw)

echo "nomarchy-detect-hw — suggestions for this machine"
echo "(read-only; paste into ~/.nomarchy after review)"
echo

if ((${#details[@]})); then
  echo "## Detected"
  for d in "${details[@]}"; do
    printf '  · %s\n' "$d"
  done
  echo
fi

echo "## flake.nix — hardwareProfile"
if ((${#profiles[@]})); then
  echo "      hardwareProfile = ["
  for p in "${profiles[@]}"; do
    printf '        "%s"\n' "$p"
  done
  echo "      ];"
else
  echo "      # (no modules detected — leave hardwareProfile unset or null)"
fi
echo

echo "## system.nix — nomarchy.hardware / power (safe defaults active;"
echo "## heavier opt-ins stay commented, same as the installer)"
if ((${#profiles[@]})) && printf '%s\n' "${profiles[@]}" | grep -qx 'common-pc-laptop'; then
  echo "  nomarchy.system.power.laptop = true;"
  echo "  # nomarchy.system.power.batteryChargeLimit = 80;"
fi
for nm in "${nomarchy_lines[@]:-}"; do
  case "$nm" in
    hardware.intel.enable=true)
      echo "  nomarchy.hardware.intel.enable = true;            # GuC/HuC (i915)"
      echo "  # nomarchy.hardware.intel.computeRuntime = true;  # OpenCL/oneVPL (opt-in)"
      ;;
    hardware.intel.guc=false)
      echo "  nomarchy.hardware.intel.guc = false;              # xe driver → GuC default-on"
      ;;
    hardware.amd.enable=true)
      echo "  nomarchy.hardware.amd.enable = true;              # amd-pstate + VA-API"
      echo "  # nomarchy.hardware.amd.rocm.enable = true;       # ROCm (multi-GB, opt-in)"
      echo '  # nomarchy.hardware.amd.rocm.gfxOverride = "";    # e.g. "11.0.0" for unlisted iGPU'
      ;;
    hardware.fingerprint.enable=true)
      echo "  nomarchy.hardware.fingerprint.enable = true;      # fprintd (enroll: fprintd-enroll)"
      echo "  # nomarchy.hardware.fingerprint.pam = true;       # login + sudo (opt-in)"
      ;;
    hardware.camera.hideIrSensor=true)
      echo "  nomarchy.hardware.camera.hideIrSensor = true;     # dual-sensor: hide IR node"
      ;;
    *)
      echo "  # (unmapped NOMARCHY line: $nm)"
      ;;
  esac
done
if [[ -n "$npu" ]]; then
  echo "  # nomarchy.hardware.npu.enable = true;            # $npu NPU (experimental; userspace BYO)"
  echo "  # nomarchy.hardware.latestKernel = true;          # if the NPU driver needs a newer kernel"
fi
if printf '%s\n' "${profiles[@]:-}" | grep -qx 'common-gpu-nvidia'; then
  echo "  # NVIDIA: common-gpu-nvidia is in hardwareProfile (flake.nix)."
  echo "  # Hybrid/PRIME, power, open-module — plain NixOS; see docs/HARDWARE.md §6"
  echo "  # hardware.nvidia.prime = { ... };"
  echo "  # hardware.nvidia.powerManagement.enable = true;"
  echo "  # hardware.nvidia.open = false;  # or true on newer cards"
fi
if ((${#nomarchy_lines[@]} == 0)) && [[ -z "$npu" ]] \
   && ! printf '%s\n' "${profiles[@]:-}" | grep -qx 'common-gpu-nvidia' \
   && ! printf '%s\n' "${profiles[@]:-}" | grep -qx 'common-pc-laptop'; then
  echo "  # (no nomarchy.hardware lines suggested)"
fi
echo
echo "Apply with: sudo nixos-rebuild switch --flake \${NOMARCHY_PATH:-\$HOME/.nomarchy}#default"
echo "Docs: docs/HARDWARE.md §8"
