# rc - runlevel compatibility
#
# This task guesses what the "default runlevel" should be and starts the
# appropriate script.
#
# Edited to support booting to non-default runlevel by adding a
# single digit from [2345] as last option on kernel command line
# at boot. See elif statement below. lsw 10/4/2008
#

start on stopped rcS

script
	runlevel --reboot || true

	if grep -q -w -- "-s\|single\|S" /proc/cmdline; then
	    telinit S
	elif RL="$(grep -o "[[:blank:]][2345]$" /proc/cmdline || true)"; then
	    if [ -n "$RL" ]; then
		telinit $RL
	    else
		telinit 2
	    fi
	elif [ -r /etc/inittab ]; then
	    RL="$(sed -n -e "/^id:[0-9]*:initdefault:/{s/^id://;s/:.*//;p}" /etc/inittab || true)"
	    if [ -n "$RL" ]; then
		telinit $RL
	    else
		telinit 2
	    fi
	else
	    telinit 2
	fi
end script

