#!/usr/bin/sh

# Start under new service unit, to detach from the current non-gui logind
# session
if [ -z "$INVOCATION_ID" ]; then
    exec systemd-run --system \
        --unit=qubes-gui-agent-session.service \
        --description="Start GUI session via Xephyr" \
        --property=TTYPath=/dev/tty8 \
        --property=StandardInput=tty \
        --wait \
        -- "$0" "$@"
fi

# do not let child processes believe they are running as qrexec service
# directly
unset QREXEC_REMOTE_DOMAIN
unset QREXEC_SERVICE_FULL_NAME
unset QREXEC_SERVICE_ARGUMENT
unset QREXEC_REQUESTED_TARGET
unset QREXEC_REQUESTED_TARGET_TYPE
unset QREXEC_AGENT_PID

DISPLAY_XORG=:1
DISPLAY_XEPHYR=:0

XSESSION="/etc/X11/xinit/xinitrc"
if [ -f /etc/X11/Xsession ]; then
    # Debian-based distro, set Xsession appropriately
    XSESSION="/etc/X11/Xsession qubes-session"
fi

# Wait for Xorg display (started by qubes-run-xorg)
if ! timeout 30s sh -c "while ! xdpyinfo -display $DISPLAY_XORG > /dev/null 2>&1; do sleep 1; done"; then
    echo "Timed out waiting for Xorg display $DISPLAY_XORG"
    exit 1
fi

# parameters for logind session
export DISPLAY=$DISPLAY_XEPHYR
export XDG_SEAT=seat0
export XDG_SESSION_CLASS=user

# Run xsession into Xephyr
user=$(qubesdb-read --default=user /default-user)
exec /usr/bin/qubes-gui-runuser $user /usr/bin/env env DISPLAY=$DISPLAY_XORG /usr/bin/xinit $XSESSION -- /usr/bin/qubes-run-xephyr $DISPLAY_XEPHYR > ~/.xsession-errors 2>&1
