#! /bin/bash -e

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

#DEBHELPER#

VERBOSE=true

UCSROLE=$(ucr get server/role)

case "$1" in
	configure)
		# run join script
		if [ "$UCSROLE" = "domaincontroller_master" -o "$UCSROLE" = "domaincontroller_backup" ]; then
			/usr/lib/univention-install/99opsi4ucs.inst || true
		fi

		if [ -d "/etc/opsi/.ucs_temp" -a -d "/var/www/" ]; then
			mkdir -p /var/www/icon/opsi || true
			mkdir -p /var/www/icon/50x50 || true

			if [ -e "/etc/opsi/.ucs_temp/configed.png" ]; then
				cp /etc/opsi/.ucs_temp/configed.png /var/www/icon/opsi/
				chmod 644 /var/www/icon/opsi/configed.png
			fi

			if [ -e "/etc/opsi/.ucs_temp/opsi-configed.png" ]; then
				cp /etc/opsi/.ucs_temp/opsi-configed.png /var/www/icon/50x50/
				chmod 644 /var/www/icon/50x50/opsi-configed.png
			fi

			rm -rf /etc/opsi/.ucs_temp
		fi

	;;

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

	;;

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

test -x /usr/sbin/univention-config-registry && univention-config-registry register opsi4ucs || true

exit 0
