#!/bin/sh
set -eu

# CodeBurn is an Electron app staged by apply_extra to /app/extra/codeburn. 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.
#
# The CLI that reads and prices the session logs is bundled in the upstream
# build (resources/cli); the app runs it with its own Electron binary in Node
# mode, so nothing here reaches for a host Node, Python or agent CLI.
#
# 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

# Priced-model tables from LiteLLM, exchange rates and the parsed-session cache
# default to $HOME/.cache/codeburn, which is a tmpfs here — every launch would
# re-download and re-parse. CODEBURN_CACHE_DIR is the app's own knob for that
# location; point it at the per-app cache directory, which persists and needs no
# filesystem grant.
: "${CODEBURN_CACHE_DIR:=${XDG_CACHE_HOME:-$HOME/.cache}/codeburn}"
export CODEBURN_CACHE_DIR

# OpenCode resolves its data directory through XDG_DATA_HOME, which Flatpak
# points inside the sandbox, so the read-only grant for the real directory would
# never be looked at. OPENCODE_DATA_DIR is OpenCode's own override and takes
# precedence; assign it only when unset, so a `flatpak override --env=` can move
# it (for a renamed or forked build, say).
: "${OPENCODE_DATA_DIR:=$HOME/.local/share/opencode}"
export OPENCODE_DATA_DIR

# apply_extra resolves the launcher's upstream name from the .deb's own .desktop
# file and records it next to the app tree, so an upstream rename does not need
# a change here.
launcher="$(cat /app/extra/launcher 2>/dev/null || true)"
[ -n "$launcher" ] || launcher=codeburn
exec zypak-wrapper "/app/extra/codeburn/$launcher" --ozone-platform-hint=auto "$@"
