#!/bin/bash
#
# Debian Edu specific replacement for 'ltsp initrd' command.
# Author/Copyright:	2021-2023, Wolfgang Schweer <wschweer@arcor.de>
# Author/Copyright:	2026, Mike Gabriel <mike.gabriel@das-netzwerkteam.de>
# Licence:		GPL2+
# first edited:		2021-11-10

version=2026-11-11

set -e

usage() {
	cat <<EOF
Usage: $0 command

Update specific LTSP initrd file (ltsp.img) for each use case. This needs to be
done after the /etc/ltsp/ltsp.conf [clients] section has been modified.

Options:
	--help     Display the debian-edu-ltsp-initrd help message.
	--version  Output version information and exit.
EOF
}

thin_prepare() {
	type="${1}"

	if [ "${type}" = "bare" ]; then
		# Needed for thin client auto login user.
		mkdir -p /etc/ltsp/getty@tty1.service.d
		cat <<EOF > /etc/ltsp/getty@tty1.service.d/override.conf
[Service]
ExecStart=
ExecStart=-/usr/sbin/agetty -a thin --noclear %I $TERM
RestartSec=10
EOF

		# Needed for thin client autofs setup (USB mass storage support (rw mode).
		mkdir -p /etc/ltsp/autofs
		cat <<EOF > /etc/ltsp/autofs/extra.autofs
/- /etc/auto.usb0 --mode=0777 --timeout=3
EOF
		cat <<EOF > /etc/ltsp/autofs/auto.usb0
/usb0 -fstype=auto,rw,user,umask=000 :/dev/sda1
EOF

		# Needed for thin client auto login configuration (startx).
		mkdir -p /etc/ltsp/skel
		cat <<EOF > /etc/ltsp/skel/.profile
while true ; do
    startx
done
EOF

		# Needed for thin client auto login configuration (x2goclient start).
		cat <<EOF > /etc/ltsp/skel/.xinitrc
exec x2goclient --no-menu --thinclient --background=/usr/share/desktop-base/active-theme/login/background-nologo.svg --add-to-known-hosts --no-session-edit --close-disconnect
EOF

		# Needed for thin client x2goclient configuration.
		mkdir -p /etc/ltsp/skel/.x2goclient

		# Pick up x2go_desktop variabe from debian-edu-ltsp-install script run.
		if [ -r "/var/lib/debian-edu/debian-edu-ltsp-initrd_x2go-command" ]; then
			x2go_command="$(head -n1 "/var/lib/debian-edu/debian-edu-ltsp-initrd_x2go-command")"
		fi
		if [ -z "${x2go_command}" ]; then
			# helpless fallback that nearly always works in X2Go
			x2go_command=TERMINAL
		fi

		# Needed for thin client (x2goclient preconfigured session).
		cat /etc/debian-edu/thin/x2gothinclient_sessions.in \
		    | sed -e "s/@HOSTNAME@/$(hostname -s)/g" \
		          -e "s/@X2GO_COMMAND@/${x2go_command}/g" \
		    > /etc/ltsp/skel/.x2goclient/sessions

		# Needed for thin client x2goclient configuration.
		cat <<EOF > /etc/ltsp/skel/.x2goclient/settings
[toolbar]
show=false
EOF

		# Needed for thin client x2goclient configuration.
		cat <<EOF > /etc/ltsp/skel/.x2goclient/printing
[General]
pdfview=false
showdialog=true

[CUPS]
defaultprinter=

[print]
command=lpr
ps=false
startcmd=false
stdin=false

[view]
command=xpdf
open=true
EOF

	fi

}

thin_cleanup() {
	type="${1}"

	if [ "${type}" = "bare" ]; then
		rm -f /etc/ltsp/getty@tty1.service.d/override.conf
		rmdir /etc/ltsp/getty@tty1.service.d
		rm -Rf /etc/ltsp/autofs
	fi

	rm -Rf /etc/ltsp/skel
}

thin() {
	type="${1}"
	arch="${2}"
	# Add specific configuration.
	if [ "${type}" = "bare" ]; then
		cat <<EOF >> /etc/ltsp/ltsp.conf
POST_INIT_THIN_USER='useradd -G disk -m -d /run/home/thin -k /etc/ltsp/skel -r thin'
POST_INIT_SYSTEMD='mkdir /etc/systemd/system/getty@tty1.service.d && \
cp /etc/ltsp/getty@tty1.service.d/override.conf /etc/systemd/system/getty@tty1.service.d'
POST_INIT_AUTOFS='cp /etc/ltsp/autofs/extra.autofs /etc/auto.master.d && \
cp /etc/ltsp/autofs/auto.* /etc'
EOF
	fi
	# Create the ltsp.img file and move it to the right location.
	echo "Updating LTSP initrd for type: ${type}, arch: ${arch}:"
	ltsp initrd
	mv /srv/tftp/ltsp/ltsp.img "/srv/tftp/ltsp/x2go-${type}-${arch}/ltsp.img"
	echo "Moved /srv/tftp/ltsp/ltsp.img to /srv/tftp/ltsp/x2go-${type}-${arch}/"

	# Clean up ltsp.conf from image specific items.
	sed -i '/POST_INIT/d' /etc/ltsp/ltsp.conf
}

dlw() {
	# Add specific configuration.
	cat <<EOF >> /etc/ltsp/ltsp.conf
LIGHTDM_CONF="greeter-hide-users"
POST_INIT_USE_FQDN="sed -i '/10.0.2.2/ s/server/tjener.intern tjener/' /etc/hosts"
EOF
	# Create the ltsp.img file and move it to the right location.
	echo "Updating LTSP initrd for dlw:"
	ltsp initrd
	mv /srv/tftp/ltsp/ltsp.img /srv/tftp/ltsp/dlw/
	echo "Moved /srv/tftp/ltsp/ltsp.img to /srv/tftp/ltsp/dlw/"

	# Clean up ltsp.conf from image specific items.
	sed -i '/POST_INIT/d' /etc/ltsp/ltsp.conf
	sed -i '/LIGHTDM_CONF/d' /etc/ltsp/ltsp.conf
}

server() {

	# Add specific configuration.
	cat <<EOF >> /etc/ltsp/ltsp.conf
MASK_SYSTEM_SERVICES="etckeeper exim4 tftpd-hpa isc-dhcp-server xrdp xrdp-sesman"
EOF
	# Update the ltsp.img file and move it to the right location.
	echo "Updating LTSP initrd for use with the server's filesystem:"
	ltsp initrd
	if uname -r | grep -q 686 ; then
		mv /srv/tftp/ltsp/ltsp.img /srv/tftp/ltsp/x86_32/ltsp.img
		echo "Moved /srv/tftp/ltsp/ltsp.img to /srv/tftp/ltsp/x86_32/"
	else
		mv /srv/tftp/ltsp/ltsp.img /srv/tftp/ltsp/"$(uname -m)"/ltsp.img
		echo "Moved /srv/tftp/ltsp/ltsp.img to /srv/tftp/ltsp/x86_64/"
	fi

	# Clean up ltsp.conf from specific items.
	sed -i '/MASK_SYSTEM/d' /etc/ltsp/ltsp.conf
}

case "$1" in
	-h|--help) usage ; exit 0 ;;
	--version) echo $version ; exit 0 ;;
esac

if [ ! -r /etc/ltsp/ltsp.conf ]; then
	echo "NOTE: debian-edu-ltsp-install needs to run first, before using debian-edu-ltsp-initrd makes any sense."
	exit 0
fi

# Make Debian Edu profile available.
if [ -r /etc/debian-edu/config ] ; then
	. /etc/debian-edu/config
fi

# LTSP images base directory
BASE="/srv/tftp/ltsp"

if [ -d "$BASE"/x86_64 ] || [ -d "$BASE"/x86_32 ]; then server ; fi
if [ -d "$BASE"/dlw ] ; then dlw ; fi

# X2Go thin client variants.
variants="bare display desktop"
archs="amd64"

for v in ${variants}; do
	for a in ${archs}; do
		if [ -d "$BASE/x2go-${v}-${a}" ] ; then
			thin_prepare "${v}"
			thin "${v}" "${a}"
			thin_cleanup "${v}"
		fi
	done
done

case "$1" in
	-h|--help) usage ; exit 0 ;;
	--version) echo $version ; exit 0 ;;
	*) exit 0 ;;
esac
