#!/usr/bin/env bash

/usr/libexec/os-session-select gamescope
if [[ $(grep -q "autologin=true" "$HOME/.config/gamescope-session-type") ]] ; then
        if [[ "$XDG_CURRENT_DESKTOP" == "GNOME" ]] ; then
                exec gnome-session-quit --no-prompt
        fi
        if [[ "$XDG_CURRENT_DESKTOP" == "KDE" ]] ; then
                VERSION=$(grep -E '^X-KDE-PluginInfo-Version=' /usr/share/xsessions/plasmax11.desktop 2>/dev/null | cut -d'=' -f2)
                if [[ "$VERSION" =~ ^6\..* ]]; then
                        exec qdbus org.kde.Shutdown /Shutdown org.kde.Shutdown.logout
                else
                        exec qdbus org.kde.ksmserver /KSMServer logout 0 0 0
                fi
        fi
        if [[ "$XDG_CURRENT_DESKTOP" == "MATE" ]] ; then
                exec mate-session-save --force-logout
        fi
        if [[ "$XDG_CURRENT_DESKTOP" == "XFCE" ]] ; then
                exec xfce4-session-logout --logout
        fi
        if [[ "$XDG_CURRENT_DESKTOP" == "Hyprland" ]] ; then
                exec loginctl terminate-user "$(id -u)"
        fi
fi
