#!/bin/sh
# rikkiti-session — bring up a full Rikkiti Wayland session (compositor + shell +
# session services). Invoked by greetd (or any display manager) as the session
# command; see install/usr/share/wayland-sessions/rikkiti.desktop and
# docs/04-session-glue.md.
#
# Flow: set env → ensure a session D-Bus → start secrets (gnome-keyring) + SSH
# agent (gcr-ssh-agent) → exec the compositor, which spawns the panel
# (RIKKITI_STARTUP) and the autostart list (copyq, nm-applet, portals, …).
set -eu

# ---- environment ----------------------------------------------------------
export XDG_SESSION_TYPE=wayland
export XDG_SESSION_DESKTOP=rikkiti
export XDG_CURRENT_DESKTOP=Rikkiti
export XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}"
# Toolkit Wayland hints. The goal is: prefer native Wayland, fall back to X11
# (XWayland) for apps that need it — never force a backend that can't fall back.
export MOZ_ENABLE_WAYLAND=1               # Firefox/Thunderbird native Wayland
export QT_QPA_PLATFORM="wayland;xcb"      # Qt: try Wayland (needs qtwayland), else XWayland
export ELECTRON_OZONE_PLATFORM_HINT=auto  # Electron (VSCode/Discord/…): Wayland if available, else X11
export CLUTTER_BACKEND=wayland
export _JAVA_AWT_WM_NONREPARENTING=1      # Java/AWT/Swing (e.g. SimpleHelp) under a non-reparenting WM
# NOT set on purpose: GDK_BACKEND (let GTK auto-pick wayland/x11) and
# SDL_VIDEODRIVER (forcing it breaks games that need X11; SDL auto-detects).
export XCURSOR_THEME="${XCURSOR_THEME:-Adwaita}"
export XCURSOR_SIZE="${XCURSOR_SIZE:-24}"
# virtio-gpu's hardware cursor plane renders flipped/offset (QEMU/VM) — force a
# software cursor there so the visible pointer matches the real one. Real GPUs
# (intel/amd/nvidia) keep hardware cursors for efficiency.
for _drv in /sys/class/drm/card*/device/driver; do
	case "$(basename "$(readlink -f "$_drv" 2>/dev/null)" 2>/dev/null)" in
		virtio*) export WLR_NO_HARDWARE_CURSORS=1 ;;
	esac
done

# ---- session D-Bus --------------------------------------------------------
# The tray, notifications, secrets, and portals all need ONE session bus shared
# with the apps. If the login stack didn't already give us one (systemd --user),
# re-exec ourselves wrapped in a fresh one so everything below shares it.
if [ -z "${DBUS_SESSION_BUS_ADDRESS:-}" ] && [ -z "${RIKKITI_HAVE_BUS:-}" ]; then
	export RIKKITI_HAVE_BUS=1
	exec dbus-run-session -- "$0" "$@"
fi
# Export our Wayland/desktop env to the bus so D-Bus-activated services
# (portals, etc.) and `systemctl --user` units inherit it.
command -v dbus-update-activation-environment >/dev/null 2>&1 && \
	dbus-update-activation-environment --systemd \
		XDG_CURRENT_DESKTOP XDG_SESSION_TYPE XDG_RUNTIME_DIR 2>/dev/null || true

# ---- secrets: gnome-keyring (Secret Service + PKCS#11) --------------------
# pam_gnome_keyring (in greetd's PAM stack) unlocks it with the login password;
# --start connects to that daemon and exports its control vars for this session.
# SSH is intentionally NOT requested here — gcr-ssh-agent (below) owns that now.
if command -v gnome-keyring-daemon >/dev/null 2>&1; then
	eval "$(gnome-keyring-daemon --start --components=secrets,pkcs11 2>/dev/null)" || true
	export GNOME_KEYRING_CONTROL GNOME_KEYRING_PID
fi

# ---- SSH agent: gcr-ssh-agent --------------------------------------------
# The modern split-out of gnome-keyring's SSH agent (gcr-4). Serves keys over
# SSH_AUTH_SOCK and prompts to unlock them via the keyring. ssh adds keys on
# first use (set `AddKeysToAgent yes` in ~/.ssh/config to persist them).
for gcr in /usr/libexec/gcr-ssh-agent /usr/lib/gcr-ssh-agent /usr/lib/*/gcr-ssh-agent; do
	if [ -x "$gcr" ]; then
		mkdir -p "$XDG_RUNTIME_DIR/gcr"
		# Subshell so gcr-ssh-agent reparents to init (it daemonizes) — otherwise its
		# launcher zombies under the compositor, which doesn't reap children.
		( "$gcr" "$XDG_RUNTIME_DIR/gcr" >/dev/null 2>&1 & )
		export SSH_AUTH_SOCK="$XDG_RUNTIME_DIR/gcr/ssh"
		break
	fi
done

# ---- XDG user dirs --------------------------------------------------------
# Create ~/Documents, ~/Downloads, ~/Pictures, … (+ ~/.config/user-dirs.dirs) so
# the file manager has its standard places/bookmarks. Idempotent; no-op once set.
command -v xdg-user-dirs-update >/dev/null 2>&1 && xdg-user-dirs-update 2>/dev/null || true

# ---- GTK theming ----------------------------------------------------------
# Regenerate ~/.config/gtk-{3,4}.0/settings.ini + gtk.css from the Rikkiti config
# (accent + dark from appearance.conf; Noto Sans + Papirus defaults) so GTK apps
# are themed out-of-box without ever opening Settings. Headless, fast, idempotent.
command -v rikkiti-settings >/dev/null 2>&1 && rikkiti-settings --apply-gtk >/dev/null 2>&1 || true

# ---- greeter theme cache --------------------------------------------------
# Publish this user's accent + wallpaper to /var/lib/rikkiti/greeter so the login
# screen shows their own theme next time (the greeter runs as _greetd and can't
# read 0700 home dirs). Best-effort; never blocks login.
command -v rikkiti-greeter-export >/dev/null 2>&1 && rikkiti-greeter-export 2>/dev/null || true

# ---- compositor + shell ---------------------------------------------------
# rikkiti-comp spawns the panel (RIKKITI_STARTUP) and reads ~/.config/rikkiti/
# autostart.conf (copyq, nm-applet, the polkit agent, …).
export RIKKITI_STARTUP="${RIKKITI_STARTUP:-rikkiti-panel}"
exec rikkiti-comp
