# Copyright (c) 1995 Berkeley Software Design, Inc. All rights reserved.
# The Berkeley Software Design Inc. software License Agreement specifies
# the terms and conditions for redistribution.
#
#	BSDI rc,v 2.12 1996/01/03 20:33:49 polk Exp

# 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.

stty status '^T'

# Set shell to ignore SIGINT (signal 2), but not children; shell catches
# SIGQUIT (signal 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/contrib/bin
export PATH

if [ -r /fastboot ]
then
	echo "Fast boot: skipping disk checks."
elif [ $1x = autobootx ]
then
	echo "Automatic reboot in progress: starting file system checks."
	fsck -p
	case $? in
	0)
		;;
	2)
		exit 1
		;;
	4)
		echo "Rebooting..."
		reboot
		echo "Reboot failed; help!"
		exit 1
		;;
	8)
		echo "Automatic file system check failed; help!"
		exit 1
		;;
	12)
		echo "Boot interrupted."
		exit 1
		;;
	130)
		# interrupt before catcher installed
		exit 1
		;;
	*)
		echo "Unknown error; help!"
		exit 1
		;;
	esac
fi

trap "echo 'Boot interrupted.'; exit 1" 3

swapon -a
asyncd 2

umount -a >/dev/null 2>&1
mount -a -t nonfs
rm -f /fastboot		# XXX (root now writeable)

if [ -d /etc/rc.hardware ] 
then
	cd /etc/rc.hardware
	echo -n system specific hardware configuration:
	for i in *
	do
		case $i in
		[0-9]*.*.*[0-9])
			sh $i
			;;
		*)
			;;
		esac
	done
	echo .
	cd /
fi

if [ ! -f /.base-2.1 ]; then	# create a file as a timestamp for install
	touch /.base-2.1
fi

if [ -f /CONFIGME ]	# first-time configuration?
then
	rm -f /CONFIGME	# only do it once, even if we fail
	stty erase 
	TERM=ibmpc3
	export TERM
	# go single user on interrupt
	trap "echo 'configuration interrupted.'; exit 1" 2
	/etc/adm/lib/configure_all
	trap : 2	# back to ignoring SIGINT
	echo
	echo "Configuration Finished -- continuing with system startup..."
	echo 
fi

# clean up left-over files
rm -f /etc/nologin
rm -f /var/spool/uucp/LCK.*
rm -f /var/spool/uucp/STST/*
rm -f /var/spool/at/atrun.lock
(cd /var/run && { rm -rf -- *; install -c -m 664 -g utmp /dev/null utmp; })

# set hostname, turn on network
echo 'starting network'
. /etc/netstart

# check if NFS is in the kernel
if [ `nm /bsd | grep -c nfs` = 0 ]; then
        nfs=NO
else
        nfs=YES
fi

echo 'starting system logger'
rm -f /dev/log
syslogd

# If /etc/gated.conf exists, gated is used; otherwise routed.
if [ -f /etc/gated.conf ]; then
	/usr/contrib/bin/gdc start
fi

echo -n 'starting network daemons:'

echo -n ' portmap';		portmap

# If $routedflags == NO, routed isn't run.
if [ ! -f /etc/gated.conf -a X${routedflags} != X"NO" ]; then
	echo -n ' routed';	routed $routedflags
fi

if [ -f /etc/named.boot ]; then
	echo -n ' named';	named
fi

# Only start mountd and nfs daemons if /etc/exports exists
if [ -f /etc/exports -a X${nfs} != X"NO" ]; then
    echo -n ' mountd';		mountd
    echo -n ' nfsd';		nfsd -u -t 6
fi

# Start ijsrvd (WinSock server for IPX clients) if configuration
# file exists.
if [ -f /etc/ij.cfg ]; then
	echo -n ' ijsrvd';		daemon ijsrvd | daemon logger -t ijsrvd
fi

echo '.'

if [ -f /sbin/mount_nfs -a X${nfs} != X"NO" ]; then
	mount -a -t nfs
fi

# /var/crash should be a directory or a symbolic link
# to the crash directory if core dumps are to be saved.
if [ -d /var/crash ]; then
	echo checking for core dump...
	savecore /var/crash
fi

				echo -n 'checking quotas:'
quotacheck -a
				echo ' done.'
quotaon -a

# build ps databases
kvm_mkdb /bsd
dev_mkdb

chmod 666 /dev/tty[pqrs]*
chown root /dev/console
chmod 600 /dev/console

# check the password temp/lock file
if [ -f /etc/ptmp ]
then
	logger -s -p auth.err \
	'password file may be incorrect -- /etc/ptmp exists'
fi

# recover nvi editor files
/usr/sbin/virecover

echo clearing /tmp

# prune quickly with one rm, then use find to clean up /tmp/[lq]*
# (not needed with mfs /tmp, but doesn't hurt there...)
(cd /tmp && rm -rf [a-km-pr-zA-Z]* &&
    find -d . ! -name . ! -name lost+found ! -name quotas -exec rm -rf -- {} \;)

if [ -f /var/account/acct ]; then
    echo 'turning on accounting';	accton /var/account/acct
fi

echo 'checking system configuration'
chkconfig

echo -n standard daemons:
echo -n ' update';		update
echo -n ' cron';		cron
echo '.'

echo -n starting additional network daemons:

if [ -f /etc/ntp.conf ]; then
    echo -n ' xntpd';		xntpd
else
    # $timedflags is imported from /etc/netstart;
    # if $timedflags == NO, timed isn't run.
    if [ X${timedflags} != X"NO" ]; then
	    echo -n ' timed'; timed $timedflags
    fi
fi

if [ -f /sbin/nfsiod -a X${nfs} != X"NO" ]; then
        echo -n ' nfsiod';              nfsiod 4
fi

# $rwhod is imported from /etc/netstart;
# if $rwhod is set to something other than NO, rwhod is run.
if [ "${rwhod-NO}" != "NO" ]; then
	[ "X${rwhod}" = "XYES" ] && rwhod=""
	echo -n " rwhod $rwhod";	rwhod $rwhod
fi

# $rstatd is imported from /etc/netstart;
# if $rstatd is not to something other than NO, rstatd is run.
if [ ${rstatd-NO} != "NO" ]; then
	echo -n ' rstatd';	rstatd &
fi

if [ -f /etc/printcap ]; then
	echo -n ' printer';		lpd
fi

echo -n ' sendmail';		sendmail -bd -q30m

if [ -f /etc/start-amd ]; then
    echo -n ' amd';             /etc/start-amd
fi
    
# Kerberos runs ONLY on the Kerberos server machine
if [ -f /etc/kerberosIV/principal.db ]; then
    echo -n ' kerberos'
    kerberos >> /var/log/kerberos.log &
fi

if [ -f /var/www/docs/index.html ]; then
	echo -n ' httpd';	/usr/contrib/bin/httpd
fi

# Main fax spooler process -- modem processes are run from /etc/ttys by
# init or out-only faxq processes are run from rc.local
if [ -f /var/spool/fax/etc/config ]; then
	echo -n ' faxq';	/usr/contrib/bin/faxq
fi

# Samba (SMB/Lanmanager/NT filesystem and print server)
if [ -f /etc/smb.conf ]; then
	echo -n ' nmbd';	/usr/contrib/bin/nmbd -D
	echo -n ' smbd';	/usr/contrib/bin/smbd -D
fi

if [ -f /etc/inetd.conf ]; then
	echo -n ' inetd';		inetd
fi

echo '.'

sh /etc/rc.local

date
exit 0
