# $Id: rc.athena,v 1.6 1999/06/27 21:53:02 nathanw Exp $

# /etc/athena/rc.athena - Start the athena daemons

# Add some Athena directories to the path.  /usr/athena/etc goes at the
# end so that we don't check AFS for binaries which are on local disk.
PATH=/etc/athena:/bin/athena:$PATH:/usr/athena/etc

echo "Beginning Athena startup tasks"

# Read configuration variables
. /etc/athena/rc.conf

# Store boot-time environment for starting server processes later.
rm -f /var/athena/env.boot
env > /var/athena/env.boot

# Start AFS
if [ "$AFSCLIENT" != false ]; then
	. /etc/athena/rc.afs
fi

# Clean up the password file.
echo -n "Cleaning up passwd file... "
if [ -f /etc/master.passwd.local ]; then
	echo -n "recovered from passwd.local... "
	cp /etc/master.passwd.local /etc/master.passwd
	pwd_mkdb -p /etc/master.passwd
fi
echo "done."

# Clean up the group file.
if [ -f /etc/group.local ]; then
	echo -n "Recovering group file... "
	cp /etc/group.local /etc/group
	echo "done."
fi

# Clean up login session records.
rm -f /var/athena/sessions/*

# For some reason, emacs leaves behind a lock file sometimes.  Nuke it.
rm -f /var/tmp/!!!SuperLock!!!

# Reset time with the time hub
if [ "$TIMECLIENT" = true ]; then
	echo -n "Setting time: "
       	gettime -s "$TIMEHUB"
fi

if [ "$NFSCLIENT" != false ]; then
	echo -n "Flushing old NFS connections... "
	fsid -p -q -a
	echo "done."
fi

echo -n "Removing old attach mount points... "
detach -O -n -h -a
echo "done."

if [ "$PUBLIC" = true ]; then
	rm -f /var/athena/attachtab/mountpoint/*
	rm -f /var/athena/attachtab/locker/*
	rm -rf /srvd /os
fi

if [ "$RVDCLIENT" = true ]; then
	echo -n "Getting cluster information... "
	save_cluster_info
	if [ -f /var/athena/clusterinfo.bsh ]; then
		echo "done."
		. /var/athena/clusterinfo.bsh
		echo "Attaching system libraries:"
		attach -h -n -o hard -O $SYSLIB
		echo "Updating /etc/athena/cluster.fallback"
		cp -f /srvd/etc/athena/cluster.fallback \
			/etc/athena/cluster.fallback
	else
		echo "no information available."
	fi
fi

# Take a new release, if necessary.
if [ -x /srvd/auto_update ]; then
	/srvd/auto_update rc
fi

# If this is a PUBLIC workstation, ensure that that there are no hacks.
if [ "$PUBLIC" = "true" ]; then
	echo "Beginning public workstation cleanup"
	rm -rf /.hushlogin /usr/local/
	rm -f /etc/*.local /etc/athena/*.local /etc/athena/login/*.local
	rm -f /etc/ssh_*
	if [ -r /srvd/etc/master.passwd ]; then
		cp -p /srvd/etc/master.passwd /etc/master.passwd.local
		chmod 600 /etc/master.passwd.local
		chown root /etc/master.passwd.local
	fi
	cp -p /etc/master.passwd.local /etc/master.passwd
	pwd_mkdb -p /etc/master.passwd
	cp -p /srvd/etc/group /etc/group.local
	cp -p /etc/group.local /etc/group
	touch /etc/named.local
	cp -p /os/etc/rc.local /etc/rc.local
	cp -p /srvd/etc/athena/athinfo.access /etc/athena/athinfo.access
	rm -f /mit/*
	rmdir /mit/* 2>/dev/null
	if [ -f /srvd/.rvdinfo ]; then
		# Do not track or update rc.conf if the workstation and srvd
		# are different versions.
		newv=`awk '{ a = $5; } END { print a; }' /srvd/.rvdinfo`
		thisv=`awk '{ a = $5; } END { print a; }' /etc/athena/version`
		if [ "$newv" = "$thisv" ]; then
			echo "Running track"
			track
			if [ -f /srvd/etc/athena/rc.conf ]; then
				rm -f /etc/athena/rc.conf
	       			sed -e "s#^HOST=[^;]*#HOST=$HOST#" \
				  -e "s#^ADDR=[^;]*#ADDR=$ADDR#" \
				  -e "s#^NETDEV=[^;]*#NETDEV=$NETDEV#" \
				  -e "s#^NETMEDIA=[^;]*#NETMEDIA=$NETMEDIA#" \
				  -e "s#^MACHINE=[^;]*#MACHINE=$MACHINE#" \
				  -e "s#^SYSTEM=[^;]*#SYSTEM=$SYSTEM#" \
				  /srvd/etc/athena/rc.conf \
				  > /etc/athena/rc.conf
       	        	. /etc/athena/rc.conf
			fi
			configs=`cat /srvd/usr/athena/lib/update/configfiles`
			for i in $configs; do
				if [ -f /srvd$i ]; then
					cp -p /srvd$i $i
				else
					cp -p /os$i $i
				fi
			done
			ps -x | awk '$5~/^inetd/ { print $1; }' \
				| xargs kill -HUP
		fi
	fi

        echo "Ending public workstation cleanup"
fi

# /srvd might not have been attached when ldconfig was first run.
echo "Adding Athena shared libraries to link editor cache"
ldconfig -sm /usr/athena/lib

echo -n "Starting Athena daemons: "

# Start Athena's inetd.  Use full pathname to distinguish from native inetd.
echo -n "inetd... "
if [ "$ACCESSON" = true ]; then
	/etc/athena/inetd -n
else
	/etc/athena/inetd
fi

if [ "$KRBSRV" != false -a -f /.k ]; then
	echo -n "kerberos... "
	if [ "$KRBSRV" = true ]; then
		krbopt=""
	else
		krbopt="-r KRBSRV"
	fi
	krb5kdc $krbopt
	if [ "$KADMSRV" = true ]; then
		# Use full pathname to distinguish from native kadmind.
		echo -n "kadmind... "
		/usr/athena/etc/kadmind $krbopt
	fi
fi

if [ "$OLC" = true ]; then
	echo -n "olcd... "
	olcd
	echo -n "rpd... "
	rpd
fi

if [ "$ZSERVER" = true ]; then
	echo -n "zephyrd... "
	zephyrd
fi

if [ "$ZCLIENT" = true ]; then
	echo -n "zhm... "
	zhm
fi

if [ "$LPD" = true ]; then
	echo -n "lpd... "
	/usr/athena/etc/lpd
fi

if [ "$MRUPDATE" = true ]; then
	echo -n "update_server... "
	update_server
fi

if [ "$SSHD" = true -o \( "$SSHD" = switched -a "$ACCESSON" = true \) ]; then
	echo -n "sshd... "
	sshd
fi

echo "done."

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

echo -n "Fixing noattach, nocreate, and noremote... "
if [ "$NOCREATE" = true ]; then
	touch /etc/nocreate
else
	rm -f /etc/nocreate
fi
if [ "$NOATTACH" = true ]; then
	touch /etc/noattach
else
	rm -f /etc/noattach
fi
if [ "$NOREMOTE" = true ]; then
        touch /etc/noremote
else
        rm -f /etc/noremote
fi

echo "done."

echo "Ending Athena startup tasks"
