#!/bin/sh
#
# (C) Copyright 2002-2007 Axis Communications AB, LUND, SWEDEN
# This file is released under the GPL v2.

if [ "$1" = sysinit ]; then
	echo "Starting system initialization."

	for file in /etc/rcS.d/S??*; do
		[ -x "$file" ] && $file start
	done

	echo "System initialization is done."
else
	runlevel="$RUNLEVEL"
	[ "$runlevel" ] || runlevel="$1"
	if [ -z "$runlevel" ]; then
		echo "Usage: $0 <runlevel>" >&2
		exit 1
	fi

	prevlevel=$PREVLEVEL
	[ "$prevlevel" ] || prevlevel=N

	echo "New runlevel: $runlevel"

	if [ -d /etc/rc$runlevel.d ]; then
		if [ "$prevlevel" != N ]; then
			for file in /etc/rc$runlevel.d/K??*; do
				# Only stop what isn't already stopped.
				[ ! -e "/etc/rc$prevlevel.d/${file##*/}" ] &&
				[ -x "$file" ] &&
				$file stop
			done
		fi

		for file in /etc/rc$runlevel.d/S??*; do
			# Only start what isn't already started.
			[ ! -e "/etc/rc$prevlevel.d/${file##*/}" ] &&
			[ -x "$file" ] &&
			$file start
		done
	fi

	echo -n "All scripts for runlevel $runlevel done"
	if [ "$prevlevel" = N ]; then
		read uptime dummy 2>/dev/null </proc/uptime || :
		[ "$uptime" ] && echo -n " in ${uptime}s"
	fi

	echo .
fi

exit 0
