#!/bin/sh
#
# calamares-install-aptosid — launch the aptosid Calamares installer as root
# while keeping it a client of the running graphical session.
#
# Calamares needs root, but pkexec scrubs the environment, which otherwise
# leaves the installer unable to reach the user's Wayland compositor (or X
# server) and unstyled. This wrapper captures the session's HOME, display and
# toolkit variables while it is still running as the invoking user, then
# re-execs itself under pkexec and re-exports them. Forwarding HOME (as the old
# "sudo -E" launcher did) lets KDE apps resolve the user's colour scheme, e.g.
# Breeze Dark, from ~/.config/kdeglobals - no system-wide duplicate needed.
# On Wayland the installer then maps with app_id "io.calamares.calamares" (so
# the aptosid Hyprland float rule applies); on X11
# it authenticates with the forwarded cookie, falling back to a temporary xhost
# grant when no XAUTHORITY is set.
#
# A polkit rule (/usr/share/polkit-1/rules.d/49-calamares-aptosid.rules)
# authorises the local active user to run this without a password, as needed on
# the live medium.

SELF=/usr/libexec/calamares-install-aptosid

# --- privileged side: re-export the forwarded (allowlisted) variables --------
if [ "$(id -u)" -eq 0 ]; then
    for kv in "$@"; do
        case "$kv" in
            HOME=*|WAYLAND_DISPLAY=*|XDG_RUNTIME_DIR=*|DISPLAY=*|XAUTHORITY=*|XDG_CURRENT_DESKTOP=*|QT_QPA_PLATFORM=*|QT_QPA_PLATFORMTHEME=*|QT_STYLE_OVERRIDE=*|QT_SCALE_FACTOR=*|XDG_CONFIG_DIRS=*|XDG_DATA_DIRS=*|XCURSOR_THEME=*|XCURSOR_SIZE=*|GTK_THEME=*)
                export "$kv"
                ;;
        esac
    done
    exec calamares
fi

# --- user side: forward the session environment through pkexec ----------------
xhost_granted=

cleanup() {
    [ -n "$xhost_granted" ] && xhost -si:localuser:root >/dev/null 2>&1
}
trap cleanup EXIT INT TERM

# On X11 without an explicit cookie, root cannot open the display; grant it
# transient access. When XAUTHORITY is set the forwarded cookie is enough.
if [ -n "${DISPLAY:-}" ] && [ -z "${XAUTHORITY:-}" ] && command -v xhost >/dev/null 2>&1; then
    xhost +si:localuser:root >/dev/null 2>&1 && xhost_granted=1
fi

# These differ in behaviour when set-but-empty vs unset - e.g. an empty
# WAYLAND_DISPLAY can push a toolkit to attempt Wayland and fail on X11 - so
# forward them only when actually set. The remaining variables below are
# harmless when empty and are forwarded unconditionally.
set --
[ -n "${WAYLAND_DISPLAY:-}" ] && set -- "$@" "WAYLAND_DISPLAY=${WAYLAND_DISPLAY}"
[ -n "${XDG_RUNTIME_DIR:-}" ] && set -- "$@" "XDG_RUNTIME_DIR=${XDG_RUNTIME_DIR}"
[ -n "${DISPLAY:-}" ]         && set -- "$@" "DISPLAY=${DISPLAY}"
[ -n "${XAUTHORITY:-}" ]      && set -- "$@" "XAUTHORITY=${XAUTHORITY}"
[ -n "${QT_QPA_PLATFORM:-}" ] && set -- "$@" "QT_QPA_PLATFORM=${QT_QPA_PLATFORM}"

pkexec "$SELF" \
    "HOME=${HOME}" \
    "XDG_CURRENT_DESKTOP=${XDG_CURRENT_DESKTOP:-}" \
    "QT_QPA_PLATFORMTHEME=${QT_QPA_PLATFORMTHEME:-}" \
    "QT_STYLE_OVERRIDE=${QT_STYLE_OVERRIDE:-}" \
    "QT_SCALE_FACTOR=${QT_SCALE_FACTOR:-}" \
    "XDG_CONFIG_DIRS=${XDG_CONFIG_DIRS:-}" \
    "XDG_DATA_DIRS=${XDG_DATA_DIRS:-}" \
    "XCURSOR_THEME=${XCURSOR_THEME:-}" \
    "XCURSOR_SIZE=${XCURSOR_SIZE:-}" \
    "GTK_THEME=${GTK_THEME:-}" \
    "$@"
