#!/usr/bin/env bash
# greetd's default session: start the forgeOS greeter (Quickshell) inside a
# bare Hyprland, as the "greeter" user. Configured in /etc/greetd/config.toml.
set -u

# The greeter user's home is /, which it cannot write; keep caches and state
# in its runtime directory instead (created by logind for the greeter session).
runtime="${XDG_RUNTIME_DIR:-/tmp/forgeos-greeter-$(id -u)}"
mkdir -p "$runtime"
export XDG_CACHE_HOME="$runtime/cache"
export XDG_STATE_HOME="$runtime/state"
export XDG_DATA_HOME="$runtime/data"
export XDG_CONFIG_HOME="$runtime/config"

# The user offered by default: the first ordinary account (uid 1000 and up).
FORGEOS_GREETER_USER=$(getent passwd | awk -F: '$3 >= 1000 && $3 < 60000 && $7 !~ /(nologin|false)$/ { print $1; exit }')
export FORGEOS_GREETER_USER

# Hybrid laptops: start on the GPU that drives the internal display, as the
# desktop session does (forgeos-session).
if [[ -z ${AQ_DRM_DEVICES:-} ]]; then
	graphics_devices=$(/usr/lib/forge/graphics-devices)
	if [[ -n $graphics_devices ]]; then
		export AQ_DRM_DEVICES="$graphics_devices"
	fi
fi

# greetd starts the greeter without a locale; Qt needs UTF-8.
if [[ -r /etc/locale.conf ]]; then
	# shellcheck disable=SC1091
	. /etc/locale.conf
	export LANG
fi

export XDG_SESSION_TYPE=wayland
export QT_QPA_PLATFORM=wayland
exec /usr/bin/start-hyprland -- --config /etc/greetd/forgeos-hyprland.lua
