#! /bin/bash

DATA_HOME=${XDG_DATA_HOME:-$HOME/.local/share}
CONFIG_HOME=${XDG_CONFIG_HOME:-$HOME/.config}
SESSION_CONFIG="${CONFIG_HOME}/environment.d/01-chimera-session.conf"
SESSION_LIGHTDM_CONFIG="/etc/lightdm/lightdm.conf.d/10-chimeraos-session.conf"
SESSION_LIST=('desktop' 'desktop-xorg' 'steam' 'steam-plus' 'opengamepadui')
SELECTED_SESSION="$1"

mkdir -p "${CONFIG_HOME}/environment.d"

function print_session_list() {
	# detect active session
	CURRENT_SESSION="unknown"
	if grep "gnome" ${SESSION_LIGHTDM_CONFIG} &>/dev/null; then
		CURRENT_SESSION="desktop"
	fi

	if grep "gnome-xorg" ${SESSION_LIGHTDM_CONFIG} &>/dev/null; then
		CURRENT_SESSION="desktop-xorg"
	fi

	if grep "gamescope-session-steam" ${SESSION_LIGHTDM_CONFIG} &>/dev/null; then
		CURRENT_SESSION="steam"
	fi

	if grep "gamescope-session-steam-plus" ${SESSION_LIGHTDM_CONFIG} &>/dev/null; then
		CURRENT_SESSION="steam-plus"
	fi

	if grep "gamescope-session-opengamepadui" ${SESSION_LIGHTDM_CONFIG} &>/dev/null; then
		CURRENT_SESSION="opengamepadui"
	fi

	# print active and available sessions
	for t in ${SESSION_LIST[@]}; do
		if [ "${CURRENT_SESSION}" = "${t}" ]; then
			echo "* $t"
		else
			echo "  $t"
		fi
	done
}

function clean_steam_shortcuts() {
	# clear steam game desktop shortcut clutter
	grep -r --files-with-matches "Exec=steam steam://rungameid/" ${DATA_HOME}/applications/ | tr '\n' '\0' | xargs -0 -I {} rm {}
}

function desktop() {
	clean_steam_shortcuts

	# switch to Desktop
	sudo chimera-session-use-lightdm gnome-wayland
	if (systemctl -q is-active inputplumber.service); then
		sudo systemctl restart inputplumber || true
	fi
}

function desktop_xorg() {
	clean_steam_shortcuts

	# switch to Desktop (Xorg)
	sudo chimera-session-use-lightdm gnome-xorg
	if (systemctl -q is-active inputplumber.service); then
		sudo systemctl restart inputplumber || true
	fi
}

function gamescope_steam() {
	# switch to Steam Big Picture UI (Gamescope/Wayland)
	echo '' >${SESSION_CONFIG}
	sudo chimera-session-use-lightdm gamescope-session-steam
}

function gamescope_opengamepadui() {
	# switch to OpengamepadUI (Gamescope/Wayland)
	echo '' >${SESSION_CONFIG}
	sudo chimera-session-use-lightdm gamescope-session-opengamepadui
}

function gamescope_steam_plus() {
	# switch to Steam plus OpengamepadUI overlay (Gamescope/Wayland)
	echo '' >${SESSION_CONFIG}
	sudo chimera-session-use-lightdm gamescope-session-steam-plus
}

function print_invalid_session() {
	echo "Unknown or invalid session type: ${SELECTED_SESSION}"
	echo
	echo "Available session types:"
	print_session_list
}

# print current and available sessions when no argument specified
if [ -z "${SELECTED_SESSION}" ]; then
	print_session_list
	exit
fi

# print message when invalid session is specified
if [[ ! "${SESSION_LIST[*]}" =~ "${SELECTED_SESSION}" ]]; then
	print_invalid_session
	exit
fi

# apply the specified session type
if [ "${SELECTED_SESSION}" = "desktop" ]; then
	echo "Switching to desktop session"
	desktop
elif [ "${SELECTED_SESSION}" = "desktop-xorg" ]; then
	echo "Switching to desktop-xorg session"
	desktop_xorg
elif [ "${SELECTED_SESSION}" = "steam" ]; then
	echo "Switching to steam session"
	gamescope_steam
elif [ "${SELECTED_SESSION}" = "opengamepadui" ]; then
	echo "Switching to opengamepadui session"
	gamescope_opengamepadui
elif [ "${SELECTED_SESSION}" = "steam-plus" ]; then
	echo "Switching to steam-plus session"
	gamescope_steam_plus
fi
