# Post-install hardware re-probe (BACKLOG #58 / HARDWARE.md §8).
# Same pure nomarchy_detect_hw protocol as the installer; prints suggested
# hardwareProfile + system.nix snippets. Never rewrites the flake.
{ lib
, stdenvNoCC
, makeWrapper
, bash
, coreutils
, pciutils
, usbutils
, util-linux
}:

stdenvNoCC.mkDerivation {
  pname = "nomarchy-detect-hw";
  version = "0.1.0";

  src = ./.;

  nativeBuildInputs = [ makeWrapper ];

  installPhase = ''
    runHook preInstall

    share=$out/share/nomarchy-detect-hw
    install -Dm644 ${../nomarchy-install/hardware-db.sh} "$share/hardware-db.sh"
    install -Dm755 nomarchy-detect-hw.sh $out/bin/nomarchy-detect-hw
    patchShebangs $out/bin/nomarchy-detect-hw

    wrapProgram $out/bin/nomarchy-detect-hw \
      --prefix PATH : ${lib.makeBinPath [ bash coreutils pciutils usbutils util-linux ]} \
      --set NOMARCHY_DETECT_HW_SHARE "$share"

    runHook postInstall
  '';

  meta = {
    description = "Probe hardware and print Nomarchy hardwareProfile / system.nix suggestions";
    license = lib.licenses.mit;
    mainProgram = "nomarchy-detect-hw";
    platforms = lib.platforms.linux;
  };
}
