#! /bin/sh
#
# debian.postinst
#		Creates /dev/initctl if not already there.
#		Also checks /etc/inittab for single user entry.
#
# Version:	debian.postinst  2.08  12-Dec-1997  miquels@cistron.nl
#


case "$1" in
	configure)
		;;
	abort-upgrade|abort-remove|abort-deconfigure)
		exit 0
		;;
esac
umask 022

#
#	Check /dev/initctl
#
if [ ! -p /dev/initctl ]
then
	echo "Creating /dev/initctl"
	rm -f /dev/initctl
	mknod -m 600 /dev/initctl p
fi

#
#	Install sample network file if there isn't one yet.
#
if [ ! -f /etc/init.d/network ]
then
	if [ -f /etc/init.d/network.saved ]
	then
		cp -a /etc/init.d/network.saved /etc/init.d/network
	else
		cp -a /usr/doc/sysvinit/examples/network /etc/init.d/network
	fi
fi
rm -f /etc/init.d/network.saved
if [ ! -f /etc/inittab ]
then
	if [ -f /etc/inittab.saved ]
	then
		cp -a /etc/inittab.saved /etc/inittab
	else
		cp -a /usr/doc/sysvinit/examples/inittab /etc/inittab
	fi
fi
rm -f /etc/inittab.saved
if [ ! -f /etc/default/rcS ]
then
	cp /usr/doc/sysvinit/examples/default.rcS /etc/default/rcS
fi

#
#	See if there were saved variables.
#
if [ -f /etc/default/rcS.sed ]
then
	cp /etc/default/rcS /etc/default/rcS.TMP
	sed -f /etc/default/rcS.sed < /etc/default/rcS.TMP > /etc/default/rcS
	if [ -s /etc/default/rcS ]
	then
		rm -f /etc/default/rcS.TMP
	fi
	rm -f /etc/default/rcS.sed
fi

#
#	Check single user entry in /etc/inittab
#
if grep -q '^[^:]\{1,4\}:S:' /etc/inittab
then
	if ! grep -q '^[^:]\{1,4\}:S:wait:/sbin/sulogin' /etc/inittab
	then
		cat <<EOF

You have a single user entry in /etc/inittab, but it doesn't call
/sbin/sulogin - consider changing this manually after installation.
(change into "~~:S:wait:/sbin/sulogin").

EOF
		echo -n "Hit Enter key to continue...."
		read junk
	else
		echo "You have a single user entry in /etc/inittab -- good."
	fi
else
	cat <<EOF

You don't have a single user entry in your /etc/inittab file -
do you want me to change your inittab so that the sulogin
program is called in single user mode (prompts you for the
EOF
	echo -n "root password) [y] "
	read res
	case "$res" in
	  ""|y*|Y*)
		# Add single user entry.
		rm -f /tmp/init1.$$ /tmp/init2.$$
		head -2 /etc/inittab > /tmp/init1.$$
		echo >> /tmp/init1.$$
		echo '# What to do in single user mode.' >> /tmp/init1.$$
		echo '~~:S:wait:/sbin/sulogin' >> /tmp/init1.$$
		tail +3 /etc/inittab >> /tmp/init1.$$
		cp -a /etc/inittab /etc/inittab.dpkg-old
		cp /tmp/init1.$$ /etc/inittab	
		rm -f /tmp/init1.$$
		;;
	  *)
		echo "Okay."
		;;
	esac
fi


#
#	Fix up /etc/inittab for the new rcS
#
if grep -q /etc/init.d/boot /etc/inittab
then
	sed -e 's!^.*\(sysinit\|boot\|bootwait\):/etc/init.d/boot.*$!si::sysinit:/etc/init.d/rcS!' < /etc/inittab > /etc/inittab.tmp
	if [ ! -s /etc/inittab.tmp ]
	then
		echo "$0: installation severely fubared !! HELP" >&2
		exit 1
	fi
	mv /etc/inittab.tmp /etc/inittab
fi

#
#	Some scripts will move to other packages, so we install
#	them "by hand" instead of including them directly.
#
cd /etc/init.d
for i in mdutils.sh hwclock.sh
do
	if ! [ -f $i ]
	then
		cp /usr/doc/sysvinit/examples/$i .
	fi
done

#
# Bugs 2.72 -> 2.73
# 1. mv settime.sh hwclock.sh
# 2. mv mdinit.sh mdutils.sh
# 3. Remove /etc/init.d/keymaps
#
rm -f /etc/init.d/settime.sh /etc/rcS.d/S??settime.sh
rm -f /etc/init.d/mdinit.sh /etc/rcS.d/S??mdinit.sh
rm -f /etc/init.d/keymaps /etc/rcS.d/S??keymaps

#
#	Function like update-rc.d but simpler.
#	Usage: updatercd basename start|stop NN runlevel .
#
updatercd() {

	base=$1
	shift
	while [ "$1" != . -a "$1" != "" ]
	do
		cd /etc/rc$3.d
		if [ "$1" = stop ]
		then
			tlet=K
			tmp="`echo K??$base`"
		else
			tlet=S
		fi
		tmp="`echo $tlet??$base`"
		case "$tmp" in
			"$tlet??$base")
				# Not present yet.
				ln -s ../init.d/$base $tlet$2$base
				;;
			*)
				# Already present.
				;;
		esac
		shift 3
	done
}

#
#	Remove the old S20halt and S20 reboot links.
#
rm -f /etc/rc0.d/S20halt
rm -f /etc/rc6.d/S20reboot

#
#	Okay, we could do this with update-rc.d, but that would probably
#	be pretty slow. This way we win some speed. Besides update-rc.d
#	doesn't support runlevel `S' yet!
#
updatercd checkroot.sh	start 10 S .
updatercd isapnp	start 15 S .
updatercd modutils	start 20 S .
updatercd mdutils.sh	start 25 S start 50 0 start 50 6 .
updatercd checkfs.sh	start 30 S .
updatercd mountall.sh	start 35 S .
updatercd hostname.sh	start 40 S .
updatercd network	start 40 S .
updatercd mountnfs.sh	start 45 S .
updatercd hwclock.sh	start 50 S .
updatercd bootmisc.sh	start 55 S .
updatercd urandom	start 55 S .

#
#	Links in "normal" runlevels.
#
updatercd sendsigs	start 20 6 start 20 0 .
updatercd urandom	start 30 6 start 30 0 .
updatercd umountfs	start 40 6 start 40 0 .
updatercd halt		start 90 0 .
updatercd reboot	start 90 6 .
updatercd rmnologin	start 99 2 start 99 3 start 99 4 start 99 5 .
updatercd single        start 20 1 .

