#!/bin/sh
########################################################################
# Begin $rc_base/init.d/rc
#
# Description : Main RunLevel Control Script - Interactive Version
#
# Authors     : Gerard Beekmans - gerard@linuxfromscratch.org
#
# Version     : 00.00
#
# Notes       :
#
########################################################################

. /etc/sysconfig/rc
. ${rc_functions}

# This sets a few default terminal options.
stty sane

# These 3 signals will not cause our script to exit
trap "" INT QUIT TSTP

[ "${1}" != "" ] && runlevel=${1}

if [ "${runlevel}" = "" ]; then
	echo "Usage: ${0} <runlevel>" >&2
	exit 1
fi

previous=${PREVLEVEL}
[ "${previous}" = "" ] && previous=N

# Begin addition for interactive starup.
if [ "${runlevel}" != "sysinit" -a -f /tmp/.interactive-start ]; then
	. /tmp/.interactive-start
fi

if [ "${INTERACTIVE}" == "I" -a "${runlevel}" != "sysinit" -a \
	"${runlevel}" != "0" -a "${runlevel}" != "6" ]; then
	echo -n -e "Proceed with interactive starup of runlevel "
	echo -n -e "${INFO}${runlevel}${NORMAL}?" 
	echo -n -e "(${FAILURE}y${NORMAL})es/(${FAILURE}n${NORMAL})o "
	read -n 1 go_on
	echo ""
	if [ "${go_on}" == "n" ]; then
		# don't continue	
		exit 0
	fi
fi

if [ "${runlevel}" == "sysinit" -a "${IPROMPT}" == "yes" ]; then
	ls -l /bin/sh | grep bash 2>&1 > /dev/null
	if [ "${?}" == "0" ]; then
		# Okay..we are using bash for the shell
		# so -t and -n are okay for read
		echo ""
		# PTAB is the amount of spaces needed to format the message
		# on the screen correctly.
		echo -e "\\033[${PTAB}G""Welcome to ${INFO}${DISTRO}"
		# The "Press 'I'" message is broken in two for readability here
		echo -n -e "\\033[18G""${NORMAL}Press '${FAILURE}I${NORMAL}'"
		echo -e " to enter interactive startup"
		echo ""
		read -t ${ITIME} -n 1 INTERACTIVE 2> /dev/null
		export INTERACTIVE
		if [ "${INTERACTIVE}" == "I" ]; then
			echo -n -e "${CURS_UP}"
		        echo -e "${INFO}Interactive mode selected...${NORMAL}"
		        echo "INTERACTIVE=${INTERACTIVE}"
		        echo ""
		fi
	fi
fi		

# End addition for interactive startup

if [ ! -d ${rc_base}/rc${runlevel}.d ]; then
	boot_mesg "${rc_base}/rc${runlevel}.d does not exist." ${WARNING}
	boot_mesg_flush
	exit 1
fi

# Attempt to stop all service started by previous runlevel,
# and killed in this runlevel
if [ "${previous}" != "N" ]; then
	for i in $(ls -v ${rc_base}/rc${runlevel}.d/K* 2> /dev/null)
	do
		check_script_status

		suffix=${i#$rc_base/rc$runlevel.d/K[0-9][0-9]}
		prev_start=$rc_base/rc$previous.d/S[0-9][0-9]$suffix
		sysinit_start=$rc_base/rcsysinit.d/S[0-9][0-9]$suffix

		if [ "${runlevel}" != "0" ] && [ "${runlevel}" != "6" ]; then
			if [ ! -f ${prev_start} ] && [ ! -f ${sysinit_start} ]; then
				boot_log "${i} cant be started in runlevel ${runlevel}, because it was not started in runlevel ${previous}"
				boot_mesg -n "WARNING:\n\n${i} can't be" ${WARNING}
				boot_mesg -n " executed because it was not"
				boot_mesg -n " not started in the previous"
				boot_mesg -n " runlevel (${previous})."
				boot_mesg "" ${NORMAL}
				boot_mesg_flush
				continue
			fi
		fi
		${i} stop
		error_value=${?}

		if [ "${error_value}" != "0" ]; then
			print_error_msg
		fi
	done
fi

#Start all functions in this runlevel
for i in $( ls -v ${rc_base}/rc${runlevel}.d/S* 2> /dev/null)
do
	if [ "${previous}" != "N" ]; then
		suffix=${i#$rc_base/rc$runlevel.d/S[0-9][0-9]}
		stop=$rc_base/rc$runlevel.d/K[0-9][0-9]$suffix
		prev_start=$rc_base/rc$previous.d/S[0-9][0-9]$suffix

		[ -f ${prev_start} ] && [ ! -f ${stop} ] && continue
	fi

	check_script_status

	case ${runlevel} in
		0|6)
			${i} stop
			;;
		*)
			# Begin setup for interactive starup
			if [ "${INTERACTIVE}" == "I" ]; then
				echo -n -e "Start ${INFO}${i}${NORMAL} ? "
				echo -n -e "(${FAILURE}y${NORMAL})es/"
				echo -n -e "(${FAILURE}n${NORMAL})o "

				# Set startit variable to an unusable value
				startit="x"
				while [ "${startit}" != "y" -a \
					"${startit}" != "n" ]; do
					read -n 1 startit > /dev/null
				done

				if [ "${startit}" == "y" ]; then
					echo ""
					${i} start
					echo ""
				else
					echo ""
					echo -e "Not starting ${INFO}${i}${NORMAL}!"
					echo ""
				fi
			unset startit
			unset testsok
			else
			# Non-Interactive Startup
			${i} start
			fi
			;;
	esac
	error_value=${?}

	if [ "${error_value}" != "0" ]; then
		print_error_msg
	fi
done

# Begin addition #3 for interactive starup
if [ "${INTERACTIVE}" == "I" ]; then
	# Insert check for writeable /tmp here
	echo "INTERACTIVE='I'" > /tmp/.interactive-start 2> /dev/null
	if [ "${?}" != "0" ]; then
		# There is nothing we can do to make the next runlevel
		# check for interactivity because /tmp is not writeable.
		echo ""
		echo -e "${WARNING}/tmp is not writable!!!"
		echo -n -e "${WARNING}Interactive startup will not work"
		echo -e "${WARNING} for the next runlevel!!!"
		echo -e "${NORMAL}"
		sleep 5
	fi
else 
	# Clear any existing file
	rm -f /tmp/.interactive-start 2>&1 > /dev/null
fi
# End addition #3 for interactive startup

# End $rc_base/init.d/rc
