#! /bin/sh
#
# sysvinit postinst
#

set -e

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

umask 022

INITCTL=/run/initctl
case "$(uname -s)" in
  *FreeBSD)
	OLDINITCTL=/etc/.initctl
	;;
  *)
	OLDINITCTL=/dev/initctl
	;;
esac

# Create /run/initctl if not present, and also create compatibility
# symlinks
if [ "$INITCTL" ] && [ ! -p "$INITCTL" ]
then
	# Create new control channel
	echo "sysvinit: creating $INITCTL"
	rm -f $INITCTL
	mkfifo -m 600 $INITCTL

	# Replace existing control channel with symlink
	ln -s "$INITCTL" "$OLDINITCTL.new"
	mv "$OLDINITCTL.new" "$OLDINITCTL"

	# Reopen control channel (uses new channel).
	if ! ischroot
	then
		kill -s USR1 1
	fi
fi
rm -f /etc/ioctl.save

if [ ! -f /etc/inittab ]
then
	cp -p /usr/share/sysvinit/inittab /etc/inittab
fi

# Tell init to re-exec itself.  We loop on failure because to reduce
# the chance of a race before the new control channel is opened.
if ! ischroot
then
	echo -n "sysvinit: restarting..."
	for delay in 0 1 2 3 4 5 6 fail;
	do
		if init u
		then
			echo " done."
			break
		else
			if [ "$delay" = "fail" ]
			then
				echo " failed."
			else
				echo -n "."
				sleep "$delay"
			fi
		fi
	done
else
	echo "Not restarting sysvinit: chroot detected"
fi

# Remove old pipe if present.  No longer in use after re-exec.
if [ "$OLDINITCTL" ] && [ -p "$OLDINITCTL" ]
then
        rm -f "$OLDINITCTL"
fi

#DEBHELPER#

exit 0
