#! /bin/sh
#
# $Id: athena,v 1.1.1.9 1998/07/18 21:31:44 nathanw Exp $
# /etc/rc.d/init.d/athena - Start/Stop the athena daemons

# chkconfig: 35 75 17
# description: Starts and stops Athena services.  Necessary for the proper \
# operation of some Athena software.

# First, set up some useful variables.

HOME=/; export HOME
PATH=/bin:/usr/bin:/usr/sbin:/etc:/sbin; export PATH

umask 022

case $1 in
'start')

echo "Beginning Athena startup tasks"

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

# Clean up the password file.
echo -n "Cleaning up passwd file... "
if [ -f /etc/passwd.local ]; then
	echo -n "recovered from passwd.local... "
	cp /etc/passwd.local /etc/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

# Make sure /var/athena/core.root exists.
if [ ! -r /var/athena/core.root ]; then
	touch /var/athena/core.root
	chmod 660 /var/athena/core.root
	chown root:daemon /var/athena/core.root
fi

# Remove old login session records.
rm -rf /var/athena/sessions/*

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

# afsd is started by init.d/afs.  We just do our little additions here.
if [ "$AFSCLIENT" != false ]; then
	sh /etc/athena/config_afs
fi

# fsid not working yet so next block commented out; still true???
#if [ "$NFSCLIENT" != false ]; then
#	echo "Flushing old NFS connections... \c"
#	/bin/athena/fsid -p -a
#	echo "done."
#fi

echo "Removing old attach mount points"
/bin/athena/detach -L -O -n -h -a

if [ "$PUBLIC" = true ]; then
	cp /dev/null /var/athena/attachtab
fi

if [ "$RVDCLIENT" = true ]; then
	echo "Getting cluster information"
	/etc/athena/save_cluster_info
	if [ -f /var/athena/clusterinfo.bsh ]; then
		. /var/athena/clusterinfo.bsh
		echo "Attaching system libraries"
		/bin/athena/attach -h -n -o hard -O $SYSLIB
	fi
fi

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

# 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 "Beginning public workstation cleanup"
	rm -rf /.hushlogin /etc/X0.hosts /etc/nologin.persist /usr/local
	rm -f /etc/*.local /etc/athena/*.local /etc/athena/login/*.local
	rm -f /etc/ssh_*
	if [ -r /srvd/etc/passwd ]; then
		cp -p /srvd/etc/passwd /etc/passwd.local
		chmod 644 /etc/passwd.local
		chown root /etc/passwd.local
	fi
	if [ -r /srvd/etc/shadow ]; then
		cp -p /srvd/etc/shadow /etc/shadow.local
		chmod 600 /etc/shadow.local
		chown root /etc/shadow.local
	fi
	cp -p /etc/passwd.local /etc/passwd
	cp -p /etc/shadow.local /etc/shadow
	chmod 600 /etc/shadow
	cp -p /srvd/etc/group /etc/group.local
	cp -p /etc/group.local /etc/group

	cp /dev/null /etc/X0.hosts
	cp -p /etc/inet/hosts /etc/inet/hosts.save

	echo "Ending public workstation cleanup"
fi

# Start standard daemons.
echo -n "Starting Athena daemons: "

# Start Athena's 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="-n"
	else
		krbopt="-r $KRBSRV -n"
	fi
	/usr/athena/etc/kerberos $krbopt &
	if [ "$KADMSRV" = true ]; then
		echo -n "kadmind... "
		/usr/athena/etc/kadmind $krbopt &
	fi
fi

if [ "$KNETD" = true ]; then
	echo -n "knetd... "
	/usr/athena/etc/knetd &
fi

if [ "$OLC" = true ]; then
	echo -n "olcd... "
	/usr/athena/etc/olcd &
	echo -n "rpd... "
	/usr/athena/etc/rpd &
fi

if [ "$ZSERVER" = true ] ; then
	echo -n "zephyrd... "
	/usr/athena/etc/zephyrd &
fi

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

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

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

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

echo "done."

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

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 -f /tmp/t1 /etc/motd
chmod 644 /etc/motd
echo "done."

echo -n "Fixing noattach, nocreate, noremote, and nologin... "
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."

if [ -r /etc/nologin.persist ]; then
	cp /etc/nologin.persist /etc/nologin
	echo "Logins disabled."
else
	rm -f /etc/nologin
	echo "Logins now possible."
fi

echo "Ending Athena startup tasks"

	;;
'stop')
	if [ -s /var/athena/zhm.pid ]; then
		kill `cat /var/athena/zhm.pid`
	fi
	if [ -s /var/athena/inetd.pid ]; then
		kill `cat /var/athena/inetd.pid`
	fi
	if [ -s /var/athena/sshd.pid ]; then
		kill `cat /var/athena/sshd.pid`
	fi
	;;
*)
	echo "usage: athena {start|stop}"
	;;
esac
