#!/bin/sh
set -eu

# ChatGPT is staged by apply_extra to /app/extra/chatgpt, and the resolved
# binary name is recorded in app.env at install time. Run it through
# zypak-wrapper (from org.electronjs.Electron2.BaseApp): zypak redirects
# Chromium's SUID sandbox onto the Flatpak sandbox, so renderer and GPU
# processes stay isolated without the Debian package's chrome-sandbox postinst
# and without --no-sandbox.
#
# Keep this wrapper thin so the upstream application tree stays unmodified:
# display, credential storage and the Chromium sandbox are handled by
# portals/zypak and the manifest permissions.
[ -f /app/extra/app.env ] || {
  echo "chatgpt: /app/extra/app.env is missing, reinstall the app" >&2
  exit 1
}
# shellcheck source=/dev/null  # written by apply_extra at install time
. /app/extra/app.env

unset ELECTRON_RUN_AS_NODE

# The tray backend claims the bus name
# "org.freedesktop.StatusNotifierItem-<getpid()>-1" and gives up if that name is
# refused. Inside the sandbox the app is always PID 2 in its own PID namespace,
# so every Flatpak app with a tray races for the same name. Report a fixed,
# distinctive PID instead, and grant exactly that one bus name in finish-args.
# The preload goes through ZYPAK_LD_PRELOAD rather than LD_PRELOAD: zypak-helper
# sets its own LD_PRELOAD for the Chromium processes and would drop ours.
export CHATGPT_FAKE_PID="${CHATGPT_FAKE_PID:-919191}"
export ZYPAK_LD_PRELOAD="/app/lib/chatgpt/libfakepid.so${ZYPAK_LD_PRELOAD:+:$ZYPAK_LD_PRELOAD}"

exec zypak-wrapper "$APP_BIN" --ozone-platform-hint=auto --enable-wayland-ime "$@"
