#! /bin/sh
#
# debian.postinst
#
# Version:	debian.postinst  2.85-20  07-Jun-2004  miquels@cistron.nl
#

set -e

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


#
#	Function like update-rc.d but simpler & faster.
#	Usage: updatercd basename start|stop NN runlevel .
#
#	Heuristic: use the real update-rc.d if file-rc is detected,
#	or if the /etc/rc2.d directory is not present.
#
updatercd() {

	if [ ! -f /etc/init.d/$1 ]
	then
		return
	fi

	if [ -d /usr/share/file-rc/. ] || [ -d /usr/lib/file-rc/. ] ||
	   [ ! -d /etc/rc2.d/. ]
	then
		update-rc.d "$@" > /dev/null
		return $?
	fi

	base=$1
	shift

	tmp="`echo /etc/rc?.d/[SK]??$base`"
	case "$tmp" in
		"/etc/rc?.d/[SK]??$base")
			;;
		*)
			return
			;;
	esac

	while [ "$1" != "" ]
	do
		if [ "$1" = stop ]
		then
			tlet=K
		else
			tlet=S
		fi
		case "$2" in
			?) lev=0$2 ;;
			*) lev=$2 ;;
		esac
		shift 2
		while [ "$1" != "." ]
		do
			cd /etc/rc$1.d
			ln -s ../init.d/$base $tlet$lev$base
			shift
		done
		shift
	done
}

if [ ! -f /etc/default/rcS ]
then
	#
	#	Install sample rcS file.
	#
	cp /usr/share/initscripts/default.rcS /etc/default/rcS
else
	#
	#	Change GMT=-u to UTC=yes etc in existing rcS file.
	#
	if grep -q ^GMT /etc/default/rcS
	then
		cp /etc/default/rcS /etc/default/rcS.TMP
		sed	-e "s/^GMT=.*\(-u\|--utc\).*/UTC=yes/" \
			-e "s/^GMT=.*/UTC=no/" \
			-e 's/# Set GMT="-u".*/# Set UTC to yes or no/' \
			< /etc/default/rcS.TMP > /etc/default/rcS
		if [ -s /etc/default/rcS ]
		then
			rm -f /etc/default/rcS.TMP
		fi
	fi
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

#
#	Defaults for 2.85-16 and up for mountvirtfs levels changed,
#	so remove the existing startup links if we upgraded from
#	an older version.
#
case "$oldver" in
	2.85-1[0-5])
		update-rc.d -f mountvirtfs remove >/dev/null 2>&1 ||:
		;;
esac

#
#	Okay, we could do this with update-rc.d, but that would probably
#	be pretty slow. This way we win some speed.
#	DO NOT FOLLOW THIS EXAMPLE IN OTHER PACKAGES.
#
updatercd mountvirtfs	start 2 S . start 36 S .
updatercd bootlogd	start 5 S .
updatercd checkroot.sh	start 10 S .
updatercd modutils	start 20 S .
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 bootmisc.sh	start 55 S .
updatercd urandom	start 55 S . start 30 0 6 .

#
#	Links in "normal" runlevels.
#
updatercd sendsigs	start 20 0 6 .
updatercd umountnfs.sh	start 31 0 6 .
updatercd umountfs	start 40 0 6 .
updatercd halt		start 90 0 .
updatercd reboot	start 90 6 .
updatercd rmnologin	start 99 2 3 4 5 .
updatercd single        start 20 1 .
updatercd stop-bootlogd	start 99 2 3 4 5 .

#
#	Remove scripts that were left behind by older glibc (<< 2.3.2.ds1-12)
#	versions. We have the same functionality in mountvirtfs.
#
for i in devpts.sh mountkernfs
do
	if [ -f /etc/init.d/$i ]
	then
		rm -f /etc/init.d/$i
		update-rc.d $i remove >/dev/null 2>&1 ||:
	fi
done

# Create /dev/pts, /dev/shm, /sys
if [ `uname -s` = Linux ]
then
	#
	#	Only create /dev/{pts,shm} if /dev is on the
	#	root file system. If some package has mounted a
	#	seperate /dev (ramfs from udev, devfs) it is
	#	responsible for the presence of those subdirs.
	#
	if ! mountpoint -q /dev
	then
		if [ ! -d /dev/pts ]
		then
			mkdir /dev/pts
		fi
		if [ ! -d /dev/shm ]
		then
			mkdir /dev/shm
		fi
	fi
	if [ ! -d /sys ]
	then
		mkdir /sys
	fi
fi

exit 0

