#
# GPS navigation suite for cars
#
# vim: et:ts=2:sw=2:
#
{ pkgs, lib, cmake, glib, with_maps ? false, ... }:
pkgs.stdenv.mkDerivation rec {
  pname = "navit";
  version = "0.5.6";

  src = pkgs.fetchFromGitHub {
    owner = "navit-gps";
    repo = "navit";
    rev = "v${version}";
    sha256 = "sha256-ugT3AAZNwtf+npeEWs86cQq1engFJVkW5qgWpoGLFMo=";
  };

  # Maps, if included.
  map_archive = if with_maps then pkgs.fetchurl {
    url = "https://onedrive.live.com/download?resid=AF3B2FBA31DE500C%21161772&authkey=!AA9oOcY1Z0EcnW8";
    sha256 = "sha256-etAVQsseHfK4UTXIomDbPfRrjPtg8WgIdo2ZaOP7SkQ=";
  } else "";

  nativeBuildInputs = with pkgs; [ 
    cmake 
    libtool
    automake
    autoconf
    glib
    pkg-config
    saxon
    inkscape
    qt5.qmake
    qt5.wrapQtAppsHook
  ];

  buildInputs = with pkgs; [ 
    gettext
    glib
    gcc
    protobuf
    protobufc
    SDL
    SDL_image
    cegui
    freeglut
    quesoglc
    freetype
    espeak
    gpsd
    geoclue2
    fontconfig
    fribidi

    # GTK
    gtk2

    # Qt
    qt5.qtbase
    qt5.qtquick1
    qt5.qtquickcontrols
    qt5.qtwayland
    qt5.qt3d
    qt5.qtdeclarative
    qt5.qtmultimedia
    qt5.qtspeech

  ];


  cmakeFlags = [ 
    "-DCMAKE_BUILD_TYPE=Release" 
    "-DSAMPLE_MAP=n" 
    "-DGTK2_GDKCONFIG_INCLUDE_DIR=${pkgs.gtk2}/lib/gtk-2.0/include"
    "-DGTK2_GLIBCONFIG_INCLUDE_DIR=${pkgs.glib}/lib/glib-2.0/include"
  ];

  #
  # Map bundling.
  #

  mapHook = ''
    mkdir -p $out/share/navit/maps
    echo '<map type="binfile" data="${map_archive}" />' >> $out/share/navit/maps/world_map.xml
  '';

  # Create our bundled maps.
  postInstall = if with_maps then mapHook else "";

  meta = {
    description = "open source GPS/car-navigation frontend";
    homepage = "https://www.navit-project.org";
    license = pkgs.lib.licenses.gpl2;
  };
}
