#
# scopehal apps, including glscopeclient and optionally ngscopeclient
#
# vim: et:ts=2:sw=2:
#
{ deprekages, pkgs, lib, cmake, pkg-config, patchelf, ... }:
pkgs.stdenv.mkDerivation rec {
  pname = "scopehal-apps";
  version = "git";

  # If this is set to true, we'll also install ngscopeclient.
  includeUnstableNextGenClient = true;

  src = pkgs.fetchgit {
    url = "https://github.com/glscopeclient/scopehal-apps.git";
    rev = "617b6ee1230d19be4b883f676413664c3d7c0d7e";
    sha256 = "sha256-LJo1rcxDBYYngIU3+BiCV41HuQh/Ac/LG4wX9+IvVb8=";
    fetchSubmodules = true;
  };
  
  nativeBuildInputs = [ 
    cmake 
    pkg-config
    patchelf
    pkgs.git
  ];

  buildInputs = with pkgs; [ 
    deprekages.ffts 
    shaderc
    deprekages.vulkan-sdk

    # located via cmake module 
    glfw3
    glew
    libyamlcpp
    libpng

    # pkg-config
    gtkmm3
    pcre
    pcre2
    util-linux
    libselinux
    libsepol
    libthai
    libdatrie
  ];

  cmakeFlags = [ "-DCMAKE_BUILD_TYPE=Release" ];

  patches = [
    # Fix a format string vulnerability that's preventing us from building with -Werror=format-security.
    # Currently necessary due to https://github.com/glscopeclient/scopehal-apps/issues/571.
    (pkgs.writeText "fix-format-security.patch" ''
      --- scopehal-apps-0.0.1.orig/src/ngscopeclient/Dialog.cpp
      +++ scopehal-apps-0.0.1/src/ngscopeclient/Dialog.cpp
      @@ -207,7 +207,7 @@ void Dialog::HelpMarker(const string& he
       		ImGui::PushTextWrapPos(ImGui::GetFontSize() * 50);
       		ImGui::TextUnformatted(header.c_str());
       		for(auto s : bullets)
      -			ImGui::BulletText(s.c_str());
      +			ImGui::BulletText("%s", s.c_str());
       		ImGui::PopTextWrapPos();
       		ImGui::EndTooltip();
       	}
    '')
  ] 

  # If we're configured to, include ngscopeclient; though it's unstable enough that we
  # won't create a desktop entry for it, yet.
  ++ lib.lists.optional (includeUnstableNextGenClient) (pkgs.writeText "install-ng-client.patch" ''
       diff --git a/src/ngscopeclient/CMakeLists.txt b/src/ngscopeclient/CMakeLists.txt
       index 79171e0..b340af1 100644
       --- a/src/ngscopeclient/CMakeLists.txt
       +++ b/src/ngscopeclient/CMakeLists.txt
       @@ -153,3 +153,11 @@ target_link_libraries(ngscopeclient
               $${SIGCXX_LIBRARIES}
               )
       
       +
       +install(TARGETS ngscopeclient RUNTIME)
       +install(DIRECTORY fonts
       +       DESTINATION share/ngscopeclient)
       +install(DIRECTORY shaders
       +       DESTINATION share/ngscopeclient)
       +install(DIRECTORY icons
       +       DESTINATION share/ngscopeclient)
   '');

  # scopehal's cmake files expect to find vulkan libraries using the VULKAN_SDK environment variable.
  # (The top level's vulkan can be specified with -DVulkan_LIBRARY, but the environment variable is still
  #  used by the lower layers; which makes it better just to use this, here.
  preConfigure = "export VULKAN_SDK=${deprekages.vulkan-sdk}";

  # Link our e.g. stylesheets into places that Qt is willing to find them.
  # TODO: figure out if there's a build define that changes this
  postInstall = ''
    cp -r $out/share/glscopeclient/* $out/bin/
    cp -r $out/share/ngscopeclient/* $out/bin/
  '';

  meta = {
    description = "test and measurement tool control applications";

    longDescription =
      '' Tools for remote controlling test and measurement tools.
         Provides advanced visualization and decoding of output from oscilloscopes and control of various instruments.
      '';

    homepage = "https://www.antikernel.net/temp/glscopeclient-manual.pdf";
    license = pkgs.lib.licenses.bsd3;
  };
}
