#!/bin/sh
# donatepage-launcher - run donatepage as unprivileged user


uri="$1"


if [ "$(id -u)" -ne 0 ]; then
  exec /usr/bin/donatepage
fi

if [ -n "$SUDO_USER" ]; then
  user="$SUDO_USER"
fi

if [ -z "$user" ]; then
  echo "donatepage_launcher: could not determine graphical user; launching as root" >&2
  exec /usr/bin/donatepage
  exit
fi

# Resolve user UID and home
uid=$(id -u "$user" 2>/dev/null) || { echo "unknown user $user" >&2; exec /usr/bin/donatepage; }
homedir=$(eval echo "~$user")

# Export DBUS session bus for systemd user bus (works for Wayland/X11 with systemd)
export DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/$uid/bus"

# Try reasonable X11/XAUTH settings (may be overwritten by environment in some setups)
export DISPLAY="${DISPLAY:-:0}"
export XAUTHORITY="${XAUTHORITY:-$homedir/.Xauthority}"

# Run the GUI as the unprivileged user with the correct environment
# sudo -u runs the command as the user; we pass the env vars inline
exec sudo -u "$user" DBUS_SESSION_BUS_ADDRESS="$DBUS_SESSION_BUS_ADDRESS" \
     XAUTHORITY="$XAUTHORITY" DISPLAY="$DISPLAY" /usr/bin/donatepage
