# vim: et:ts=2:sw=2:
{
  lib,
  stdenv,
  autoPatchelfHook,
  makeWrapper,
  xkeyboard_config,
  libusb1,
  fetchzip,
  writeText,
  unzip,
  qt6,
}:
let
  desktopFile = writeText "librevna.desktop" ''
    		[Desktop Entry]
        Type=Application
        Name=LibreVNA
        Exec=/run/current-system/sw/bin/librevna
        Categories=Development;
  '';

  librevna = stdenv.mkDerivation {
    pname = "librevna";
    version = "1.6.4";

    nativeBuildInputs = [
      autoPatchelfHook
      qt6.wrapQtAppsHook
      makeWrapper
      unzip
    ];

    buildInputs = [
      qt6.qtbase
      libusb1
    ];

    src =
      if stdenv.isDarwin then
        fetchzip {
          url = "https://github.com/jankae/LibreVNA/releases/download/v1.6.4/LibreVNA-GUI-OSX-latest-v${librevna.version}.zip";
          hash = "";
        }
      else if stdenv.isAarch64 then
        fetchzip {
          url = "https://github.com/jankae/LibreVNA/releases/download/v1.6.4/LibreVNA-GUI-RPi5-v${librevna.version}.zip";
          hash = "";
        }
      else
        fetchzip {
          url = "https://github.com/jankae/LibreVNA/releases/download/v1.6.4/LibreVNA-GUI-Ubuntu-v${librevna.version}.zip";
          hash = "sha256-s8LjsmBEpJeXrWHPkKTSPV/ux4VMG7hXC9ySotE9nAE=";
        };

    installPhase = ''
      mkdir -p $out/bin
      cp -r LibreVNA-GUI $out/bin/.librevna-gui
      chmod +x $out/bin/.librevna-gui

      mkdir -p $out/share/applications
      cp ${desktopFile} $out/share/applications/librevna.desktop

      mkdir -p $out/bin
      makeWrapper $out/bin/.librevna-gui \
          $out/bin/librevna \
            --prefix "QT_XKB_CONFIG_ROOT" ":" "${xkeyboard_config}/share/X11/xkb" \
            --prefix "LD_LIBRARY_PATH" ":" "${lib.makeLibraryPath librevna.buildInputs}"
    '';
  };
in
librevna
