#! /bin/bash -e

# = = = = = = = = = = = = = = = = = = = = = = =
# =     Copyright (C) 2015-2019 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

case "$1" in
	configure)
		TFTPROOT="/tftpboot/linux"
		cfg="/etc/opsi/opsi.conf"
		if [ ! -e $cfg -a -e "${cfg}.dpkg-new" ]; then
		    cfg="${cfg}.dpkg-new"
		fi 
		if [ -e $cfg ]; then
			fileadmingroup=$(grep "^fileadmingroup" $cfg | 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
		opsi-setup --set-rights /tftpboot >/dev/null 2>/dev/null || true

		if [ -e $TFTPROOT/pxelinux.cfg/default.nomenu.old ]; then
			echo "recreating configuration files"
			mv $TFTPROOT/pxelinux.cfg/default.nomenu $TFTPROOT/pxelinux.cfg/default.nomenu.opsi
			mv $TFTPROOT/pxelinux.cfg/default.menu $TFTPROOT/pxelinux.cfg/default.menu.opsi
			mv $TFTPROOT/pxelinux.cfg/install-x64 $TFTPROOT/pxelinux.cfg/install-x64.opsi
			mv $TFTPROOT/pxelinux.cfg/install3264 $TFTPROOT/pxelinux.cfg/install3264.opsi
			mv $TFTPROOT/pxelinux.cfg/install-elilo-x64 $TFTPROOT/pxelinux.cfg/install-elilo-x64.opsi
			mv $TFTPROOT/pxelinux.cfg/elilo.conf $TFTPROOT/pxelinux.cfg/elilo.conf.opsi
			mv $TFTPROOT/pxelinux.cfg/default.nomenu.old $TFTPROOT/pxelinux.cfg/default.nomenu
			mv $TFTPROOT/pxelinux.cfg/default.menu.old $TFTPROOT/pxelinux.cfg/default.menu
			mv $TFTPROOT/pxelinux.cfg/install-x64.old $TFTPROOT/pxelinux.cfg/install-x64
			mv $TFTPROOT/pxelinux.cfg/install3264.old $TFTPROOT/pxelinux.cfg/install3264
			mv $TFTPROOT/pxelinux.cfg/install-elilo-x64.old $TFTPROOT/pxelinux.cfg/install-elilo-x64
			mv $TFTPROOT/pxelinux.cfg/elilo.conf.old $TFTPROOT/pxelinux.cfg/elilo.conf
		fi
		if [ -e /tftpboot/grub/grub.cfg.old ]; then
			mv /tftpboot/grub/grub.cfg /tftpboot/grub/grub.cfg.opsi
			mv /tftpboot/grub/grub.cfg.old /tftpboot/grub/grub.cfg
			sed -i '/if \[ $pipefilefile \]; then/,+2d' /tftpboot/grub/grub.cfg
			sed -i '/if \[ $pipefilefile \]; then/,+2d' /tftpboot/grub/grub.cfg.opsi
			if ! grep -qF "source" /tftpboot/grub/grub.cfg; then sed -i '/^pipefile=.*/a source "$pipefile"' /tftpboot/grub/grub.cfg; fi
			if ! grep -qF "source" /tftpboot/grub/grub.cfg.opsi; then sed -i '/^pipefile=.*/a source "$pipefile"' /tftpboot/grub/grub.cfg.opsi; fi
		fi
		if [ -f "$TFTPROOT/pxelinux.cfg/install-grub-x64.old" ]; then
			mv $TFTPROOT/pxelinux.cfg/install-grub-x64 $TFTPROOT/pxelinux.cfg/install-grub-x64.opsi 
			mv $TFTPROOT/pxelinux.cfg/install-grub-x64.old $TFTPROOT/pxelinux.cfg/install-grub-x64
		fi
	 	sed -i s/.bz2//g $TFTPROOT/pxelinux.cfg/install-grub-x64*
	 	sed -i s/.bz2//g $TFTPROOT/pxelinux.cfg/install-elilo-x64*
	 	sed -i s/.bz2//g $TFTPROOT/pxelinux.cfg/install-x64*
	 	sed -i s/.bz2//g $TFTPROOT/pxelinux.cfg/install3264*
	 	sed -i s/.bz2//g $TFTPROOT/pxelinux.cfg/default*
	 	sed -i s/.bz2//g $TFTPROOT/pxelinux.cfg/elilo.conf*
	 	sed -i s/.bz2//g /tftpboot/grub/grub.cfg*

		if which ucr >/dev/null 2>/dev/null; then
			$VERBOSE && echo "Detected UCS System - moving tftpboot files into /tftpboot/"
			rm -rf /tftpboot/miniroot*
			rm -rf /tftpboot/vmlinuz*
			cp -r $TFTPROOT/* /tftpboot/ || true
			if [ ! -L  /tftpboot/pxelinux.cfg ]; then
				rm -rf /tftpboot/pxelinux.cfg
				cd /tftpboot/
				ln -sf ./linux/pxelinux.cfg ./pxelinux.cfg
			fi
		fi

	;;

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

	;;

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

exit 0

