#!/bin/sh
#     Boot startup script
#
#     $Header: /afs/rel-eng.athena.mit.edu/project/release/current/source/athena/config/files/bsd-4.3/RCS/rc,v 1.106 91/09/17 10:22:55 lwvanels Exp $
#     $Source: /afs/rel-eng.athena.mit.edu/project/release/current/source/athena/config/files/bsd-4.3/RCS/rc,v $
#     $Author: lwvanels $
#

# First, set up some useful variables.

HOME=/; export HOME
PATH=/bin:/usr/ucb:/usr/bin

# Read configuration variables

. /etc/athena/rc.conf

# Check the disks.

if [ -r /fastboot ]; then
	rm -f /fastboot
	FASTBOOT=true; export FASTBOOT
	echo "Fast boot.  Skipping disk checks."		>/dev/console
elif [ $1x = autobootx ]; then
	echo "Automatic reboot in progress: "			>/dev/console
	date 							>/dev/console
	echo ""							>/dev/console
	echo "File system check: "				>/dev/console
	/etc/fsck -p 						>/dev/console
	case $? in
	0)
		echo "File system check done."			>/dev/console
		;;
	4)
		echo "fsck reports errors.  Rebooting."		>/dev/console
		/etc/reboot -n
		;;
	8)
		echo "Unrecoverable filesystem errors"		>/dev/console
		if [ ${ERRHALT}x = truex ]; then
			echo "Please contact Athena Operations"	>/dev/console
			echo "Halting machine..."		>/dev/console
			/etc/halt
		fi
		exit 1
		;;
	12)
		echo "Reboot interrupted."			>/dev/console
		if [ ${ERRHALT}x = truex ]; then
			echo "Please contact Athena Operations" >/dev/console
			echo "Halting machine..."		>/dev/console
			/etc/halt
		fi
		exit 1
		;;
	*)
		echo "Unknown error in reboot!"			>/dev/console
		if [ ${ERRHALT}x = truex ]; then
			echo "Please contact Athena Operations"	>/dev/console
			echo "Halting machine..."		>/dev/console
			/etc/halt
		fi
		exit 1
		;;
	esac
fi

# Clean up the password file.

echo -n "Cleaning up passwd file: "				>/dev/console
if [ -f /etc/passwd.local ]; then
	echo -n "recovered from passwd.local..."		>/dev/console
	/bin/rm -f /etc/passwd.dir /etc/passwd.pag
	/bin/cp /etc/passwd.local /etc/passwd
fi
echo "done."							>/dev/console

# Clean up the group file.

if [ -f /etc/group.local ]; then
	echo -n "Recovering group file..."			>/dev/console
	/bin/cp /etc/group.local /etc/group
	echo "done."						>/dev/console
fi

echo -n "Setting up filesystems: clearing mount table..."	>/dev/console
/etc/umount -a
cp /dev/null /etc/mtab

echo -n "cleaning root..."					>/dev/console
/bin/rm -rf /tmp/ /.deleted
if [ "${PUBLIC}" = "true" ]; then
	/bin/rm -rf /srvd /urvd
	/bin/mkdir /srvd /urvd
fi

echo "mounting filesystems..."					>/dev/console
/etc/mount -fv /						>/dev/console
/etc/mount -avt 4.2						>/dev/console
chmod 1777 /tmp /usr/tmp
echo "done."							>/dev/console

# Now start the network
echo ""								>/dev/console
/bin/sh /etc/athena/rc.net
if [ "$?" != "0" ]; then
	if [ ${ERRHALT}x = truex ]; then
		echo "Unable to initialize network."		>/dev/console
		echo "Please contact Athena Operations."	>/dev/console
		echo "Halting..."				>/dev/console
		/etc/halt
	else
		exit 1
	fi
fi

# Reset time with the time hub
if [ "${TIMECLIENT}" = "true" ]; then
	echo ""							>/dev/console
	echo -n "Setting time: "				>/dev/console
	if [ -f /etc/athena/gettime ]; then
		/etc/athena/gettime -s ${TIMEHUB}		>/dev/console
	else
		echo "Can't set time! gettime not found!"	>/dev/console
	fi
fi

if [ "${AFSSRV}" = "true" ]; then
	echo ""							>/dev/console
	echo -n "AFS server: "					>/dev/console
	if [ -f /usr/afs/bin/bosserver ]; then
		/usr/afs/bin/bosserver &
		echo "started."					>/dev/console
	else
		echo "Can't start AFS server!"
	fi
fi

# Start up afsd in foreground, in case any daemons are coming off AFS
if [ "${AFSCLIENT}" != "false" ]; then
	echo ""							>/dev/console
	echo -n "Starting AFS client: "				>/dev/console

	if [ \( "${AFSADJUST}" = "true" -o "${PUBLIC}" = "true" \) \
	     -a -d /usr/vice/cache/ ]; then

		echo -n "Adjusting the cache size..."		>/dev/console
		rm -f /usr/vice/etc/cacheinfo.old
		mv -f /usr/vice/etc/cacheinfo /usr/vice/etc/cacheinfo.old
		(  /bin/echo ${PUBLIC} ; \
		   /bin/df /usr/vice/cache/ ; \
		   /bin/du -s /usr/vice/cache/) | \
		 /bin/awk ' \
			(NR == 1) { if ($1=="true") { y=1 } else { y=0 } } \
			(NR == 3) { free = $4 + ($3 * y); } \
			(NR == 4) { x = int((free+$1*(1-y)-3000)*3/4); \
				if (x < 2000) { x = 2000 } ; \
				if (x > 40000) { x = 40000 } ; \
				printf("/afs:/usr/vice/cache:%d\n",x);}' \
		 > /usr/vice/etc/cacheinfo

		sed -n -e '1s/^.*:\([^:]*\)$/New AFS cache size = \1K/p' \
			/usr/vice/etc/cacheinfo 		>/dev/console
	fi
	if [ "${AFSCLIENT}" = "true" ]; then
		AFSCLIENT=4
	fi
	/etc/athena/afsd -nosettime -daemons "${AFSCLIENT}"
	cp -p /afs/athena.mit.edu/service/aklog /bin/athena/aklog
	echo -n "Configuring AFS cells..."			>/dev/console
	/etc/athena/config_afs
	echo "done"						>/dev/console
fi

echo ""								>/dev/console
echo -n "Starting syslog: "					>/dev/console
if [ -f /etc/athena/syslogd ]; then
	(sleep `echo $ADDR|awk -F. '{print int($4/2)}'`;/etc/athena/syslogd)&
	echo "done."						>/dev/console
else
	echo "can't find syslog daemon!"			>/dev/console
fi

echo ""								>/dev/console

# Here we should see about fsck'ing the local RVD packs to make sure
# they have not been corrupted.
if [ "${RVDSRV}" = "true" ]; then
	if [ -f /etc/athena/rvdstart ]; then
		/etc/athena/rvdstart				>/dev/console
	else
		echo "Can't start RVD server!"			>/dev/console
	fi
fi

if [ "${NFSCLIENT}" != "false" ]; then
	echo -n "NFS client: "					>/dev/console
	echo -n "flushing old NFS connections..."		>/dev/console
	/bin/athena/fsid -p -a
	echo "done."						>/dev/console
fi

if [ "${RVDCLIENT}" = "true" ]; then
	echo -n "RVD client: "					>/dev/console
	echo -n "flushing old virtual disks..."			>/dev/console
	RVDSERVERS=`grep RVD /usr/tmp/attachtab | awk '{print $6}'`
	if [ "${RVDSERVERS}" != "" ]; then 
		for i in ${RVDSERVERS}; do \
			(/etc/athena/rvdflush $i); \
		done
	fi
	echo "done."						>/dev/console
fi

echo -n "Removing old attach mount points..."			>/dev/console
/bin/athena/detach -O -n -h -a 
echo "done."							>/dev/console
/bin/cp /dev/null /usr/tmp/attachtab
if [ -f /usr/local/bin/attach_and_lock ]; then
        /usr/local/bin/attach_and_lock
fi

echo ""								>/dev/console
if [ "${RVDCLIENT}" = "true" ]; then
	echo -n "Getting cluster information: "			>/dev/console
	/etc/athena/save_cluster_info
	if [ -f /etc/athena/clusterinfo.bsh ]; then
		. /etc/athena/clusterinfo.bsh
	else
		echo -n "no information available..."		>/dev/console
	fi
	echo "done."						>/dev/console

	echo "Attaching system libraries:"			>/dev/console
	if [ "${USRLIB}" = "" ]; then
		/bin/athena/attach	-h -n -o hard $SYSLIB	>/dev/console
	else
		/bin/athena/attach	-h -n -o hard $SYSLIB \
					-h -n -o hard $USRLIB	>/dev/console
	fi
	case $? in
		0)  ;;
		*)
		  echo "Can't attach system libraries...continuing" >/dev/console
		  ;;
	esac
	echo "done."						>/dev/console
fi

# Take a new release, if necessary.
if [ "${AUTOUPDATE}" = "true" -a -f /srvd/auto_update ]; then
	echo -n "Checking /srvd/auto_update..."			>/dev/console
	/srvd/auto_update rc < /dev/console > /dev/console 2>&1
	echo "done."						>/dev/console
elif [ -f /srvd/.rvdinfo ]; then
	NEWVERS=`awk '{a=$5}; END{print a}' /srvd/.rvdinfo`
	VERSION=`awk '{a=$5}; END{print a}' /etc/athena/version`
	if [ "${NEWVERS}" != "${VERSION}" ]; then
		cat <<EOF					>/dev/console
The workstation software version ($VERSION) does not match the
version on the system packs ($NEWVERS).  A new version of software
may be available.  Please contact Athena Operations (x3-1410) to
have your workstation updated.
EOF
		if [ ! -f /usr/tmp/update.check -a -f /usr/ucb/logger ]; then
			/usr/ucb/logger -t `hostname` -p user.notice at revision $VERSION
			cp /dev/null /usr/tmp/update.check
		fi
	fi
fi

echo ""								>/dev/console

echo -n "Saving ex/vi files: "			 		>/dev/console
if [ -f /usr/lib/ex3.7preserve ]; then
	(cd /tmp; /usr/lib/ex3.7preserve -a)
fi
echo "done."							>/dev/console

# If this is a PUBLIC workstation, ensure that that there are no hacks.
# Do not update rc.conf if the workstation and srvd are different versions.
if [ "${PUBLIC}" = "true" ]; then
	echo -n "Public workstation cleanup..."			>/dev/console
	rm -rf /tmp/ /.hushlogin /etc/X0.hosts /usr/local/
	rm -f /etc/*.local /etc/athena/*.local
	rm -f /site/usr/lib/crontab /site/usr/lib/sendmail.cf
	ln -s /srvd/site/usr/lib/sendmail.cf /site/usr/lib/
	cp -p /srvd/etc/crontab /etc/crontab
	cp -p /srvd/etc/athena/attach.conf /etc/athena/attach.conf
	cp -p /srvd/etc/inetd.conf /etc/inetd.conf
	cp -p /srvd/etc/passwd /etc/passwd.local
	cp -p /etc/passwd.local /etc/passwd
	cp -p /srvd/etc/group /etc/group.local
	cp -p /etc/group.local /etc/group
	cp -p /etc/etc/syslog.conf /etc/syslog.conf
	cp /dev/null /etc/named.local
	cp /dev/null /etc/X0.hosts
	if [ -f /srvd/.rvdinfo ]; then
	    NEWVERS=`awk '{a=$5} END{print a}' /srvd/.rvdinfo`
	    THISVERS=`awk '{a=$5} END{print a}' /etc/version`
	    if [ "${NEWVERS}" = "${THISVERS}" ]; then
		/usr/athena/etc/track
		if [ -f /srvd/etc/athena/rc.conf ]; then
			sed -n	-e "s/^HOST=[^;]*/HOST=${HOST}/" \
				-e "s/^ADDR=[^;]*/ADDR=${ADDR}/" \
				-e "s/^MACHINE=[^;]*/MACHINE=${MACHINE}/" \
				-e p /srvd/etc/athena/rc.conf > /etc/athena/rc.conf
			. /etc/athena/rc.conf
		fi
	    fi
	fi
	(cd /dev; ./MAKEDEV ws)
	echo "done."						>/dev/console
fi

# Process crash dumps
if [ "${SAVECORE}" = "true" ] ; then
	echo -n "Saving core image: "				>/dev/console
	/etc/savecore /usr/crash				>/dev/console
	echo "done."						>/dev/console
	echo ""
elif [ "${SAVECORE}" = "false" ]; then
	echo -n "Removing old core images: "			>/dev/console
	rm -f /usr/crash/vmunix*
	rm -f /usr/crash/vmcore*
	rm -f /usr/crash/bounds
	echo "done."						>/dev/console
fi

echo -n "Adding swap devices: "					>/dev/console
/etc/swapon -a							>/dev/console
echo "done."							>/dev/console

echo ""								>/dev/console
if [ "${QUOTAS}" = "true" ]; then
	echo -n "Quota system: "				>/dev/console
	if [ "$1" = "autoboot" ]; then
	        if [ "${FASTBOOT}" != "true" ]; then
	                echo -n "checking quotas..."		>/dev/console
	                /usr/etc/quotacheck -a -p		>/dev/console
	        fi
	fi
	if [ -f /usr/etc/quotaon ]; then
		echo -n "enabling quotas..."			>/dev/console
		/usr/etc/quotaon -a
	else
		echo "Can't enable quotas! "			>/dev/console
	fi
	echo "done."						>/dev/console
fi

if [ "${NEWMAILCF}" = "true" -a -f /srvd/site/usr/lib/sendmail.cf ]; then
	echo -n "Updating sendmail.cf..."			>/dev/console
	rm -f /site/usr/lib/sendmail.cf
	cp -p /srvd/site/usr/lib/sendmail.cf /site/usr/lib/
	echo "done."						>/dev/console
fi
if [ -f /usr/lib/sendmail.cf ]; then
	echo -n "Freezing sendmail configuration..."		>/dev/console
	/usr/lib/sendmail -bz
	echo "done."						>/dev/console
fi
echo "Removing sendmail temporary files."			>/dev/console
rm -f /usr/spool/mqueue/[lxtn]f*

# Start standard daemons

echo -n "Starting standard daemons: "				>/dev/console
if [ -f /etc/update ]; then
	echo -n "update..."					>/dev/console
	/etc/update -n
fi
if [ -f /etc/cron ]; then
	echo -n "cron..."					>/dev/console
	/etc/cron
fi
if [ -f /etc/inetd ]; then
	echo -n "inetd..."					>/dev/console
	/etc/inetd &
fi
if [ "${NFSCLIENT}" != "false" ]; then
	echo -n "biod..."					>/dev/console
	if [ "${NFSCLIENT}" = "true" ]; then
		/etc/biod 4 &
	else
		/etc/biod "${NFSCLIENT}" &
	fi
fi
if [ "${RPC}" = "true" -o "${NFSSRV}" != "false" ]; then
	echo -n "portmapper..."					>/dev/console
	/etc/portmap &
	echo -n "rpcd..."					>/dev/console
	/etc/rpcd &
	if [ "${NFSSRV}" != "false" ]; then
		if [ -f /usr/athena/etc/rpc.mountd ]; then
			/usr/athena/etc/rpc.mountd -s		2>/dev/console
			case $? in
			0)
				echo -n "nfsd..."		>/dev/console
				if [ "${NFSSRV}" = "true" ]; then
					NFSOPTS=4
				else
					NFSOPTS="${NFSSRV}"
				fi
				/etc/nfsd ${NFSOPTS} &
			;;
			esac
		fi
	fi
fi
if [ "${NIPSRV}" = "true" ]; then
	echo -n "nipsrv..."					>/dev/console
	/usr/athena/etc/nipsrv on
fi
if [ "${TIMESRV}" = "true" ]; then
	echo -n "timed (master)..."				>/dev/console
	/etc/athena/timed -M &
elif [ "${TIMECLIENT}" = "true" ]; then
	echo -n "timed..."					>/dev/console
	/etc/athena/timed &
fi
if [ "${ACCOUNT}" = "true" ]; then
	echo -n "accounting..."					>/dev/console
	/usr/etc/accton /usr/adm/acct
fi
if [ "${LPD}" = "true" ]; then
	rm -f /dev/printer
	echo -n "printer..."					>/dev/console
	/usr/lib/lpd &
	if [ -f /usr/athena/etc/s_chkaf ]; then
		echo -n "s_chkaf..."				>/dev/console
		/usr/athena/etc/s_chkaf &
	fi
fi
if [ "${KRBSRV}" != "false" -a -f /.k ]; then
	echo -n "kerberos..."					>/dev/console
	if [ "${KRBSRV}" = "true" ]; then
		krbopt="-n"
	else
		krbopt="-r ${KRBSRV} -n"
	fi
	/usr/athena/etc/kerberos $krbopt &
	if [ "${KADMSRV}" = "true" ]; then
		echo -n "kadmind..."				>/dev/console
		/usr/athena/etc/kadmind $krbopt &
	fi
fi
if [ "${KNETD}" = "true" ]; then
	echo -n "knetd..."					>/dev/console
	/usr/athena/etc/knetd &
fi
if [ "${SNMP}" = "true" ]; then
	echo -n "snmpd..."					>/dev/console
	/etc/athena/snmpd &
fi

if [ "${SENDMAIL}" = "true" -a -f /usr/lib/sendmail ]; then
	echo -n "sendmail..."					>/dev/console
	/usr/lib/sendmail -bd -q30m &
fi

if [ "${OLC}" = "true" ]; then
	echo -n "olcd..."					>/dev/console
        /usr/athena/etc/olcd &
fi

if [ "${ZCLIENT}" = "true" ]; then
	echo -n "zhm..."					>/dev/console
	rm -f /etc/athena/zhm.pid
	/etc/athena/zhm &
fi
if [ "${ZSERVER}" = "true" ] ; then
	if [ -f /usr/etc/zephyrd ] ; then
		echo -n "zephyrd..."				>/dev/console
		/bin/csh -fc "unlimit; exec /usr/etc/zephyrd"	>/dev/console &
	else
		echo ""						>/dev/console
		echo "Zephyr server not found"			>/dev/console
	fi
fi

if [ "${MRUPDATE}" = "true" ]; then
	echo -n "update_server..."				>/dev/console
	/usr/athena/etc/update_server &
fi

if [ -f /etc/athena/galatead.conf ]; then
	echo -n "galatea..."					>/dev/console
	/usr/athena/etc/galatead &
fi

echo "done."							>/dev/console
echo ""								>/dev/console

if [ "${NFSCLIENT}" != "false" ]; then
	echo -n "Mounting default NFS filesystems..."		>/dev/console
	/etc/mount -avt nfs
	echo "done."						>/dev/console
fi

if [ "${PUBLIC}" = "true" ]; then
	echo -n "Clearing out /mit..."				>/dev/console
	/bin/rm -f /mit/*
	/bin/rmdir /mit/*
	echo "done."						>/dev/console
fi

echo -n "Editing /etc/motd: "					>/dev/console
awk '{ prev = $0 } END { print prev }' /etc/athena/version	> /tmp/t1
if [ "${PUBLIC}" != "true" ]; then
	awk '{ if ( NR > 1 ) print }' /etc/motd			>>/tmp/t1
fi
mv /tmp/t1 /etc/motd
chmod 644 /etc/motd
echo "done."							>/dev/console

echo ""								>/dev/console
echo -n "Fixing noattach, nocreate, and nologin..."		>/dev/console
if [ "${NOCREATE}" = "true" ]; then
	echo -n "" >> /etc/nocreate
else
	rm -f /etc/nocreate
fi
if [ "${NOATTACH}" = "true" ]; then
	echo -n "" >> /etc/noattach
else
	rm -f /etc/noattach
fi
rm -f /etc/nologin
echo "done."							>/dev/console
echo "Logins now possible."					>/dev/console
echo ""								>/dev/console

echo -n "Initializing psdatabase: "				>/dev/console
/bin/ps -U
echo "done."							>/dev/console

echo -n "Cleaning up uucp lock files: "				>/dev/console
/bin/rm -f /usr/spool/uucp/LCK.*
echo "done."							>/dev/console

echo -n "Correcting tty protection modes: "			>/dev/console
/bin/chmod 666 /dev/tty[pqrsS]*
/etc/chown root.tty /dev/tty[pqrsS]*
echo "done."							>/dev/console

if [ -f /etc/rc.local ]; then
	echo "Executing rc.local..."				>/dev/console
	sh /etc/rc.local
	echo "done."						>/dev/console
fi
exit 0
