#!/bin/sh
set -eu

# Cursor is an Electron app staged by apply_extra to /app/extra/cursor. Run it
# through zypak-wrapper from org.electronjs.Electron2.BaseApp so Chromium keeps
# its internal sandbox inside Flatpak. A generated host-bin directory is
# appended to PATH so the developer tools the integrated terminal and the agent
# shell out to — git, language toolchains, build and test runners — resolve to
# the user's own host installs, which is where their projects and credentials
# already are.

unset ELECTRON_RUN_AS_NODE

extra_root=/app/extra
launcher_file="$extra_root/cursor-launcher"
launcher="cursor"
if [ -r "$launcher_file" ]; then
  read -r recorded < "$launcher_file" || recorded=""
  [ -n "$recorded" ] && launcher="$recorded"
fi

host_bin="$(/app/lib/cursor/cursor-setup-host-bin || true)"
if [ -n "$host_bin" ] && [ -d "$host_bin" ]; then
  export PATH="/app/bin:$PATH:$host_bin"
else
  export PATH="/app/bin:$PATH"
fi

# Launch by absolute path and leave the working directory alone: it is
# inherited by the commands Cursor spawns, and a working directory that exists
# only inside the sandbox (such as the app tree) cannot be entered by anything
# forwarded to the host.
exec zypak-wrapper "$extra_root/cursor/$launcher" --ozone-platform-hint=auto "$@"
