#
# Fixes armcord to work on wayland.
#
# vim: et:ts=2:sw=2:
#
{ ... }:
let
  overlay = final': prev': {
    armcord = prev'.armcord.overrideAttrs (prev: {

      installPhase = ''
        runHook preInstall

        mkdir -p "$out/bin"
        cp -R "opt" "$out"
        cp -R "usr/share" "$out/share"
        chmod -R g-w "$out"

        # Wrap the startup command
        makeWrapper $out/opt/ArmCord/armcord $out/bin/armcord \
          "''${gappsWrapperArgs[@]}" \
          --prefix XDG_DATA_DIRS : "${prev'.gtk3}/share/gsettings-schemas/${prev'.gtk3.name}/" \
          --add-flags "--ozone-platform=wayland --enable-features=WebRTCPipeWireCapturer" \
          --prefix LD_LIBRARY_PATH : "${prev'.lib.makeLibraryPath prev.buildInputs}" \
          --suffix PATH : ${prev'.lib.makeBinPath [ prev'.xdg-utils ]}

        # Fix desktop link
        substituteInPlace $out/share/applications/armcord.desktop \
          --replace /opt/ArmCord/ $out/bin/

        runHook postInstall
      '';
    });

  };
in
{
  nixpkgs.overlays = [ overlay ];
}
