#!/bin/sh -e

# Cause debconf to load up the debconf templates, which are used at purge
# time.
. /usr/share/debconf/confmodule
db_stop

if [ "$1" = configure -a -e /etc/mooix.conf ]; then
	. /etc/mooix.conf || true

	# Set proper umask for touching the moo object tree.
	umask 022

	# Only on new installs...
	if [ -z "$2" ]; then
		# Create all the moo's users and groups.
		adduser --group --quiet --home $MOOROOT --shell /bin/sh --system $MOOADMIN
		adduser --group --quiet --home $MOOROOT --shell /bin/sh --system $MOOENTRYGROUP
	fi

	# Add mooix group and make user be in that group (user owns no
	# files, so this is safe).
	if [ -n "$2" ] && dpkg --compare-versions "$2" lt 1.0rc3.pre0; then
		if [ -z "$MOOENTRYGROUP" ]; then
			echo "MOOENTRYGROUP is not set in /etc/mooix.conf!" >&2
			exit 1
		fi
		deluser --quiet $MOOENTRYGROUP
		adduser --group --quiet --home $MOOROOT --shell /bin/sh --system $MOOENTRYGROUP
	fi
	
	# Set up programs that must be suid+sgid to just created users.
	# This is probably not the best way to do this.
	for PROG in /usr/bin/moo /usr/bin/mooscript; do
		if ! dpkg-statoverride --list $PROG >/dev/null 2>&1; then
			dpkg-statoverride --update --add $MOOADMIN $MOOENTRYGROUP 6755 $PROG
		fi
	done
	# And those that need to be sgid.
	for PROG in /usr/bin/moologin; do
		if ! dpkg-statoverride --list $PROG >/dev/null 2>&1; then
			dpkg-statoverride --update --add root $MOOENTRYGROUP 2755 $PROG
		fi
	done
	

	# Add example entry to inetd.conf.
	if [ -x /usr/sbin/update-inetd -a -f /etc/inetd.conf ]; then
		if ! grep -q in.mooix /etc/inetd.conf; then
			update-inetd --group OTHER --add "#<disabled>#7777	stream	tcp	nowait	root		/usr/sbin/tcpd /usr/sbin/in.mooix"
		fi
	fi

	# A directory permission changed.
	if [ -n "$2" ] && dpkg --compare-versions "$2" lt 0.4.1.12.0; then
		chmod g+w /usr/lib/mooix/system/admin/contents
	fi

	# dbversion field didn't exist, so set it up on upgrade from old
	# versions. Don't want to inherit it.
	if [ -n "$2" ] && dpkg --compare-versions "$2" lt 0.5.99; then
		echo 0.01 > /var/lib/mooix/system/mooinfo/dbversion
	fi

	# Stupid mistake cleanup.
	if [ -n "$2" ] && [ -e /var/lib/mooix/system/mooinfo/dbversion ] && \
	   dpkg --compare-versions "$2" lt 0.6.16; then
		chown mooadmin:mooadmin /var/lib/mooix/system/mooinfo/dbversion
	fi

	# Update/create the system object tree.
	/usr/sbin/splittree $DISTOBJ $MOOROOT $DISTOBJ $MOOROOT $MOOADMIN 1

	# Bring up the moo daemon, but not the moo as a whole, to do
	# upgrade and so on. Check first to see if it is already running.
	PIDFILE=/var/run/mood.pid
	DAEMON=/usr/sbin/mood
	if ! start-stop-daemon --stop --signal 0 --quiet --pidfile $PIDFILE >/dev/null 2>&1; then
		if ! start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON; then
			echo Unable to start mood daemon >&2
			exit 1
		fi
	fi

	# Mood has to be running for mooupgrade.
	if [ ! -z "$2" ]; then
		/usr/sbin/mooupgrade -q -r -s "$DISTOBJ"
	fi
	
	# Record the currently installed db version, don't want this field to be
	# inherited as that would allow the value to change on upgrade.
	if [ ! -e $MOOROOT/system/mooinfo/dbversion ]; then
		cp $MOOROOT/system/mooinfo/parent/dbversion \
		   $MOOROOT/system/mooinfo/dbversion
		chown $MOOADMIN:$MOOADMIN $MOOROOT/system/mooinfo/dbversion
	fi

	# Compile the parser grammar, to make sure it is compiled even if there is
	# clock skew, and so that the first person to log in doesn't have to sit
	# through this.
	cd $MOOROOT/mixin/parser && runmeth compilegrammar >/dev/null

	# Bring down the moo daemon, but don't run the stophook stuff.
	start-stop-daemon --stop --quiet --pidfile $PIDFILE --exec $DAEMON
fi
	
# The moo starts up in full here (maybe, depending on local policy..)
#DEBHELPER#
