#!/bin/sh
set -eu

# tldraw offline is an Electron app staged by apply_extra to
# /app/extra/tldraw-offline. Run it through zypak-wrapper (from
# org.electronjs.Electron2.BaseApp): zypak redirects Chromium's SUID sandbox
# onto the Flatpak sandbox, so the app keeps an internal sandbox rather than
# running with --no-sandbox. --ozone-platform-hint=auto lets Chromium use
# Wayland when available and fall back to X11. Flatpak redirects $HOME, so the
# app's own state (working copies, recent-file list) stays sandboxed; documents
# themselves are reached through the portal.
#
# Defensively clear ELECTRON_RUN_AS_NODE: if it leaks in from the launching
# environment (e.g. a terminal inside VS Code/Electron), the binary would start
# as plain Node instead of the GUI app.
unset ELECTRON_RUN_AS_NODE

# apply_extra resolves the launcher's upstream name from the .deb's own .desktop
# file and records it next to the app tree — upstream renamed it once already
# (`@tldesktop` -> `tldraw-offline` in v1.12.0), so it is not hardcoded here.
launcher="$(cat /app/extra/launcher 2>/dev/null || true)"
[ -n "$launcher" ] || launcher=tldraw-offline
exec zypak-wrapper "/app/extra/tldraw-offline/$launcher" --ozone-platform-hint=auto "$@"
