{ protobuf
, pkg-config
, h2o
, openssl
, eigen
, ncurses5
, zstd
, zlib
, abseil-cpp
, curl
, gitMinimal
, stdenv
, fetchFromGitHub
, lib
, buildAll ? false
}:

let
  core = "ubxtool rtcmtool navrecv navnexus navparse";
  noncore = if buildAll then "navdump navcat reporter rinreport galmonmon navdisplay" else "";
in
stdenv.mkDerivation {
  pname = "galmon";
  version = "git-master";

  src = fetchFromGitHub {
    owner = "berthubert";
    repo = "galmon";
    rev = "refs/heads/master";
    hash = "sha256-NSuZYs6oFOB4dP2ypdVq6OZRgXSBHXFskH4I7uMTu+8=";
    fetchSubmodules = true;
  };

  patches = [ ./patches/galmon-fix-linker.patch ];

  buildInputs = [
    protobuf pkg-config h2o openssl eigen ncurses5 zstd zlib curl gitMinimal abseil-cpp
  ];

  buildPhase = ''
    make ${core} ${noncore}
  '';

  installPhase = ''
    mkdir -p $out/bin
    mv ${core} ${noncore} $out/bin
    # avoid collision with gpsd's ubxtool
    mv $out/bin/ubxtool $out/bin/galmon-ubxtool
    ${lib.optionalString buildAll "cp -r html $out/"}
  '';
}