#!/bin/bash
set -e

# Allow root to connect to the Wayland/X11 display
xhost +SI:localuser:root > /dev/null 2>&1 || true

# Launch Ubiquity with only the essential display/D-Bus variables preserved.
# Ensure LANG, LANGUAGE, and LC_* variables are preserved so Ubiquity can
# correctly detect the user's preferred locale and apply it to the target system.
#
# LIBGL_ALWAYS_SOFTWARE=1 forces software rendering to avoid crashes caused
# by Ubiquity's ancient OpenGL APIs on modern GPUs (e.g., Intel Arc, NVIDIA RTX).
exec sudo --preserve-env=DBUS_SESSION_BUS_ADDRESS,XDG_DATA_DIRS,XDG_RUNTIME_DIR,GTK_THEME,XAUTHORITY,DISPLAY,PATH,SHELL,LANG,LANGUAGE,LC_CTYPE,LC_NUMERIC,LC_TIME,LC_COLLATE,LC_MONETARY,LC_MESSAGES,LC_PAPER,LC_NAME,LC_ADDRESS,LC_TELEPHONE,LC_MEASUREMENT,LC_IDENTIFICATION sh -c 'LIBGL_ALWAYS_SOFTWARE=1 exec ubiquity gtk_ui'
