#!/bin/bash
# setup-greetd-autologin
#
# Purpose: Make it easy to set up autologin for greetd
#
###############################################################################
#
# Copyright (C) 2025 Hunter Ellett <hunter2k1@disroot.org>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
###############################################################################

if [ ! $(id -u) = 0 ]; then
	dialog --msgbox "Please run this as root." 0 0
	exit 1
fi

setup_error() {
	dialog --msgbox "Something is wrong with your greetd-autologin installation. Please reinstall \
	the package and try again." 0 0
	exit 1
}

pick_user() {
	while true; do
	AUTOLOGIN_USER=$(dialog --inputbox "Please enter the name of the user you want to log into as:" 0 0 --output-fd 1)
	dialog --stdout --yesno "You chose $AUTOLOGIN_USER. Is this correct?" 0 0
	local confirm=$?
		if [ "$confirm" -eq 0 ]; then
			break
		fi
	done
}

pick_session() {
	while true; do
	choice=$(dialog --stdout --radiolist "Please choose the session you would like to use:" 0 0 7 \
	"KDE Wayland" "KDE Plasma Wayland" ON \
	"KDE X11" "KDE Plasma X11" off \
	"MATE" "MATE session" off \
	"XFCE" "XFCE X11 session" off \
	"XFCE Wayland" "XFCE Wayland session (experimental, requires labwc)" off \
	"Other" "Other session" off)

	dialog --stdout --yesno "You chose $choice. Is this correct?" 0 0
	session_confirm=$?
		if [ "$session_confirm" -eq 0 ]; then
			break
		fi
	done
}

pick_explicit_session() {
	while true; do
	EXPLICIT_SESSION=$(dialog --inputbox "Please enter the session command to run for autologin, keep in mind \
	this should include all commands needed to run the session:" 0 0 --output-fd 1)
	dialog --stdout --yesno "The command will be '$EXPLICIT_SESSION'. Is this correct?" 0 0
	local confirm=$?
		if [ "$confirm" -eq 0 ]; then
			break
		fi
	done
}

apply_settings() {
	for file in /usr/lib/greetd-autologin/*; do
		if [ -f "$file" ]; then
			sed -i "s/^user = \".*\"/user = \"$AUTOLOGIN_USER\"/" "$file"
		elif [ ! -f "$file" ]; then
			setup_error
		fi
	done

	if [ -f /usr/lib/greetd-autologin/custom ]; then
		sed -i "s/^command = \".*\"/command = \"$EXPLICIT_SESSION\"/" "/usr/lib/greetd-autologin/custom"
	elif [ ! -f /usr/lib/greetd-autologin/custom ]; then
		setup_error
	fi
}

# Run the steps
pick_user

pick_session

# I used this to make sure choice selection was working
# if [ "$choice" = "MATE" ]; then
# 	dialog --msgbox "This works" 0 0
# 	exit 0
# 	else
# 	setup_error
# fi

if [ "$choice" = "Other" ]; then
	pick_explicit_session
fi

apply_settings

if [ -d /etc/greetd ] && [ -d /usr/lib/greetd-autologin ]; then

	TOML="/etc/greetd/autologin.toml"

	if [ -h "$TOML" ]; then
		unlink $TOML
		else
		rm -f $TOML
	fi

	if [ "$choice" = "KDE Wayland" ]; then
		ln -s /usr/lib/greetd-autologin/kde-wayland $TOML
	elif [ "$choice" = "KDE X11" ]; then
		ln -s /usr/lib/greetd-autologin/kde-x11 $TOML
	elif [ "$choice" = "MATE" ]; then
		ln -s /usr/lib/greetd-autologin/mate $TOML
	elif [ "$choice" = "XFCE" ]; then
		ln -s /usr/lib/greetd-autologin/xfce $TOML
	elif [ "$choice" = "XFCE Wayland" ]; then
		ln -s /usr/lib/greetd-autologin/xfce-wayland $TOML
	elif [ "$choice" = "Other" ]; then
		ln -s /usr/lib/greetd-autologin/custom $TOML
	else
		setup_error
	fi
fi

dialog --msgbox "greetd-autologin has been set up. Please set GREETER=autologin in /etc/sysconfig/greetd and reboot." 0 0

exit 0
