{ stdenv, pkgs, fetchurl, lib, unzip, autoPatchelfHook, makeWrapper, ... }:

stdenv.mkDerivation rec {
  pname = "xenia";
  version = "latest";
  src = fetchurl {
    url = "https://dsp56300.com/builds/xenia/beta/DSP56300Emu-1.4.4-Linux_x86_64-Xenia-CLAP.zip";
    sha256 = "sha256-UdWkQYiG5J30SvESrHZWfk7PFlKfUDHsbFtRoVukX3s=";
  };
  nativeBuildInputs = [ makeWrapper unzip autoPatchelfHook ];
  buildInputs = with pkgs; [
    freetype
    fontconfig
    alsa-lib
    pipewire
    mesa
    freeglut
    pkg-config
    stdenv.cc.cc.lib
    xorg.libX11
    xorg.libXcursor
    xorg.libXrandr
    xorg.libXinerama
    xorg.libXcomposite
  ];

  unpackPhase = ''
    unzip $src
  '';

  installPhase = ''
    mkdir -p $out/lib/clap
    cp -r usr/local/lib/clap/Xenia.clap $out/lib/clap/
  '';

  postFixup = ''
    patchelf --set-rpath "${lib.makeLibraryPath buildInputs}" $out/lib/clap/Xenia.clap
  '';

  meta = with lib; {
    homepage = "https://dsp56300.wordpress.com/xenia/";
    description = "Xenia";
    platforms = platforms.linux;
  };
}
