#!/usr/bin/sh
# driftwm-session — launched by display manager (GDM) instead of bare driftwm.
# Re-execs through login shell to pick up .profile / PAM env vars,
# then imports the full environment into systemd/D-Bus so services like
# gnome-keyring (PAM-unlocked by GDM) are visible to all activated units.

if [ -z "$_DRIFTWM_WRAPPER" ]; then
    export _DRIFTWM_WRAPPER=1
    exec "$SHELL" -l -c "$0 $@"
fi

# Import the full login manager environment into systemd and D-Bus.
# GDM's PAM already started and unlocked gnome-keyring — this makes
# SSH_AUTH_SOCK, GNOME_KEYRING_CONTROL, etc. visible to D-Bus-activated
# services so they find the unlocked daemon instead of socket-activating
# a fresh locked one.
systemctl --user import-environment
hash dbus-update-activation-environment 2>/dev/null && \
    dbus-update-activation-environment --all

# Notify systemd that the graphical session is starting, then active.
# Services with PartOf=graphical-session.target (waybar, portals, etc.)
# will auto-start and auto-stop with the compositor.
systemctl --user start graphical-session-pre.target
systemctl --user start graphical-session.target

# Run driftwm with logs captured for debugging.
DRIFTWM_LOG="${XDG_RUNTIME_DIR:-/tmp}/driftwm.log"
driftwm 2>"$DRIFTWM_LOG"
STATUS=$?

# Compositor exited — tear down the graphical session so dependent
# services stop cleanly instead of becoming orphans.
systemctl --user stop graphical-session.target
systemctl --user stop graphical-session-pre.target
systemctl --user unset-environment WAYLAND_DISPLAY DISPLAY
