#!/bin/sh

set -e

# Versions older than 0.4.3 didn't support runlevel S properly.
# The easiest way to fix this is to change everything back
# to the link structure and then back again to the runlevel file.
#
if [ "$1" = "configure" -a -n "$2" ]
then
    if dpkg --compare-versions $2 lt 0.4.3
    then
	echo "Updating runlevel.conf to support new runlevel S."
	/usr/lib/file-rc/rcfile2link.sh && rm -f /etc/runlevel.conf /etc/runlevel.fallback
	if [ -f /etc/rc0.d/S05halt -o -f /etc/rc6.d/S05reboot ]
	then
	    echo "Correcting handling of halt and reboot"
	    [ -f /etc/rc0.d/S05halt ] && mv -f /etc/rc0.d/S05halt /etc/rc0.d/S90halt
	    [ -f /etc/rc6.d/S05reboot ] && mv -f /etc/rc6.d/S05reboot /etc/rc6.d/S90reboot
	fi
    fi
fi

if [ -d /etc/rc0.d -a ! -f /etc/runlevel.conf ]; then
	/usr/lib/file-rc/rclink2file.sh > /etc/runlevel.conf
	cp /etc/runlevel.conf /etc/runlevel.fallback
	( cd / && tar czf /var/backups/rc-links.tar.gz etc/rc?.d )
	rm -rf /etc/rc[0-6S].d
fi
dpkg-divert --package file-rc --add --rename \
	--divert /usr/sbin/update-rc.d.links /usr/sbin/update-rc.d
cp /usr/lib/file-rc/update-rc.d /usr/sbin/update-rc.d
dpkg-divert --package file-rc --add --rename \
	--divert /etc/init.d/rc.links /etc/init.d/rc
cp /usr/lib/file-rc/rc /etc/init.d/rc
dpkg-divert --package file-rc --add --rename \
	--divert /etc/init.d/rcS.links /etc/init.d/rcS
cp /usr/lib/file-rc/rcS /etc/init.d/rcS

#DEBHELPER#
