#!/bin/sh
#	Athena boot startup script for use with Ultrix 4.2
#
#	$Id: rc.athena,v 1.5 98/04/22 04:29:36 mhpower Exp $

# First, set up some useful variables.

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

# Read configuration variables

. /etc/athena/rc.conf

# 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 "cleaning root..."					>/dev/console
/bin/rm -rf /tmp/ /.deleted
if [ "${PUBLIC}" = "true" ]; then
	/bin/rm -rf /srvd /urvd
fi
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 == 4) { own=0; if($6=="/usr/vice/cache"){\
					own=1;free=$2*85/100;}\
					else free = $4 + ($3 * y); } \
			(NR == 5) { if(own==1) x = free;\
				else {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 [ "${TIMESRV}" = "true" -o "${TIMECLIENT}" != "true" ]; then
		AFSOPTS="-nosettime"
	fi
	if [ "${AFSCLIENT-true}" = "true" ]; then
		AFSCLIENT=4
	fi
	AFSOPTS="${AFSOPTS} -daemons ${AFSCLIENT}"
	/etc/athena/afsd ${AFSOPTS}
	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/syslog ]; then
		/etc/syslog -L					>/dev/console
		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/nfsid -p -a
	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

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

# 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 /site/usr/lib/*.local
	rm -f /site/usr/lib/crontab /site/usr/lib/sendmail.cf /etc/athena/login/*.local
#	ln -s /srvd/usr/etc/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 /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/athena/version`
	    if [ "${NEWVERS}" = "${THISVERS}" ]; then
		echo "Running track..."				> /dev/console
		/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 "s/^NETDEV=[^;]*/NETDEV=${NETDEV}/" \
				-e p /srvd/etc/athena/rc.conf > /etc/athena/rc.conf
			. /etc/athena/rc.conf
		fi
	    fi
	fi
	echo "done."						>/dev/console
fi

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

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" ]; then
	echo -n "Updating sendmail.cf..."			>/dev/console
	if [ -f /srvd/etc/sendmail.cf ]; then
		rm -f /etc/sendmail.cf
		cp -p /srvd/etc/sendmail.cf /etc/sendmail.cf
	fi
	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

if [ "${NFSCLIENT}" != "false" ]; then
	echo -n "biod..."					>/dev/console
	if [ "${NFSCLIENT}" = "true" ]; then
		/etc/biod 4 &
	else
		/etc/biod "${NFSCLIENT}" &
	fi
fi

# Always run portmapper for third party software.
echo -n "portmapper..."						>/dev/console
/etc/portmap 

if [ "${RPC}" = "true" -o "${MOUNTD}" = "true" -o "${NFSSRV}" != "false" ];then
        if [ -f /usr/local/etc/mountd -a -s /etc/exports ]; then
                echo -n 'mountd...'                     >/dev/console
		/usr/local/etc/mountd
        fi
        if [ "${NFSSRV}" != "false" ]; then
                echo -n "nfsd..."                       >/dev/console
                if [ "${NFSSRV}" = "true" ]; then
                        NFSOPTS=4
                else
                        NFSOPTS="${NFSSRV}"
                fi
                (/usr/bin/sleep 240; /etc/nfsd ${NFSOPTS}) &
        fi
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 "qmail... "                            > /dev/console
		env - PATH="/var/qmail/bin:$PATH" qmail-start ./Mailbox splogger qmail &
		/usr/local/bin/tcpserver -H -lbloom-picayune.mit.edu -R -v -c 30 -u 89 -g 93 0 smtp /var/qmail/bin/qmail-smtpd 2>&1 | /var/qmail/bin/splogger smtpd 2 &
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
	echo -n "zephyrd..."					>/dev/console
	/usr/athena/etc/zephyrd &
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, noremote, 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
if [ "${NOREMOTE}" = "true" ]; then
        echo -n "" >> /etc/noremote
else
        rm -f /etc/noremote
fi
rm -f /etc/nologin
echo "done."							>/dev/console
echo "Logins now possible."					>/dev/console
echo ""								>/dev/console

echo -n "Correcting tty protection modes: "			>/dev/console
/bin/chmod 666 /dev/tty[pqrsS]*
if [ "${SYSTEM}" = "ULTRIX31" -o "${SYSTEM}" = "ULTRIX42" ]; then
	/etc/chown root /dev/tty[pqrsS]*
else
	/etc/chown root.tty /dev/tty[pqrs$]*
fi
echo "done."							>/dev/console

if [ "${SYSTEM}" = "ULTRIX31" -o "${SYSTEM}" = "ULTRIX42" ]; then
#
# Removing Opser remnant files
#
        echo -n 'Removing remnant Opser files...'               >/dev/console
        rm -f /etc/opseractive
        rm -f /etc/remoteopser
        rm -f /etc/sdnologin
        echo 'done.'                                            >/dev/console
fi
