#! /bin/bash -e

# = = = = = = = = = = = = = = = = = = = = = = =
# =     Copyright (C) 2015-2018 uib GmbH      =
# =           http://www.uib.de               =
# =          All rights reserved.             =
# = = = = = = = = = = = = = = = = = = = = = = =

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

VERBOSE=true
TFTPROOT="/var/lib/univention-client-boot"

case "$1" in
	configure)
		if [ -e "/etc/opsi/opsi.conf" ]; then
			fileadmingroup=$(grep "fileadmingroup" /etc/opsi/opsi.conf | cut -d "=" -f 2 | sed 's/ //g')
		else
			fileadmingroup=pcpatch
		fi

		chown -R opsiconfd:$fileadmingroup /tftpboot/linux
		chmod 2775 /tftpboot/linux
		chmod 2775 /tftpboot/linux/pxelinux.cfg
		find /tftpboot/linux -type f -exec chmod 664 {} \;
		if [ -L /tftpboot/linux/pxelinux.cfg/default ]; then
			link_destination=$(readlink /tftpboot/linux/pxelinux.cfg/default)

			if [ "$link_destination" == "/tftpboot/linux/pxelinux.cfg/default.menu" ]; then
				rm /tftpboot/linux/pxelinux.cfg/default
				echo "Removed broken absolute link to default"
			fi
		fi

		if [ ! -e /tftpboot/linux/pxelinux.cfg/default ]; then
			echo "link default is broken or non existing"
			echo "linking default to default.menu"
			if [ ! -e /tftpboot/linux/pxelinux.cfg/default -a -L /tftpboot/linux/pxelinux.cfg/default ]; then
				rm /tftpboot/linux/pxelinux.cfg/default
			fi
			ln -sf default.menu /tftpboot/linux/pxelinux.cfg/default
		fi
		serviceentry=$(opsi-admin --no-depot -Sd method config_getObjects '[]' '{"id":"clientconfig.configserver.url"}' | grep defaultValues | cut -d "'" -f2)
		if [ "$serviceentry" != "" ]; then
			sed -i "s,append.*$,& service=$serviceentry,g" /tftpboot/linux/pxelinux.cfg/default.menu
		fi
		opsi-setup --set-rights /tftpboot >/dev/null 2>/dev/null || true

		# check for Univention Corporate Server and link to directory
		if which ucr >/dev/null 2>/dev/null; then
			$VERBOSE && echo "Detected UCS System - moving tftpboot files into $TFTPROOT"
			if [ ! -d "$TFTPROOT" ]; then
				$VERBOSE && echo "Creating $TFTPROOT"
				mkdir $TFTPROOT
			fi

			if [ -L "$TFTPROOT"/pxelinux.cfg ]; then
				unlink "$TFTPROOT"/pxelinux.cfg
			elif [ -d "$TFTPROOT"/pxelinux.cfg ]; then
				rm /tftpboot/linux/pxelinux.cfg/default
				mv /tftpboot/linux/pxelinux.cfg/* "$TFTPROOT"/pxelinux.cfg
				rmdir /tftpboot/linux/pxelinux.cfg
			fi
			mv /tftpboot/linux/* $TFTPROOT/

			echo "removing /linux/directory from install-elilo templates"
			sed -i "s_/linux__g" $TFTPROOT/pxelinux.cfg/install-elilo-x64
			sed -i "s_/linux__g" $TFTPROOT/pxelinux.cfg/install-elilo-x86
		else
			exit 0
		fi
	;;

	abort-upgrade|abort-remove|abort-deconfigure)

	;;

	*)
		echo "postinst called with unknown argument \`$1'" >&2
		exit 1
	;;
esac

exit 0

