#!/sbin/sh
# @(#) $Revision: 78.2 $       

arg=$1
arg2=$2

PATH=/sbin
export PATH

#
# Set termio configuration for output device.
#
/sbin/stty clocal icanon echo opost onlcr ixon icrnl ignpar 2> /dev/null

umask 022

get_scripts() {
        state=$1
        mode=$2
        dir=/sbin/rc${state}.d
        ls $dir 2>/dev/null |
        while read name
        do
                case $name in
                ${mode}*)
                        path=$dir/$name
                        if [ "$mode" = "S" ]; then
                                desc=`$path start_msg`
                        elif [ "$mode" = "K" ]; then
                                desc=`$path stop_msg`
                        fi
                        echo $path $desc
                esac
        done
}

if [ -f /sbin/rc.utils ]; then
	. /sbin/rc.utils
else
	init_list() {
		echo $1
	}
	add_list() {
		eval $1
	}
	run_list() {
		:
	}
fi

#
# If /etc/rc.config contains default information (first boot),
# /sbin/auto_parms will invoke /sbin/set_parms to remedy the situation.
#
# For 10.0 release, the default HOSTNAME is unset or an empty string.
# Assume a timezone if /etc/TIMEZONE does not exist.
#

TZ=EST5EDT
if [ -f /etc/rc.config ]; then
	. /etc/rc.config

	if [ -x /sbin/auto_parms ]; then
	    	/sbin/auto_parms
	else
		echo "\nWARNING: /sbin/auto_parms does not exist"
		echo "DHCP invocation skipped."
	fi
else
	echo "\nWARNING: /etc/rc.config does not exist"
	echo "System name not set, default $TZ assumed."
fi
export TZ

#
# Set runlevel information
#
set `who -r` x
new=$7	# new run level
old=$9	# previous run level

#
# Check to see if we are run from /etc/inittab, or from the command line.
# If run from the command line, set the old run-level to the new run-level.
if [ $PPID != 1 ]; then
	old=$new

# If the new run-level was specified on the command line, go to that state
# instead.

	if [[ -n $arg2 ]]; then
		new=$arg2
	fi
fi

if [ "$new" = S ]; then
	new=0
	tosingle=1
else
	tosingle=0
fi

BOOT=0
if [ "$old" = S ]; then
	old=0
	BOOT=1
fi

#
# Process scripts
#
found=0
if [ "$new" -gt "$old" ]; then
	# new run level is higher than old, so run start scripts in
	# all intermediate run levels and in new run level.
	if [ $BOOT = 1 ]; then
		init_list "HP-UX Start-up in progress"
	else
		init_list "Transition to run-level $new in progress"
	fi
	typeset -i lvl=$old
	lvl=lvl+1
	while [ $lvl -le "$new" ]; do
		get_scripts $lvl S |
		while read name descrip; do
		    if [ -s "$name" ]; then
			add_list "$name start" "$descrip"
			found=1
		    fi
		done
		lvl=lvl+1
	done
elif [ "$new" -lt "$old" ]; then
	# new run level is lower than old level, so run kill scripts
	# in all intermediate levels and in new level.
	if [ "$new" = 0 ]; then
		init_list "System shutdown in progress"
	else
		init_list "Transition to run-level $new in progress"
	fi
	typeset -i lvl=$old
	lvl=lvl-1
	while [ $lvl -ge "$new" ]; do
		get_scripts $lvl K |
		while read name descrip; do
		    if [ -s "$name" ]; then
			add_list "$name stop" "$descrip"
			found=1
		    fi
		done
		lvl=lvl-1
	done
	# If we're ending up in state 0 or S, run the start scripts for
	# that state.
	if [ "$new" = 0 ]; then
		get_scripts 0 S |
		while read name descrip; do
		    if [ -s "$name" ]; then
			add_list "$name start" "$descrip"
			found=1
		    fi
		done
	fi
else
	# old and new run levels are the same.  Assume that execution
	# is from the command line and run start scripts for the current
	# run level
	init_list "Starting subsystems for run-level $new"
	get_scripts ${new} S |
	while read name descrip; do
	    if [ -s "$name" ]; then
		add_list "$name start" "$descrip"
		found=1
	    fi
	done
fi
if [ $found = 1 ]; then
	if [ "$BOOT" = 1 ]; then
		run_list boot
	else
		run_list
	fi
fi

if [ "$new" = 0 ]; then
	case $arg in
		"shutdown")
			exec /sbin/sh
				;;
		"reboot")
			/sbin/reboot
				;;
		"off")
			/sbin/reboot -h
				;;
	esac

#
# If transitioned to real state 0 (that is, not state S) via init, halt.
#

	if [[ $PPID -eq 1 && "$tosingle" -ne 1 ]]; then
		/sbin/reboot -h
	fi
fi

#
# Output message to indicate completion
#
echo
if [ $BOOT = 1 ]; then
	echo "The system is ready."
else
	echo "Transition to run-level $new is complete."
fi
