{ lib
, fetchFromGitHub
, stdenv

, pkg-config
}:

let
  tar1090 = fetchFromGitHub {
    owner = "wiedehopf";
    repo = "tar1090";
    rev = "0678a884f18c4ce78b569a88340db3763ae45ff6";
    sha256 = "sha256:0000000000000000000000000000000000000000000000000000";
  };
  tar1090-db = fetchFromGitHub {
    owner = "wiedehopf";
    repo = "tar1090-db";
    rev = "bad4a563c52d71e791d4dbdb392e2257b3df7f17";
    sha256 = "sha256:0000000000000000000000000000000000000000000000000000";
  };
in
stdenv.mkDerivation rec {
  pname = "tar1090";
  version = "unstable-2022-10-06";

  src = tar1090;

  buildInputs = [
    libusb1
    ncurses
    zlib
    zstd

    librtlsdr
    libbladeRF
    libiio
    libad9361
  ];

  nativeBuildInputs = [
    pkg-config
  ];

  makeFlags = "RTLSDR=yes AIRCRAFT_HASH_BITS=15 HISTORY=yes BLADERF=yes PLUTOSDR=yes AGGRESSIVE=yes HAVE_BIASTEE=yes";
  enableParallelBuilding = true;

  installPhase = ''
    runHook preInstall

    install -d $out/bin
    install -m 0755 readsb $out/bin/readsb
    ln -s $out/bin/readsb $out/bin/viewadsb

    runHook postInstall
  '';
}