#!/bin/bash
# Sets Hyprland as the preselected session on the login screen.
#
# This CachyOS install uses KDE's new "plasma-login-manager" (binary/service
# name `plasmalogin`), NOT sddm -- sddm isn't even installed
# (`pacman -Q sddm` fails, there's no sddm.service). Writing
# /var/lib/sddm/state.conf (the classic SDDM "remember last session" file)
# has no effect here.
#
# The correct mechanism, confirmed by `strings`-ing
# /usr/lib/qt6/plugins/plasma/kcms/systemsettings/kcm_plasmalogin.so (the
# System Settings module for the login screen), is the config key
# `PreselectedSession` under `[General]`, in /etc/plasmalogin.conf or a
# conf.d drop-in. This is independent of `[Autologin]`, which only applies
# if autologin is actually enabled (it isn't here -- a password is still
# required).
set -euo pipefail
cd "$(dirname "${BASH_SOURCE[0]}")"
source ./00-config.sh

SET_EXPECT=$(mktemp)
trap 'rm -f "$SET_EXPECT"' EXIT
cat > "$SET_EXPECT" <<EOF
#!/usr/bin/expect -f
set timeout 20
log_file -a /tmp/set_plasmalogin_default.log
spawn ssh -tt ${SSH_USER}@${VM_IP} {sudo mkdir -p /etc/plasmalogin.conf.d && printf '[General]\nPreselectedSession=hyprland.desktop\n' | sudo tee /etc/plasmalogin.conf.d/default_session.conf}
expect {
  -nocase -re {(password|passwort).*: ?\$} {
    send "${VM_PASSWORD}\r"
    exp_continue
  }
  eof
}
EOF
chmod +x "$SET_EXPECT"
rm -f /tmp/set_plasmalogin_default.log
"$SET_EXPECT"

echo ">>> Verifying:"
ssh "${SSH_USER}@${VM_IP}" "cat /etc/plasmalogin.conf.d/default_session.conf"
