#!/bin/sh
# RFab Companion App launcher (Linux). electron-builder.config.js puts this file in place of the
# Electron binary, which it renames to rfab-companion.bin (afterPack, Linux only).
#
# Chromium's sandbox needs unprivileged user namespaces or a root-owned SUID chrome-sandbox beside the
# binary. Inside an AppImage the helper can never be root-owned, and Ubuntu 23.10 and later block the
# namespaces by default (kernel.apparmor_restrict_unprivileged_userns = 1), so the app died at start:
# "The SUID sandbox helper binary was found, but is not configured correctly" (Bruno, Ubuntu 26.04,
# AppImage 1.3.2, 2026-10-22). When neither way can work, start without the sandbox: the window shows
# only our own bundled pages. The .deb sets the helper's mode at install, so there the sandbox stays on.
DIR="$(dirname "$(readlink -f "$0")")"
sysctl_is() { [ "$(cat "/proc/sys/$1" 2>/dev/null)" = "$2" ]; }
helper_ok() { [ -u "$DIR/chrome-sandbox" ] && [ "$(stat -c %u "$DIR/chrome-sandbox" 2>/dev/null)" = "0" ]; }
if ! helper_ok && { sysctl_is kernel/apparmor_restrict_unprivileged_userns 1 || sysctl_is kernel/unprivileged_userns_clone 0 || sysctl_is user/max_user_namespaces 0; }; then
  case " $* " in *" --no-sandbox "*) ;; *) set -- --no-sandbox "$@" ;; esac
fi
exec "$DIR/rfab-companion.bin" "$@"
