#!/bin/bash
#

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

version=2026-09-28

set -e
set -x #sgg

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

### FIXME: copy of dlw-debootstrap, needs to be reworked for creating an
### image from the LTSP host's file system. (Read LTSP docs to understand
### the concept

#exit 0
#sgg 

# Check parameters
while [ "$#" -gt 0 ]; do
    case "$1" in
        --create-squashfs-image) create_squashfs_image="true" ; shift ;;
        *) shift ;;
    esac
done


# 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_dlwltsphost TERM INT EXIT
fi

function cleanup_debian_edu_ltspinstall_dlwltsphost()
{
	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_dlwltsphost()
{

	set -e

	if echo "$PROFILE" | grep -Eq 'Main-Server' ; then
		echo "ERROR: Main-Server cannot be converted to a Diskless Workstation!"
		exit -1
	fi

	# Abort if create_squashfs_image is set to "no".
	if [ "true" != "${create_squashfs_image}"  ] ; then
		echo "ERROR: You must use --create-squashfs-image as command line parameter!"
		exit -1
	fi

	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

	# Debian Edu specific common additional image excludes; for diskless
	# workstations the /skole mountpoint (for autofs) needs to be clean.
	# This applies for both a combined server and a separate LTSP server.
	# For a combined server image the autofs service needs to be enabled (see below).
	if echo "$PROFILE" | grep -Eq 'Workstation|LTSP-Server' ; then
		cat <<EOF > /etc/ltsp/image-local.excludes
skole/*
EOF
	fi

	# FIXME: is this needed?
	#if [ -e /etc/environment ]; then
	#	. /etc/environment
	#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 exim4 tftpd-hpa isc-dhcp-server xrdp xrdp-sesman"
EOF

	# FIXME:
	# Temporary workaround needed in some cases for configuring the resolver inside
	# the SquashFS image.
	#rm -f /etc/resolv.conf
	#echo "nameserver $dns_server" > /etc/resolv.conf
	#echo "search intern" >> /etc/resolv.conf

	# FIXME: Temporary workaround needed to turn DLW images into machines of profile type 'Workstation'
	#sed -i /etc/debian-edu/config -e "s/^PROFILE=.*/PROFILE=\"Workstation\"/g"
	# Create SqashFS image.
	ltsp image /,,/boot,subdir=boot,,/usr,subdir=usr,,/var,subdir=var

	# FIXME: Revert resolver workaround from above.
	#rm -f /etc/resolv.conf
	#ln -s /run/resolvconf/resolv.conf /etc/resolv.conf

	# FIXME: Revert temporary change to PROFILE in /etc/debian-edu/config
	#sed -i /etc/debian-edu/config -e "s/^PROFILE=.*/PROFILE=\"${PROFILE}\"/g"
	# 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_dlwltsphost
fi
