#!/bin/bash
# toggle-server-mode: switch between workstation and headless server mode.
#
# Server mode:
#   - System stays on when lid is closed (harmless on desktops)
#   - SDDM auto-login into the kiosk session (Glances dashboard)
#   - Switches to graphical.target if needed
#   - Glances service started
#
# Workstation mode:
#   - Default lid behaviour restored
#   - Workstation display manager and target restored
#   - Glances service stopped

if [[ $EUID -ne 0 ]]; then
    echo "This script must be run as root (sudo)." >&2
    exit 1
fi

# Load config from the invoking user's home (not root's)
INVOKE_USER="${SUDO_USER:-$USER}"
INVOKE_HOME=$(eval echo "~$INVOKE_USER")
CONFIG="$INVOKE_HOME/.config/headless-kiosk/config"
[[ -f "$CONFIG" ]] && source "$CONFIG"

KIOSK_USER="${KIOSK_USER:-$INVOKE_USER}"
KIOSK_SESSION="${KIOSK_SESSION:-kiosk}"
KIOSK_DISPLAY_MANAGER="${KIOSK_DISPLAY_MANAGER:-sddm}"
WORKSTATION_DISPLAY_MANAGER="${WORKSTATION_DISPLAY_MANAGER:-$KIOSK_DISPLAY_MANAGER}"
WORKSTATION_TARGET="${WORKSTATION_TARGET:-graphical.target}"

LOGIND_CONF=/etc/systemd/logind.conf.d/headless-server.conf

# Build autologin config for SDDM
sddm_autologin_conf() { cat << EOF
[Autologin]
User=$KIOSK_USER
Session=$KIOSK_SESSION
EOF
}
SDDM_CONF=/etc/sddm.conf.d/autologin.conf

if [[ -f "$LOGIND_CONF" ]]; then
    # ----------------------------------------------------------------
    # Switch to WORKSTATION mode
    # ----------------------------------------------------------------
    rm "$LOGIND_CONF"
    systemctl reload systemd-logind

    rm -f "$SDDM_CONF"
    systemctl disable --now glances

    if [[ "$KIOSK_DISPLAY_MANAGER" != "$WORKSTATION_DISPLAY_MANAGER" ]]; then
        systemctl disable --now "$KIOSK_DISPLAY_MANAGER" 2>/dev/null || true
        systemctl enable "$WORKSTATION_DISPLAY_MANAGER" 2>/dev/null || true
    fi

    if [[ "$WORKSTATION_TARGET" != "graphical.target" ]]; then
        systemctl set-default "$WORKSTATION_TARGET"
        systemctl isolate "$WORKSTATION_TARGET"
    fi

    echo "WORKSTATION mode enabled."
    echo "  - Lid behaviour restored"
    echo "  - Display manager: $WORKSTATION_DISPLAY_MANAGER"
    echo "  - Default target: $WORKSTATION_TARGET"
    echo "  - Glances service stopped"
else
    # ----------------------------------------------------------------
    # Switch to SERVER mode
    # ----------------------------------------------------------------
    mkdir -p "$(dirname "$LOGIND_CONF")"
    cat > "$LOGIND_CONF" << 'EOF'
# Headless server configuration.
# The system stays on when the lid is closed,
# whether on battery or AC power.

[Login]
HandleLidSwitch=ignore
HandleLidSwitchExternalPower=ignore
EOF
    systemctl reload systemd-logind

    if [[ "$KIOSK_DISPLAY_MANAGER" != "$WORKSTATION_DISPLAY_MANAGER" ]]; then
        systemctl disable "$WORKSTATION_DISPLAY_MANAGER" 2>/dev/null || true
        systemctl enable "$KIOSK_DISPLAY_MANAGER"
    fi

    mkdir -p "$(dirname "$SDDM_CONF")"
    sddm_autologin_conf > "$SDDM_CONF"

    systemctl set-default graphical.target
    systemctl enable --now glances
    systemctl isolate graphical.target

    echo "SERVER mode enabled."
    echo "  - System stays on when lid is closed"
    echo "  - Display manager: $KIOSK_DISPLAY_MANAGER (autologin -> kiosk)"
    echo "  - Default target: graphical.target"
    echo "  - Glances service started"
fi

echo ""
echo "Status:"
echo "  HandleLid      : $(systemd-analyze cat-config systemd/logind.conf | grep '^HandleLidSwitch=' | head -1 || echo 'default (suspend)')"
echo "  Glances        : $(systemctl is-active glances)"
echo "  DM autologin   : $(grep -q "Autologin" "$SDDM_CONF" 2>/dev/null && echo "yes (sddm / kiosk)" || echo "no")"
echo "  Default target : $(systemctl get-default)"
