{
  lib,
  stdenvNoCC,
  fetchurl,
  dpkg,
  makeWrapper,
  autoPatchelfHook,
  alsa-lib,
  gtk3,
  libnotify,
  nss,
  xdg-utils,
  at-spi2-core,
  libsecret,
  libuuid,
  libxscrnsaver,
  libxtst,
  mesa,
}:

let
  version = "1.2.13";

  srcInfo =
    if stdenvNoCC.hostPlatform.system == "x86_64-linux" then
      {
        url = "https://github.com/Farigh/pokeclicker-automation-desktop/releases/download/v${version}/pokeclicker-desktop-with-scripts_${version}_amd64.deb";
        hash = "sha256-V4z0RDyaJHICUBcFanfZqGVHNVJcKakABx9RSen6hZw=";
      }
    else if stdenvNoCC.hostPlatform.system == "aarch64-linux" then
      {
        url = "https://github.com/Farigh/pokeclicker-automation-desktop/releases/download/v${version}/pokeclicker-desktop-with-scripts_${version}_arm64.deb";
        hash = "sha256-tcVD0eV3hT9Iq75cJ7o/EK7uLHHBWhxdUHsWdJvcNlk=";
      }
    else
      throw "pokeclicker-automation-desktop: unsupported platform: ${stdenvNoCC.hostPlatform.system}";
in
stdenvNoCC.mkDerivation {
  pname = "pokeclicker-automation-desktop";
  inherit version;

  src = fetchurl srcInfo;

  nativeBuildInputs = [
    dpkg
    makeWrapper
    autoPatchelfHook
  ];

  buildInputs = [
    alsa-lib
    gtk3
    libnotify
    mesa
    nss
    xdg-utils
    at-spi2-core
    libsecret
    libuuid
    libxscrnsaver
    libxtst
  ];

  unpackPhase = "true";

  installPhase = ''
    runHook preInstall

    dpkg-deb -x "$src" "$out"

    if [ -d "$out/usr/share" ]; then
      mkdir -p "$out/share"
      mv "$out/usr/share/"* "$out/share/"
      rmdir -p --ignore-fail-on-non-empty "$out/usr" || true
    fi

    mkdir -p "$out/bin"

    appDir="$out/opt/PokéClicker with Scripts"
    makeWrapper "$appDir/pokeclicker-desktop-with-scripts" "$out/bin/pokeclicker-desktop-with-scripts" \
      --add-flags "--no-sandbox"

    ln -s "$out/bin/pokeclicker-desktop-with-scripts" "$out/bin/pokeclicker-automation-desktop"

    desktopFile="$out/share/applications/pokeclicker-desktop-with-scripts.desktop"
    if [ -f "$desktopFile" ]; then
      substituteInPlace "$desktopFile" \
        --replace 'Exec="/opt/PokéClicker with Scripts/pokeclicker-desktop-with-scripts" %U' \
                  'Exec=pokeclicker-desktop-with-scripts %U'
    fi

    runHook postInstall
  '';

  autoPatchelfIgnoreMissingDeps = true;

  meta = with lib; {
    description = "PokéClicker desktop wrapper with scripts support (Farigh fork)";
    homepage = "https://github.com/Farigh/pokeclicker-automation-desktop";
    license = licenses.isc;
    platforms = [
      "x86_64-linux"
      "aarch64-linux"
    ];
    mainProgram = "pokeclicker-desktop-with-scripts";
    sourceProvenance = with sourceTypes; [ binaryNativeCode ];
  };
}
