#!/bin/bash
# 获取实际登录用户（通过 logname 或 sudo 环境）
if [ -n "$SUDO_USER" ]; then
    REAL_USER="$SUDO_USER"
elif [ -n "$PKEXEC_UID" ]; then
    REAL_USER=$(getent passwd "$PKEXEC_UID" | cut -d: -f1)
else
    REAL_USER=$(logname 2>/dev/null || echo "$USER")
fi

# 设置显示环境
export DISPLAY=:0

# 查找正确的 .Xauthority 文件
if [ -f "/home/$REAL_USER/.Xauthority" ]; then
    export XAUTHORITY="/home/$REAL_USER/.Xauthority"
elif [ -f "/run/user/$(id -u "$REAL_USER")/gdm/Xauthority" ]; then
    export XAUTHORITY="/run/user/$(id -u "$REAL_USER")/gdm/Xauthority"
elif [ -f "/run/user/$(id -u "$REAL_USER")/wayland-0" ]; then
    # Wayland 环境（UOS 可能默认 X11，但兼容）
    export WAYLAND_DISPLAY="wayland-0"
fi

# 设置 DBus 会话地址
export DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/$(id -u "$REAL_USER")/bus"

# 执行主程序
exec /usr/bin/lib/uos-upgrade-assistant "$@"