{ stdenv
, lib
, buildFHSEnv
, fetchurl
, makeWrapper
, gsettings-desktop-schemas
, opensc
, writeTextDir
, configText ? ""
}:

let
  version = "2503-8.15.0";
  sysArch = "armhf";
  mainProgram = "horizon-client";

  wrapBinCommands = path: name: ''
    makeWrapper "$out/${path}/${name}" "$out/bin/${name}_wrapper" \
      --set GTK_THEME Adwaita \
      --suffix XDG_DATA_DIRS : "${gsettings-desktop-schemas}/share/gsettings-schemas/${gsettings-desktop-schemas.name}" \
      --suffix LD_LIBRARY_PATH : "$out/lib/omnissa/horizon:$out/lib/omnissa/horizon/vdpService:$out/lib/omnissa"
  '';

  omnissaHorizonClientFiles = stdenv.mkDerivation {
    pname = "omnissa-horizon-armhf-files";
    inherit version;

    src = fetchurl {
      url = "https://download3.omnissa.com/software/CART26FQ1_LIN_2503_TARBALL/Omnissa-Horizon-Client-Linux-2503-8.15.0-14256322247.tar.gz";
      sha256 = "sha256-x98ITXF9xwzlPq375anQ2qBpMbZAcCqDVXBfvZPha7Q=";
    };

    nativeBuildInputs = [ makeWrapper ];

    installPhase = ''
      mkdir ext
      tar -xzf $src
      cd Omnissa-Horizon-Client-Linux-*/${sysArch}

      mkdir -p ext
      for archive in *.tar.gz; do
        tar -C ext --strip-components=1 -xf "$archive"
      done

      chmod -R u+w ext/usr/lib

      mkdir -p $out
      mv ext/usr $out
      mv ext/${sysArch}/lib $out/
      mv ext/${sysArch}/include $out/

      mkdir -p $out/lib/omnissa/horizon/pkcs11
      ln -s ${opensc}/lib/pkcs11/opensc-pkcs11.so $out/lib/omnissa/horizon/pkcs11/libopenscpkcs11.so

      chmod +x "$out/usr/bin/horizon-client"
      ${wrapBinCommands "usr/bin" "horizon-client"}
    '';
  };

  omnissaFHSUserEnv =
    pname:
    buildFHSEnv {
      inherit pname version;

      runScript = "${omnissaHorizonClientFiles}/bin/${pname}_wrapper";

      targetPkgs = pkgs: with pkgs; [
        atk
        cairo
        dbus
        file
        fontconfig
        freetype
        gdk-pixbuf
        glib
        gtk3
        libjpeg
        libpng
        libpulseaudio
        libtiff
        libuuid
        libv4l
        libxml2
        pango
        pcsclite
        pixman
        udev
        omnissaHorizonClientFiles
        xorg.libX11
        xorg.libXau
        xorg.libXcursor
        xorg.libXext
        xorg.libXi
        xorg.libXrandr
        xorg.libXrender
        xorg.libXtst
        zlib

        (writeTextDir "etc/omnissa/config" configText)
      ];
    };
in
stdenv.mkDerivation {
  pname = "omnissa-horizon-client";
  inherit version;

  dontUnpack = true;

  installPhase = ''
    mkdir -p $out/bin
    ln -s ${omnissaFHSUserEnv "horizon-client"}/bin/horizon-client $out/bin/
    ln -s ${omnissaFHSUserEnv "horizon-eucusbarbitrator"}/bin/horizon-eucusbarbitrator $out/bin/
  '';

  passthru.unwrapped = omnissaHorizonClientFiles;

  meta = {
    description = "Omnissa Horizon Client for ARM";
    homepage = "https://www.omnissa.com/products/horizon-8/";
    license = lib.licenses.unfree;
    platforms = [ "aarch64-linux" "armv7l-linux" ];
  };
}
