# Code from https://gist.github.com/lukalot/fcbf3216ad13b8303ab0947af0d5abd5
{pkgs, ...}: let
  pname = "cursor";
  version = "0.35.0";

  src = pkgs.fetchurl {
    # this will break if the version is updated.
    # unfortunately, i couldn't seem to find a url that
    # points to a specific version.
    # alternatively, download the appimage manually and
    # include it via src = ./cursor.AppImage, instead of fetchurl
    url = "https://downloader.cursor.sh/linux/appImage/x64";
    hash = "sha256-ZURE8UoLPw+Qo1e4xuwXgc+JSwGrgb/6nfIGXMacmSg=";
  };
  appimageContents = pkgs.appimageTools.extract {inherit pname version src;};
in
  with pkgs;
    appimageTools.wrapType2 {
      inherit pname version src;

      extraInstallCommands = ''
        install -m 444 -D ${appimageContents}/${pname}.desktop -t $out/share/applications
        substituteInPlace $out/share/applications/${pname}.desktop \
          --replace-quiet 'Exec=AppRun' 'Exec=${pname}'
        cp -r ${appimageContents}/usr/share/icons $out/share

        # Ensure the binary exists and create a symlink if it doesn't already exist
        if [ -e ${appimageContents}/AppRun ]; then
          install -m 755 -D ${appimageContents}/AppRun $out/bin/${pname}-${version}
          if [ ! -L $out/bin/${pname} ]; then
            ln -s $out/bin/${pname}-${version} $out/bin/${pname}
          fi
        else
          echo "Error: Binary not found in extracted AppImage contents."
          exit 1
        fi
      '';

      extraBwrapArgs = [
        "--bind-try /etc/nixos/ /etc/nixos/"
      ];

      # vscode likes to kill the parent so that the
      # gui application isn't attached to the terminal session
      dieWithParent = false;

      extraPkgs = pkgs: [
        unzip
        autoPatchelfHook
        asar
        # override doesn't preserve splicing https://github.com/NixOS/nixpkgs/issues/132651
        (buildPackages.wrapGAppsHook.override {inherit (buildPackages) makeWrapper;})
      ];
    }
