#!/bin/sh
# Launcher for the SmokeTunnel desktop app on Linux (2026-10-20, docs/client-linux.md): installed as /usr/bin/smoketunnel
# by the .deb, used by the AppImage's AppRun. It picks the rendering path before the app starts:
#
#   - Inside a virtual machine (VMware, VirtualBox, QEMU/KVM, Hyper-V, Parallels) the virtual GPU's OpenGL is the
#     known way to get a Flutter window drawn upside down and, under GNOME on Wayland, to take the compositor down
#     with it (vmwgfx, 2026-10-20). There the app runs on Mesa's software renderer and on X11 (Xwayland), which the
#     compositor can show without sharing GPU buffers.
#   - SMOKETUNNEL_GL=native keeps the GPU path anywhere; SMOKETUNNEL_GL=software forces the safe path anywhere.
#
# Everything else is passed through (a smoke:// link from the URL handler, --tab=account, ...).
app="${SMOKETUNNEL_APP:-/opt/smoketunnel/smoketunnel}"
mode="${SMOKETUNNEL_GL:-auto}"
if [ "$mode" = auto ]; then
  virt="$(systemd-detect-virt --vm 2>/dev/null || true)"
  case "$virt" in
    none|"") mode=native ;;
    *) mode=software ;;
  esac
fi
if [ "$mode" = software ]; then
  export LIBGL_ALWAYS_SOFTWARE=1
  export GALLIUM_DRIVER="${GALLIUM_DRIVER:-llvmpipe}"
  # llvmpipe starts one rasteriser thread per CPU and glibc one malloc arena per thread: on an 8-core VM the app
  # sat at 1 GB RSS (2026-10-20). Two threads draw this UI fine; the arenas are capped the same way.
  export LP_NUM_THREADS="${LP_NUM_THREADS:-2}"
  export MALLOC_ARENA_MAX="${MALLOC_ARENA_MAX:-2}"
  # X11 when a display is there (Xwayland under GNOME); Wayland-only sessions keep their backend
  if [ -n "$DISPLAY" ]; then export GDK_BACKEND=x11; fi
fi
exec "$app" "$@"
