#!/usr/bin/bash

source /usr/share/Navix-WSL/0-functions

grep -q "release 9" /etc/redhat-release 2>/dev/null
(( $? == 0 )) && is_rhel9=1

# {{{ function help ()
function help {
	cat <<-EOL
		${bwhite}Usage:${normal} systemd-wsl [command]
		${bwhite}Commands${normal}:
		          ${bwhite}on${normal}        Enable systemd in WSL
		          ${bwhite}off${normal}       Disable systemd in WSL
		          ${bwhite}status${normal}    Show systemd status in WSL

	EOL

	exit 1
}
# }}}

# {{{ function remove_wlsconf_entries ()
function remove_wlsconf_entries {
	sed -ri '/\[boot\]/d' "${WSLCONF}" 2> /dev/null
	sed -ri '/systemd[[:space:]]*=.*/d' "${WSLCONF}" 2> /dev/null
}
# }}}

# {{{ function enable_systemd ()
function enable_systemd {
	(( is_enable == 1 )) && {
		echo "Systemd startup configuaration is ${byellow}already set${normal} to enabled in WSL."
		exit 0
	}

	if (( is_set == 1 )); then
		sed -ri 's/systemd[[:space:]]*=.*/systemd=true/' "${WSLCONF}"
	else
		remove_wlsconf_entries
		if [[ -f ${WSLCONF} ]]; then
			sed -ri '1i [boot]\nsystemd=true\n' "${WSLCONF}"
		else
			cat <<-EOL > "${WSLCONF}"
				[boot]
				systemd=true
			EOL
		fi
	fi

	ls -al /etc/systemd/system/systemd-logind.service 2>/dev/null | grep -q -- '-> /dev/null'
	(( $? == 0 )) &&
		rm -f /etc/systemd/system/systemd-logind.service

	check_status
	if (( is_enable != 1 )); then
		echo "${bred}[ERROR]${normal} Failed to enable systemd in WSL."
		exit 1
	fi
	cat <<-EOL
		Systemd has been ${bgreen}enabled${normal} in WSL. Please ${byellow}restart${normal} your WSL instance.

		    🔄 ${shutdown_command}
		    🔄 wsl -d ${WSL_DISTRO_NAME}

	EOL
}
# }}}

# {{{ function disable_systemd ()
function disable_systemd {
	(( is_enable == 0 )) && {
		echo "Systemd startup configuration is ${byellow}already set${normal} to disabled in WSL."
		exit 0
	}

	(( is_set == 1 )) &&
		sed -ri 's/systemd[[:space:]]*=.*/systemd=false/' "${WSLCONF}"
	check_status

	if (( is_enable == 1 )); then
		echo "${bred}[ERROR]${normal} Failed to disable systemd in WSL."
		exit 1
	fi
	cat <<-EOL
		Systemd has been ${bcyan}disabled${normal} in WSL. Please ${byellow}restart${normal} your WSL instance.

		    🔄 ${shutdown_command}
		    🔄 wsl -d ${WSL_DISTRO_NAME}

	EOL
}
# }}}

# {{{ function status_systemd ()
function status_systemd {
	local imogi=( "❌" "✅")

	cat <<-EOL
		📊 ${bmagenta}Systemd status in WSL${normal}

		    ${imogi[run]} Systemd is currently running
		    ${imogi[is_enable]} Systemd enabled in wsl.conf

	EOL

	(( is_rhel9 == 1 )) && {
		cat <<-EOL
			In WSL2, when using systemd of RHEL9 series including Navix 9,
			the wsl -t command may cause an incomplete shutdown of systemd-logind.
			Therefore, please use the wsl --shutdown command instead of wsl -t,
			or use the shutdown now command on the WSL Linux OS to shut down the system.

			${byellow}In conclusion${normal},
			do not use ${bred}wsl -t${normal} to terminate,

			but use for shutdown commands such as:

			🔄 ${bcyan}wsl --shutdown${normal} or
			🔄 ${bgreen}shell>${normal} ${bwyite}shutdown now${normal}

		EOL
	}

	(( is_enable == 1 )) && return 0 || return 1
}
# }}}

# {{{ function check_status ()
function check_status {
	[[ -z ${run} ]] && {
		[[ ! -f /usr/bin/pidof ]] &&
			dnf -y install procps-ng 2>/dev/null >& /dev/null

		pidof systemd >& /dev/null
		(( $? == 0 )) && run=1 || run=0
	}

	grep -q "^[[:space:]]*systemd[[:space:]]*=" ${WSLCONF} 2>/dev/null
	(( $? == 0 )) && is_set=1 || is_set=0
	grep -q "^[[:space:]]*systemd[[:space:]]*=[[:space:]]*true" ${WSLCONF} 2>/dev/null
	(( $? == 0 )) && is_enable=1 || is_enable=0
}
# }}}

(( $UID != 0 )) && {
	echo "Please run as root or use sudo."
	exit 1
}

WSLCONF="/etc/wsl.conf"

pidof systemd >& /dev/null
(( $? == 0 )) && run=1 || run=0

if (( is_rhel9 == 1 )); then
	shutdown_command="wsl --shutdown"
	(( run == 1 )) &&
		shutdown_command+=" or ${bgreen}shell>${normal} systermctl poweroff"
else
	shutdown_command="wsl -t ${WSL_DISTRO_NAME}"
fi

check_status

case "$1" in
	on)
		enable_systemd
		;;
	off)
		disable_systemd
		;;
	status)
		status_systemd
		;;
	*)
		help
		;;
esac

exit $?

# vim: filetype=sh noet si sw=4 ts=4 fdm=marker
