# /usr/etc/rc - continued system initialization.

case "$#:$1" in
1:start|1:stop|1:down)
    action=$1
    ;;
*)  echo >&2 "Usage: $0 start|stop|down"
    exit 1
esac

daemonize()
{
    # Function to start a daemon, if it exists.
    local IFS=':'

    for dir in $PATH
    do
	if [ -f "$dir/$1" ]
	then
	    echo -n " $1"
	    "$@" &
	    sleep 0.01
	    return
	fi
    done
}

case $action in
start)
    # Select console font.
    test -f /etc/font && loadfont /etc/font </dev/console

    # Cleanup.
    rm -rf /tmp/. /usr/run/. /usr/spool/locks/.

    # Empty utmp
    :>/usr/run/utmp

    # Reseed /dev/random from data saved at shutdown.
    if [ -f /usr/adm/random.dat ]
    then
	dd if=/usr/adm/random.dat of=/dev/random bs=16 count=1 conv=silent
    fi

    # Replace the old random data, just in case we can't shutdown properly.
    # (We don't want to see that old data ever again.)  Note that dd may block.
    if [ -d /usr/adm ]
    then
	umask 077
	dd if=/dev/random of=/usr/adm/random.dat count=1 seek=0 conv=silent &
	umask 022
    fi

    # Start servers.
    ifs="$IFS"; IFS=,
    for server in `sysenv servers`; do $server; done
    IFS="$ifs"; unset ifs
    sleep 1

    # Network initialization.
    (: </dev/tcp) 2>/dev/null && net=t	# Is there a TCP/IP server?

    echo -n "Starting daemons:"
    daemonize syslogd
    daemonize update
    daemonize cron

    if [ "$net" ]
    then
	# We need a name daemon, maybe our own, maybe a real one.
	named=nonamed
	test -f /etc/named.boot && named=named

	if [ -f /etc/rc.net ]
	then
	    # Let a customized TCP/IP initialization script figure it out.
	    . /etc/rc.net
	else
	    # Standard network daemons.
	    daemonize dhcpd
	    daemonize $named
	    test -s /etc/inetd.conf && daemonize inetd
	    test -s /etc/tsd.conf && daemonize tsd $(cat /etc/tsd.conf)
	    test -s /etc/rip.conf && daemonize rip
	    test -f /etc/printcap && daemonize lpd
	fi
    fi

    # The last daemon has been started, so close the list:
    echo .

    if [ "$net" ]
    then
	# Get the nodename from the DNS and set it.
	t='-t 10'
	trap '' 2
	while :;
	do
	    intr $t hostaddr -h

	    case $? in
	    142)
		echo "\
Unable to obtain an IP address after 10 seconds.  Hit DEL to get a root
prompt to investigate, otherwise just wait until an address is received..."
		t=
		;;
	    130)
		echo "Single user."
		intr sh
		echo "Continue waiting for an address..."
		;;
	    *)	break
	    esac
	done
	trap 2
    fi

    # Recover files being edited when the system crashed.
    test -f /usr/sbin/elvprsv && elvprsv /usr/tmp/elv_*.*

    # Run the daily cleanup on systems that are not on at night.
    test -f /usr/etc/daily && sh /usr/etc/daily boot &
    ;;

down)
    # Save some random bits from /dev/random, so it can be reseeded quickly
    # on reboot.  (/dev/random needs quite some time to gather enough
    # randomness on its own just after a reboot.)
    if [ -d /usr/adm ]
    then
	umask 077
	dd if=/dev/random of=/usr/adm/random.dat count=1 seek=0 conv=silent &
	umask 022
    fi

    # Write the disciplined time back to the hardware clock.
    #XXX#test -s /etc/tsd.conf && calcmos -O 60 -s 60
esac

#
# $PchId: rc,v 1.8 2003/12/18 14:49:21 philip Exp philip $
