#!/bin/bash
#

# Author/Copyright: Wolfgang Schweer <wschweer@arcor.de>
# Licence:          GPL2+
# first edited:     2019-11-21

version=2026-09-28

set -e

if [ -f /etc/debian-edu/config ]; then
	source /etc/debian-edu/config
fi

# Check if this script is sourced or executed directly.
( return 0 2>/dev/null ) && sourced=true || sourced=false

if [ "${sourced}" = "false" ]; then
	trap cleanup_debian_edu_ltspinstall_x2gominidesktop TERM INT EXIT
fi

function cleanup_debian_edu_ltspinstall_x2gominidesktop()
{
	trap - TERM INT EXIT
	umount -q ${target}/dev/pts || true
	umount -q ${target}/proc || true
	if grep -q BD "/etc/apt/sources.list" && [ -f "/media/cdrom/.disk/info" ] ; then
		umount "${target}/media/cdrom" || true
		umount "/media/cdrom" || true
	fi
	exit 0
}

function do_debian_edu_ltspinstall_x2gominidesktop() {

	set -e

	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

	# Use http/https proxy if configured.
	if [ -e /etc/environment ]; then
		. /etc/environment
	fi

	x2go_script="$(command -v x2gothinclient_create)"
	if [ -z "${x2go_script}" ]; then
		echo "ERROR: '$x2go_script' not found, install via \`apt-get install x2gothinclient-management\`"
		exit 1
	fi

	ltsp="$(command -v ltsp)"
	if [ -z "${ltsp}" ]; then
		echo "ERROR: '$ltsp' not found, install via \`apt-get install ltsp\`"
		exit 1
	fi

	cat <<EOF >> /etc/ltsp/ltsp.conf
MASK_SYSTEM_SERVICES="etckeeper"
EOF

	# Set distro codename value for both testing and stable release cases.
	if grep -q / /etc/debian_version ; then
		dist_codename="$(cat /etc/debian_version | cut -d/ -f1)"
	else
		dist_codename=$(lsb_release -sc)
	fi

	if [ -e /etc/environment ]; then
		. /etc/environment
	fi

	dist=${dist:-${dist_codename}}
	arch=${arch:-$(dpkg --print-architecture)}
	create_squashfs_img=${create_squashfs_img:-"no"}

	x2go_chroot="x2go-minidesktop+${dist}+${arch}"

	export TC_BASE="/srv/ltsp"
	export TC_CHROOT="${TC_BASE}/${x2go_chroot}"
	export TC_CONFIG="/srv/x2gothinclient/${x2go_chroot}/config"
	export TC_DISTRO_CODENAME="${dist}"
	export TC_DEBMIRROR_URL="http://deb.debian.org/debian"
	export TC_X2GO_DEBURL=""
	export TC_NONINTERACTIVE="true"
	export TC_FLAVOUR="minidesktop"

	x2gothinclient_create

	# Create optional SquashFS image file.
	if [ "yes" == "${create_squashfs_image}"  ] ; then
		${ltsp} image "${x2go_chroot}"
	fi

	# next modification avoids ltsp command error if lot of images are available.
	#ALL_IMAGES=1 ltsp kernel
	#ltsp initrd
	#if uname -r | grep -q 686 ; then
	#	mv /srv/tftp/ltsp/ltsp.img /srv/tftp/ltsp/x86_32/ltsp.img
	#else
	#	mv /srv/tftp/ltsp/ltsp.img /srv/tftp/ltsp/"$(uname -m)"/ltsp.img
	#fi

	# FIXME: Clean up ltsp.conf from specific items.
	#sed -i '/PRE_INIT_MAIN/d' /etc/ltsp/ltsp.conf
	#sed -i '/PRE_INIT_AUTOFS/d' /etc/ltsp/ltsp.conf
	#sed -i '/MASK_SYSTEM/d' /etc/ltsp/ltsp.conf

	# FIXME: Create ltsp initrd (ltsp.img) and move it to the right location.
	#ltsp initrd
	#mv /srv/tftp/ltsp/ltsp.img /srv/tftp/ltsp/${dlw}

	# Update iPXE menu and adjust the created file for Debian Edu needs.
	#ltsp ipxe
	#FIXME: sed -i 's#ltsp/ltsp.img#ltsp/${img}/ltsp.img#' /srv/tftp/ltsp/ltsp.ipxe

	# FIXME: 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
}

if [ "${sourced}" = "false" ]; then
	do_debian_edu_ltspinstall_x2gominidesktop
fi
