#!/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_dlwdebootstrap TERM INT EXIT
else
	do_install_function="do_debian_edu_ltspinstall_dlwdebootstrap"
	do_cleanup_function="cleanup_debian_edu_ltspinstall_dlwdebootstrap"
fi

function cleanup_debian_edu_ltspinstall_dlwdebootstrap()
{
	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_dlwdebootstrap() {

	set -e

	# Generate chroot and SquashFS image (optional) for diskless workstation from
	# scratch via debootstrapping a fresh Debian Edu chroot.

	# The chroot is basically a workstation installation.
	# Petter Reinholdtsen's debian-edu-bless script is used to achieve it.

	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

	# 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"}

	dlw="dlw+${dist}+${arch}"
	target="/srv/ltsp/${dlw}"
	do_chroot="chroot ${target}"

	if [ "${sourced}" = "false" ] && [ -d "${target}" ]; then
		echo "ERROR: ${dlw} diskless workstation already created"
		exit 1
	elif [ "${sourced}" = "true" ] && [ -d "${target}" ]; then
		echo "INFO: ${dlw} diskless workstation already created, skipping creation"
		return 0
	fi

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

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

	mkdir -p  ${target}
	chmod 755 ${target}

	# Use BD-ISO if available.
	if [ "true" == "${BD_ISO}" ] && ! mountpoint -q /media/cdrom ; then
		mount /media/cdrom
	fi

	if grep -q BD /etc/apt/sources.list && [ -f /media/cdrom/.disk/info ] ; then
		mkdir -p ${target}/media/cdrom
		mount --bind /media/cdrom ${target}/media/cdrom
		mirror="file:///media/cdrom/"
	fi

	export DEBIAN_FRONTEND=noninteractive

	# Install base system
	${debootstrap} --arch="${arch}" --no-check-gpg --variant=minbase --include=locales,console-setup,linux-image-"${arch}" "${dist}" "${target}" "${mirror}"

	# Care for complete sources list (security, also nonfree -- in case firmware is needed).
	${do_chroot} sh -c "cp /usr/share/doc/apt/examples/debian.sources /etc/apt/sources.list.d/debian.sources"

	# Care for same locale and keyboard settings in the chroot, also useful to quiet down Perl.
	cp /etc/locale.gen "${target}/etc/"
	cp /etc/default/locale "${target}/etc/default"
	${do_chroot} locale-gen
	cp /etc/default/keyboard "${target}/etc/default"
	cp /etc/default/console-setup "${target}/etc/default"
	${do_chroot} setupcon -k

	# Care for special files.
	# Unset temporary directory variables to avoid problems when directory
	# is missing inside the chroot. https://bugs.debian.org/765443
	unset TMPDIR TEMP TEMPDIR TMP
	mount /dev/pts -t devpts "${target}/dev/pts"
	mount proc -t proc "${target}/proc"
	if grep -q BD /etc/apt/sources.list && mountpoint -q /media/cdrom ; then
		echo "deb [trusted=yes] $mirror $dist main" > "${target}/etc/apt/sources.list"
	fi
	mkdir -p "${target}/tmp/user/0/"
	cp /usr/share/debian-edu-config/tools/debian-edu-bless "${target}/sbin"

	# Install the mandatory ltsp package (and apt-utils to don't delay package configuration).
	${do_chroot} apt-get install -y -qq apt-utils ltsp

	# Turn system into a Debian Edu workstation.
	${do_chroot} sh -c "PROFILE=Workstation DESKTOP=$desktop /sbin/debian-edu-bless"
	mkdir -p "${target}/skole"
	chmod 755 "${target}/skole"
	rm -f "${target}/etc/resolv.conf"
	# FIXME: Next line needed for kerberized NFS home directory access.
	#cp /etc/krb5.keytab "${target}/etc/"
	# Care for Debian Edu certificates.
	cp /etc/ssl/certs/debian-edu-server.crt "${target}/etc/ssl/certs/"
	cp /usr/local/share/ca-certificates/*.crt "${target}/usr/local/share/ca-certificates/"
	${do_chroot} update-ca-certificates
	# Adjust Sitesummary configuration
	cp -f /etc/sitesummary/site "${target}/etc/sitesummary/"
	cp -f /etc/sitesummary/sitegroup "${target}/etc/sitesummary/"
	echo ltsp-client > "${target}/etc/sitesummary/hostclass"

	# Care for temporary mounts.
	umount "${target}/dev/pts"
	umount "${target}/proc"
	if grep -q BD "/etc/apt/sources.list" && [ -f "/media/cdrom/.disk/info" ] ; then
		umount "${target}/media/cdrom"
		umount "/media/cdrom"
	fi

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

	# 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_dlwdebootstrap
fi
