#
# libsigrok with added DreamSourceLab device support, from DSView
#
# vim: et:ts=2:sw=2:
#
{ lib
, pkgs
, pkg-config
, cmake
, libzip
, boost
, fftw
, libusb1
, python3
, fetchpatch
, glib
}:
pkgs.stdenv.mkDerivation rec {
  pname = "libsigrok4DSL";

  version = "1.2.2";

  src = pkgs.fetchFromGitHub {
    owner = "DreamSourceLab";
    repo = "DSView";
    rev = "v${version}";
    sha256 = "sha256-QaCVu/n9PDbAiJgPDVN6SJMILeUO/KRkKcHYAstm86Q=";
  };

  patches = [
    # Build specifically libsigrok4DSL, not the rest of dsview.
    ./only_libsigrok.patch
  ];

  nativeBuildInputs = [ cmake pkg-config ];

  buildInputs = [
    libusb1
    glib
  ];

  cmakeFlags = [
    "-DCMAKE_BUILD_TYPE=Release"
  ];

  postInstall = ''
    mkdir -p $out/share/pkgconfig

    # Convert our libsigrok to the structure scopehal likes.
    mkdir -p $out/include/libsigrok4DSL
    cp $out/include/*.h $out/include/libsigrok4DSL/

    mkdir -p $out/include/log
    cp $out/include/xlog.h $out/include/log/

    # Also generate a pkg-config file.
    cat << EOF > $out/share/pkgconfig/libsigrok4DSL.pc
    prefix = $out
    libdir = $out/lib
    includedir = $out/include

    Name: libsigrok4DSL
    Description: libsigrok with DreamSourceLab devices
    Version: ${version}

    Libs: -L$out/lib -lsigrok4DSL -lglib-2.0
    Cflags: -I$out/include -I${glib.dev}/include/glib-2.0 -I${glib.out}/lib/glib-2.0/include
    EOF
  '';

  meta = with lib;
    {
      description = "sigrok with support for instruments from DreamSourceLab, including logic analyzer, oscilloscope, etc";
      homepage = "https://www.dreamsourcelab.com/";
      license = licenses.gpl3Plus;
      platforms = platforms.linux;
    };
}
