# /etc/rc - System startup script run by init before going multiuser.
#set -x

umask 022
TERM="${TERM-vs100}"
PATH=/usr/local/sbin:/sbin:/usr/sbin:/usr/local/bin:/bin:/usr/bin
export TERM PATH

usage()
{
    echo >&2 "Usage: $0 [-safn] start|stop|down"
    exec intr sh
}

while getopts 'safn' opt
do
    case $opt in
    s)	sflag=t ;;	# Single user
    a)	aflag=t ;;	# Ask for /usr
    f)	fflag=t ;;	# Force a full file system check
    n)	nflag=t ;;	# Don't check
    *)	usage
    esac
done
shift `expr $OPTIND - 1`

case "$#:$1" in
1:start|1:stop|1:down)
    action=$1
    ;;
*)  usage
esac

case $action in
start)
    # Load national keyboard map.
    test -f /etc/keymap && keymap -l /etc/keymap

    # Initialize virtual disk devices.
    test -f /etc/vdisk.conf && vdisk_ctrl -c /etc/vdisk.conf

    # Tell FS the name of the device that contains the root filesystem
    mount -R

    do_mkvar=''
    if [ -f /.cdrom ]
    then
	do_mkvar=1	
	sh /etc/rc.mkvar
    fi

    if [ ! "$nflag" ]
    then
    	echo;echo "Checking File Systems."
	if [ "$fflag" ]
	then
	    intr fsck -pf
	else
	    intr fsck -p
	fi

	case $? in
	0)  # All clean or fixed.
	    ;;
	2)  # Quit typed.
	    sflag=t why="Quit typed"
	    ;;
	4)  # Minor errors have been corrected, but on a mounted FS.
	    echo "Mounted device fixed, rebooting"
	    reboot -f
	    ;;
	*)  # Serious errors, need manual intervention.
	    echo "Please run fsck manually, reboot if / is repaired."
	    intr sh
	esac
    fi

    # Set timezone and time.
    . /etc/timeinfo
    readclock || sflag=t why="Unable to set system time"

    test "$aflag" || mount -av || sflag=s why="Mount failed"

    if [ ! "$do_mkvar" ]
    then
	svrctl set-pipe-fs /var
    fi

    test -d /usr/bin || aflag=t
    if [ "$aflag" ]
    then
	intr sh -c '
	    echo -n "Finish the name of device to mount as /usr: /dev/"
	    read usr
	    mount /dev/$usr /usr
	' || sflag=s why="Mount failed"
    fi

    if [ "$sflag" ]
    then
	echo "${why+"$why - "}Single user."
	intr sh
    fi

    mount -p |
    {
	read rootdev rootdir rest 
	case $rootdev:$rootdir in
	/dev/ram:/)
		# Remove boot-only things to make space.
		rm -fr /boot /minix
	esac
    }

    # Things should be alright now.
    echo;echo "Multiuser startup in progress."

esac

# Further initialization.
test -f /usr/etc/rc && sh /usr/etc/rc $action
if [ -f /usr/lib/packages ]
then
	IFS=/
	while read package rest <&9
	do
		rc="/opt/$package/etc/rc"
		test -f "$rc" && sh "$rc" $action 9<&-
	done 9</usr/lib/packages
fi
test -f /usr/local/etc/rc && sh /usr/local/etc/rc $action
test -f /var/etc/rc && sh /var/etc/rc $action

# Any messages?
test "$action" = start -a -f /etc/issue && cat /etc/issue
exit 0

#
# $PchId: rc,v 1.8 2015/12/02 14:28:04 philip Exp $
#
