# 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.95 2001/09/19 16:42:21 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

if [ -f /etc/rc.first ] 
then
	. /etc/rc.first
fi

if [ $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,nonetc

# The following test does not work if /var or /usr are mounted via
# NFS; NFS file systems are mounted later.
for dir in /var/db /usr/bin; do
	if [ ! -d $dir ]; then
		echo "Required filesystems were not mounted; help!"
		exit 1
	fi
done

# 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; })

if [ -d /etc/rc.configure ]
then
	cd /etc/rc.configure
	trap "echo 'configuration interrupted.'; exit 1" 2
	stty erase ^H
	TERM=$(	(echo 'console() {
		term="$2"; for i do
			case $i in
			term=*) term=$(expr "$i" : "term=\(.*\)")
			esac
		done; echo "$term"; exit 0
	    }'; grep '^console[ 	]' /etc/ttys) | sh)
	export TERM

	for i in *
	do
		case $i in
		[0-9]*.*.*[0-9])
			if ! ./$i ; then
				exit 1
			fi
			;;
		*)
			;;
		esac
	done
	trap : 2
	cd /
fi

if [ -r /etc/license ]
then
	sysctl -w kern.license="$(cat /etc/license)" > /dev/null
else
	echo "Warning: no license for this machine"
fi

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-4.3 ]; then	# create a file as a timestamp for install
	touch /.base-4.3
fi

gettyd -s			# start gettyd in single user mode

# 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'
syslogd ${syslogdflags-"-l"}

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

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

echo -n 'starting network daemons:'

if [ -f /etc/snmp/snmpd.conf -a -x /usr/contrib/bin/snmpd ]; then
	echo -n ' snmpd';	snmpd
fi

if [ X"${ilmid}" = XYES ]; then
	echo -n ' ilmid';	ilmid
fi

if [ -f /etc/scspd.conf -a X"${atmarpd}" != XNO ]; then
	echo -n ' scspd';	scspd
	echo -n ' atmarpd';	atmarpd ${atmarpd}
fi

echo -n ' portmap';		portmap

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

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

if [ X${nis_domain} != X ]; then
	echo -n " ypbind";	ypbind -s
fi

if [ -f /etc/dhcpd.conf -a "X${dhcpdflags}" != X"NO" ]; then
	[ "X${dhcpdflags}" = "XYES" ] && dhcpdflags=""
	echo -n ' dhcpd';		dhcpd $dhcpdflags
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 -n 6
fi

# start status monitor and locking daemon if they exist
if [ -f /usr/libexec/statd ]; then
	echo -n ' statd';		/usr/libexec/statd
fi

if [ -f /usr/libexec/lockd -a X${nfs} != X"NO" ]; then
	echo -n ' lockd';		/usr/libexec/lockd
fi

echo '.'

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

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

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

# build ps databases
kvm_mkdb /bsd
dev_mkdb

# build lsof database
rm -f /var/run/lsof.dc
if [ -x /usr/contrib/bin/lsof ]; then
	lsof -Du/var/run/lsof.dc -p99999 > /dev/null 2>&1
	if [ $? -eq 0 ]; then
		echo "problems creating /var/run/lsof.dc"
		rm -f /var/run/lsof.dc
	else
		chmod 0644 /var/run/lsof.dc
	fi
fi

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/share/vi/recover

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 \
	! -name quota.user ! -name quota.group -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 ' cron';		cron
if [ -f /usr/libexec/randomd -a "X${randomdflags}" != X"NO" ]; then
	[ "X${randomdflags}" = "XYES" ] && randomdflags=""
	echo -n ' randomd';	/usr/libexec/randomd $randomdflags
	if [ ! -e /dev/random ] ; then
		ln -sf /var/run/random.stream /dev/random
	fi
	if [ ! -e /dev/urandom ] ; then
		ln -sf /var/run/random.md5 /dev/urandom
	fi
fi
echo '.'

if [ -f /etc/ntp.conf ]; then
	NTPSERVERS=$(awk '$1 == "peer" { peers = peers " " $2 }
		$1 == "server" && $2 !~ /^127.127/ { servers = servers " " $2 }
		$1 == "broadcastclient" && (NF == 2) { print $2 }
		$1 == "manycastclient" && (NF > 1) { print $2 }
		$1 == "multicastclient" { print (NF > 1) ? 
		    gensub("^[:blank:]*multicastclient[:blank:]*", "", "g") :
		    "224.0.1.1" }
		END { servers = servers " " peers; 
		    print gensub("^ *", "", "g", servers) }' /etc/ntp.conf)
	if [ -n "${NTPSERVERS}" ]; then
		echo -n 'using ntpdate to set the clock'
		ntpdate -bs ${NTPSERVERS}
		echo '.'
	fi
fi

if [ "X${sshdflags}" != "XNO" -a -x /usr/contrib/bin/ssh-keygen ]; then
	# Generate keys if they don't exist...
	if [ ! -f /etc/ssh_host_key ] ; then
		echo -n 'generating ssh host key (may take a while) ...'
		/usr/contrib/bin/ssh-keygen -q -t rsa1 -N '' \
		    -f /etc/ssh_host_key 
		echo ' done!'
	fi
	if [ ! -f /etc/ssh_host_dsa_key ]  ; then
		echo -n 'generating ssh DSA host key (may take a while) ...'
		/usr/contrib/bin/ssh-keygen -q -t dsa -N '' \
		    -f /etc/ssh_host_dsa_key 
		echo ' done!'
	fi
	if [ ! -f /etc/ssh_host_rsa_key ]  ; then
		echo -n 'generating ssh RSA host key (may take a while) ...'
		/usr/contrib/bin/ssh-keygen -q -t rsa -N '' \
		    -f /etc/ssh_host_rsa_key 
		echo ' done!'
	fi
fi

echo -n starting additional network daemons:

if [ -f /etc/ntp.conf ]; then
	echo -n ' ntpd';		( limit openfiles 580; ntpd )
else
	# $timedflags is imported from /etc/netstart;
	# if $timedflags == NO, timed isn't run.
	if [ "X${timedflags}" != X"NO" ]; then
		[ "X${timedflags}" = "XYES" ] && timedflags=""
		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 [ -f /usr/sbin/rwhod -a "${rwhod-NO}" != "NO" ]; then
	[ "X${rwhod}" = "XYES" ] && rwhod=""
	echo -n " rwhod";		rwhod $rwhod
fi

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

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

if [ -f /etc/mail/sendmail.cf -a -x /usr/sbin/sendmail ]; then
	(cd /var/spool/mqueue; rm -f [lnx]f*)
	# Build /etc/mail/aliases.db if there isn't one (req. in newer versions)
	if [ ! -f /etc/mail/aliases.db ]; then
		/usr/bin/newaliases >/dev/null 2>&1
	fi
	echo -n ' sendmail';		/usr/sbin/sendmail -bd -q30m
fi

if [ -f /etc/amd.conf ]; then
	echo -n ' amd';			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 [ "X${maximflags}" != "XNO" -a -x /usr/sbin/maxim ]; then
	echo -n ' maxim';	maxim -s
fi

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

if [ -f /var/www/squid/conf/squid.conf ]; then
	echo -n ' squid';	/var/www/squid/bin/start-squid
fi

# Main fax spooler processes -- 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';	faxq
	echo -n ' hfaxd'
	/usr/contrib/lib/hylafax/hfaxd -i hylafax -o oldfax -s snpp
fi

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

# "-u internal" is the same as the default action if
# there is no -u option.
if [ -f /etc/inetd.conf ]; then
	echo -n ' inetd';	inetd -u ${inetd_ignore:-internal}
fi

if [ -f /etc/raddb/users -a -f /etc/raddb/clients ]; then
	echo -n ' radiusd';	radiusd
fi

# Start rarpd if it might answer queries (requires both the configuration
# file in /etc/ethers, and boot files in /tftpboot).
if [ -f /etc/ethers -a -d /tftpboot ]; then
	echo -n ' rarpd';	rarpd -a
fi

if [ "X${apmdflags}" != "XNO" -a -x /usr/libexec/apmd ]; then
	echo -n ' apmd';	/usr/libexec/apmd ${apmdflags}
fi

if [ "X${sshdflags}" != "XNO" -a -x /usr/contrib/bin/sshd ]; then
	echo -n ' sshd';	/usr/contrib/bin/sshd ${sshdflags}
fi

echo '.'

# Transition to symmetric I/O mode and start additional CPUs
if [ -f /etc/mp.config -a ! -f /etc/mp.nostart ]; then
	echo 'Starting additional CPUs';	cpu mp
fi

sh /etc/rc.local

date
exit 0
