#! /bin/bash -e

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

case "$1" in
	configure)
		fileadmingroup=$(grep "fileadmingroup" /etc/opsi/opsi.conf | cut -d "=" -f 2 | sed 's/\s*//g')
		if [ -z "$fileadmingroup" ]; then
			fileadmingroup=pcpatch
		fi
		if [ $fileadmingroup != pcpatch -a -z "$(getent group $fileadmingroup)" ]; then
			groupmod -n $fileadmingroup pcpatch
		else
			if [ -z "$(getent group $fileadmingroup)" ]; then
				groupadd $fileadmingroup
			fi
		fi

		if [ -z "`getent passwd pcpatch`" ]; then
			useradd --system -g $fileadmingroup -d /var/lib/opsi -s /bin/bash pcpatch
		fi

		if [ -z "`getent passwd opsiconfd`" ]; then
			useradd --system -g $fileadmingroup -d /var/lib/opsi -s /bin/bash opsiconfd
		fi

		if [ -z "`getent group opsiadmin`" ]; then
			groupadd opsiadmin
		fi

		chown -R root:$fileadmingroup /etc/opsi/backendManager
		find /etc/opsi/backendManager -type d -exec chmod 770 {} \;
		find /etc/opsi/backendManager -type f -exec chmod 660 {} \;
		chown -R root:$fileadmingroup /etc/opsi/backends
		chmod 770 /etc/opsi/backends
		chmod 660 /etc/opsi/backends/*.conf
		chown root:$fileadmingroup /etc/opsi/opsi.conf
		chmod 660 /etc/opsi/opsi.conf

		test -e /etc/opsi/pckeys || touch /etc/opsi/pckeys
		chown root:$fileadmingroup /etc/opsi/pckeys
		chmod 660 /etc/opsi/pckeys

		test -e /etc/opsi/passwd || touch /etc/opsi/passwd
		chown root:$fileadmingroup /etc/opsi/passwd
		chmod 660 /etc/opsi/passwd

		[ -e "/etc/opsi/backendManager/acl.conf" ]      || ln -s /etc/opsi/backendManager/acl.conf.default      /etc/opsi/backendManager/acl.conf
		[ -e "/etc/opsi/backendManager/dispatch.conf" ] || ln -s /etc/opsi/backendManager/dispatch.conf.default /etc/opsi/backendManager/dispatch.conf

		update-python-modules python-ldaptor.public >/dev/null 2>/dev/null || true
		update-python-modules -p >/dev/null 2>/dev/null || true
	;;

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

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

#DEBHELPER#
