#!/bin/sh
set -e

if [ -d /var/service ]; then
	if [ -e /etc/default/roundup ]; then
		. /etc/default/roundup
	fi
	if [ $USERUNIT -eq 0 ]; then
		touch /etc/roundup/service/down
	fi
	x=`dpkg-query -W -f '${Status}' runit 2>/dev/null` || true
	if [ "X$x" != X ]; then
	    # runit is installed, now check the version:
	    y=`dpkg-query -W -f '${Version}' runit`
	    dpkg --compare-versions "$y" ge '1.8.0-2'
	    if [ $? -eq 1 ]; then
		# have an old version of 'runit':
		if [ ! -e /var/service/roundup ]; then
		    ln -s /etc/roundup/service /var/service/roundup
		fi
	    else
		update-service --add /etc/roundup/service roundup
	    fi
	fi
fi

if which update-python-modules >/dev/null 2>&1; then
	update-python-modules -a  roundup.public roundup.private
fi

if [ -x "/etc/init.d/roundup" ]; then
	update-rc.d roundup defaults >/dev/null
	update-rc.d roundup-dirs defaults 19 99
	if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
		invoke-rc.d roundup-dirs restart || exit $?
		invoke-rc.d roundup restart || exit $?
	else
		/etc/init.d/roundup-dirs restart || exit $?
		/etc/init.d/roundup restart || exit $?
	fi
fi
