#! /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;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 {} \;
		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-custom.cfg.old ]; then
			mv /tftpboot/grub/grub-custom.cfg /tftpboot/grub/grub-custom.cfg.opsi 
			mv /tftpboot/grub/grub-custom.cfg.old /tftpboot/grub/grub-custom.cfg
		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

		if [ -e /tftpboot/linux/pxelinux.cfg/grubx64.efi ]; then
			if [ -f /tftpboot/linux/pxelinux.cfg/grubx64.efi ]; then
				rm /tftpboot/linux/pxelinux.cfg/grubx64.efi
			fi
			if [ -L /tftpboot/linux/pxelinux.cfg/grubx64.efi ]; then
				unlink /tftpboot/linux/pxelinux.cfg/grubx64.efi 
			fi
		fi
		cd /tftpboot/linux/pxelinux.cfg/
		ln -sf opsi-netboot.efi grubx64.efi

		if which opsi-setup 2>/dev/null; then
			$VERBOSE && echo "running opsi-setup to patch configservice to PXE menus"
			opsi-setup --init-current-config || true 
		fi
		
		rm -rf /tftpboot/linux/pxelinux.cfg/default
		echo "link default is broken or non existing"
		echo "linking default to default.menu"
		cd /tftpboot/linux/pxelinux.cfg/ && ln -sf default.menu default

	;;

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

	;;

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

exit 0

