#!/bin/sh
#	$Id: rc,v 1.160 1998/10/09 17:11:14 des Exp $
#	From: @(#)rc	5.27 (Berkeley) 6/5/91

# System startup script run by init on autoboot
# or after single-user.
# Output and error are redirected to console by init,
# and the console is the controlling terminal.

# Note that almost all the user-configurable behavior is no longer in
# this file, but rather in /etc/rc.conf.  Please check this file
# first before contemplating any changes here.

stty status '^T'

# Set shell to ignore SIGINT (2), but not children;
# shell catches SIGQUIT (3) and returns to single user after fsck.
trap : 2
trap : 3	# shouldn't be needed

HOME=/; export HOME
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin
export PATH

# Configure ccd devices.
if [ -f /etc/ccd.conf ]; then
	ccdconfig -C
fi

swapon -a

if [ $1x = autobootx ]; then
	echo Automatic reboot in progress...
	fsck -p
	case $? in
	0)
		;;
	2)
		exit 1
		;;
	4)
		reboot
		echo "reboot failed... help!"
		exit 1
		;;
	8)
		echo "Automatic file system check failed... help!"
		exit 1
		;;
	12)
		echo "Reboot interrupted"
		exit 1
		;;
	130)
		# interrupt before catcher installed
		exit 1
		;;
	*)
		echo "Unknown error in reboot"
		exit 1
		;;
	esac
else
	echo Skipping disk checks ...
fi

trap "echo 'Reboot interrupted'; exit 1" 3

# root must be read/write both for NFS diskless and for VFS LKMs before
# proceeding any further.
mount -u -o rw /
if [ $? != 0 ]; then
	echo "Filesystem mount failed, startup aborted"
	exit 1
fi

umount -a >/dev/null 2>&1

mount -a -t nonfs
if [ $? != 0 ]; then
	echo "Filesystem mount failed, startup aborted"
	exit 1
fi

# If there is a global system configuration file, suck it in.
if [ -f /etc/rc.conf ]; then
	. /etc/rc.conf
fi

# Start all available services, in topologically sorted order
for _rcscript in $(
(
    for _dir in ${startup_dirs:=${local_startup} /etc/rc.d}
	_rcdfiles="${_rcdfiles} $(ls $_dir | sed "s|$_dir||" | grep '^[0-9]*[A-Za-z][A-Za-z]*[0-9]*')
    }
    for _file in $_rcdfiles
    do
        _didit=0
        for _dir in ${startup_dirs}
        do
	    if [ "$_didit" -eq "0" -e $_dir/$_rcscript
	    then
		_didit=1
		echo $_file $_file
		sed -n -e "s/^#RC:AFTER  *\([0-9]*[A-Za-z][A-Za-z]*[0-9]*\)/\1 $_file/p" \
		       -e "s/^#RC:BEFORE  *\([0-9]*[A-Za-z][A-Za-z]*[0-9]*\)/$_file \1/p" \
		    $_dir/$_file
	    fi
       done
    done) | tsort)
do
    _didit=0
    for _dir in ${startup_dirs}
    do
	if [ "$_didit" -eq "0" -e $_dir/$_rcscript
	then
	    _didit=1
	    if grep -q '^#RC:RUNINRC' $_file; then
		. /etc/rc.d/$_rcscript
	    else
		/etc/rc.d/$_rcscript start
	    fi
	fi
    done
done

date
exit 0
