#!/bin/sh
# Pre-Electron argv wrapper for Super Productivity on Linux.
#
# Forces --ozone-platform=x11 when launched inside *our* Snap sandbox on a
# Wayland session. This is load-bearing: the programmatic
# app.commandLine.appendSwitch('ozone-platform','x11') in electron/start-app.ts
# is not enough in practice — field reports on issue #7270 show Chromium's
# Ozone init dlopens libEGL/libgbm on the core22 Mesa path before
# appendSwitch is honored, which segfaults under host-vs-snap Mesa ABI
# drift. Putting the flag into argv before Electron's argv parser runs
# bypasses that.
#
# Same mechanism used by Signal Desktop and Mattermost Desktop snaps
# (snapcrafters/signal-desktop, snapcrafters/mattermost-desktop).
#
# All Linux launches pass through this wrapper so the app can set a stable
# WM_CLASS for desktop-file matching. Only our Snap on Wayland receives the
# extra ozone flag. The SNAP_NAME gate protects .deb/.rpm installs invoked via
# xdg-open from *another* snap (where $SNAP leaks into the child env).
#
# See docs/research/snap-wayland-gpu-fix-research.md §18.

# Derive the real ELF path. Inside our snap confinement, $SNAP is the
# revision mount root and is more reliable than $0 resolution through
# snapd's wrapper chain. Elsewhere, resolve $0 through symlinks — this
# handles /usr/bin/superproductivity symlinks from .deb/.rpm installs.
if [ -n "$SNAP" ] && [ "$SNAP_NAME" = "superproductivity" ]; then
  IS_OUR_SNAP=1
  BIN_DIR="$SNAP"
else
  IS_OUR_SNAP=
  SELF=$(readlink -f "$0" 2>/dev/null || echo "$0")
  BIN_DIR=$(dirname "$SELF")
fi
BIN="$BIN_DIR/superproductivity-bin"
# Must match linux.desktop.entry.StartupWMClass in electron-builder.yaml.
APP_CLASS="superproductivity"

# If the user already supplied --ozone-platform or --class on argv, don't
# override. Stop scanning at -- so positional args aren't misread as flags.
HAS_OZONE_PLATFORM=
HAS_APP_CLASS=
for arg in "$@"; do
  case "$arg" in
    --) break ;;
    --ozone-platform=* | --ozone-platform) HAS_OZONE_PLATFORM=1 ;;
    --class=* | --class) HAS_APP_CLASS=1 ;;
  esac
done

if [ -z "$HAS_APP_CLASS" ]; then
  set -- "--class=$APP_CLASS" "$@"
fi

if [ -n "$IS_OUR_SNAP" ] && [ -z "$HAS_OZONE_PLATFORM" ] && { [ "$XDG_SESSION_TYPE" = "wayland" ] || [ -n "$WAYLAND_DISPLAY" ]; }; then
  exec "$BIN" --ozone-platform=x11 "$@"
fi

exec "$BIN" "$@"
