#!/bin/sh

[ "$(id -u "${SUBJECT}")" -ge 1000 ] || exit 0

# Enable auto-rotate and the on-screen keyboard (user units)
systemctl --global enable auto-rotate.service xbindkeys.service mithra-osk.service

# Enable autologin
mkdir -p /etc/sddm.conf.d
echo -e "[Autologin]\nUser=${SUBJECT}\nSession=sonicde" > /etc/sddm.conf.d/00-autologin.conf

# Add user to mandatory groups. Add each independently: usermod -aG validates
# the whole list up front and aborts atomically if any group is missing, so a
# single absent optional group (e.g. pipewire before its sysusers entry is
# processed) must not strip the user of video/audio/input. Losing video locks
# the session out of the GPU nodes (/dev/nvhost-gpu, /dev/dri/*) -> kwin_x11
# SIGSEGVs on GL init and the KDE session hangs before the desktop.
# pipewire: grants RT scheduling rights to libpipewire-module-rt via
# /etc/security/limits.d/25-pw-rlimits.conf, preventing audio underruns/skips.
for grp in wheel video audio input pipewire; do
	getent group "${grp}" >/dev/null 2>&1 && usermod -aG "${grp}" "${SUBJECT}"
done

# Remove openbox session file
rm -f /usr/share/xsessions/openbox.desktop
