# /etc/init.d/athena - Start/Stop the athena daemons
#
#

# First, set up some useful variables.

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

umask 022


case $1 in 
'start')

# Let the log host know that we're booting, and whether there was a panic.

panic=`dmesg | awk '
	/^panic/	{ panic = " (" $0 ")"; boots = 0; }
	/UNIX(R)/	{ if (++boots == 2) panic = ""; }
	END		{ print panic; }'`
logger -p user.notice "Booting$panic"

echo  "MIT configuration..." > /dev/console

# Read configuration variables

. /etc/athena/rc.conf

# Clean up the password file.

echo "Cleaning up passwd file: \c"				>/dev/console
if [ -f /etc/passwd.local -a -f /etc/shadow.local ]; then
	echo "recovered from passwd.local... \c"		>/dev/console
	/bin/cp /etc/passwd.local /etc/passwd
	/bin/cp /etc/shadow.local /etc/shadow
fi
echo "done."							>/dev/console

# Clean up the group file.

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

# Remove old cruft and locks
rm -rf /.deleted /etc/sm/* /etc/sm.bak/*

# Reset time with the time hub
if [ "${TIMECLIENT}" = "true" ]; then
	echo ""							>/dev/console
	echo  "Setting time: \c"				>/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

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

echo "Loading pcfile system... \c"				>/dev/console
/usr/sbin/modload /usr/kernel/fs/pcfs
echo "Loading fd driver... \c"					>/dev/console
/usr/sbin/modload /usr/kernel/fs/fdfs
echo "Done"

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

echo "Removing old attach mount points... \c"			>/dev/console
/bin/athena/detach -L -O -n -h -a
echo "done."							>/dev/console

echo ""								>/dev/console

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

if [ "${RVDCLIENT}" = "true" ]; then
	echo "Getting cluster information: \c"			>/dev/console
	/etc/athena/save_cluster_info
	if [ -f /var/athena/clusterinfo.bsh ]; then
		. /var/athena/clusterinfo.bsh
		echo "Attaching system libraries:"		>/dev/console
		/bin/athena/attach -h -n -o hard -O $SYSLIB	>/dev/console
		echo "done."					>/dev/console
	else
		echo "no information available..."		>/dev/console
	fi
fi

# Take a new release, if necessary.
echo  "Checking /srvd/auto_update... \c"		>/dev/console
if [ -x /srvd/auto_update ]; then
	/srvd/auto_update rc < /dev/console > /dev/console 2>&1
fi
echo "done."						>/dev/console
echo ""							>/dev/console

# Whenever Solaris decides to configure the devices directory, it insists
# on creating /dev/audio and /dev/audioctl mode 600 (which would require
# various CD-playing programs to be setuid), despite our efforts.  Make
# sure they're world-writable.
chmod 666 /dev/audio /dev/audioctl

# 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 "Public workstation cleanup... \c"			>/dev/console
	rm -rf /.hushlogin /etc/X0.hosts /usr/local/
	rm -f /etc/*.local /etc/athena/*.local /etc/athena/login/*.local
	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/named.local
	cp -p /srvd/etc/netspy /etc/netspy
	cp /dev/null /etc/X0.hosts
	cp -p /etc/inet/hosts /etc/inet/hosts.save
	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 -F /os -T / -W /srvd/usr/athena/lib \
			-s stats/os_rvd slists/os_rvd
		/usr/athena/etc/track
		cp -p /etc/inet/hosts.save /etc/inet/hosts
		rm -f /etc/inet/hosts.save
		if [ -f /srvd/etc/athena/rc.conf ]; then
       			sed -n  -e "s/^HOST=[^;]*/HOST=${HOST}/" \
			  -e "s/^ADDR=[^;]*/ADDR=${ADDR}/" \
			  -e "s/^NETDEV=[^;]*/NETDEV=${NETDEV}/" \
			  -e "s/^MACHINE=[^;]*/MACHINE=${MACHINE}/" \
			  -e p /srvd/etc/athena/rc.conf > /etc/athena/rc.conf
       	        	. /etc/athena/rc.conf
		fi
		for i in `cat /srvd/usr/athena/lib/update/configfiles`; do
			if [ -f /srvd$i ]; then
				cp -p /srvd$i $i
			else
				cp -p /os$i $i
			fi
		done
	    fi
	fi

	# MAE tries to open /dev/dsk/c0t6d0s[02] to read the floppy
	# and CDROM, and complains if they can't be read.  (In the
	# case of the floppy, it can still read the disk by going
	# through vold, but it can't read the CD-ROM.)  Work around
	# that here.  Better solutions are welcome.
	chmod a+r /dev/dsk/c0t6d0s0 /dev/dsk/c0t6d0s2

        echo "done."						>/dev/console
fi

echo "Removing sendmail temporary files."			>/dev/console
if [ -d /var/spool/mqueue ]; then
  (cd /var/spool/mqueue; rm -f nf* lf*)
fi
# Sync aliases db.
/usr/lib/sendmail -bi

# Start standard daemons
echo ""								>/dev/console
echo  "Starting Athena daemons: \c"				>/dev/console

#	if [ "${TIMECLIENT}" = "true" ]; then
#		echo  "timed..."				>/dev/console
#		/etc/athena/timed &
#	fi

# Start Athena's inetd
echo "inetd... \c"						>/dev/console
/etc/athena/inetd &

if [ "${KRBSRV}" != "false" -a -f /.k ]; then
	echo "kerberos... \c"					>/dev/console
	if [ "${KRBSRV}" = "true" ]; then
		krbopt="-n"
	else
		krbopt="-r ${KRBSRV} -n"
	fi
	/usr/athena/etc/kerberos $krbopt &
	if [ "${KADMSRV}" = "true" ]; then
		echo "kadmind... \c"				>/dev/console
		/usr/athena/etc/kadmind $krbopt &
	fi
fi

if [ "${KNETD}" = "true" ]; then
	echo "knetd... \c"					>/dev/console
	/usr/athena/etc/knetd &
fi

if [ "${SNMP}" = "true" ]; then
	echo  "snmpd... \c"					>/dev/console
	/etc/athena/snmpd &
fi

if [ "${SENDMAIL}" = "true" ]; then
	echo  "sendmail... \c"					>/dev/console
	/usr/lib/sendmail -bd -q1h;
fi

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

if [ "${ZSERVER}" = "true" ] ; then
	echo  "zephyrd... \c"					>/dev/console
	/usr/athena/etc/zephyrd &
fi

if [ "${ZCLIENT}" = "true" ]; then
	echo  "zhm... \c"					>/dev/console
	rm -f /var/athena/zhm.pid
	/etc/athena/zhm &
fi

if [ "${LPD}" = "true" ]; then
	echo  "lpd... \c"					>/dev/console
	/usr/athena/etc/lpd &
fi

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

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

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

echo "Editing /etc/motd... \c"					>/dev/console
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."							>/dev/console

echo  "Fixing noattach, nocreate, noremote, and nologin... \c"	>/dev/console
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."							>/dev/console

rm -f /etc/nologin
echo "Logins now possible."					>/dev/console

echo " done!"							>/dev/console
	;;
'stop')
	pid=`/usr/bin/ps -e | /usr/bin/grep timed | /usr/bin/sed -e 's/^  *//' -e 's/ .*//'`
	if test "$pid"
	then
		kill $pid
	fi
	pid=`/usr/bin/ps -e | /usr/bin/grep snmpd | /usr/bin/sed -e 's/^  *//' -e 's/ .*//'`
	if test "$pid"
	then
		kill $pid
	fi
	pid=`/usr/bin/ps -e | /usr/bin/grep zhm | /usr/bin/sed -e 's/^  *//' -e 's/ .*//'`
	if test "$pid"
	then
		kill $pid
	fi
	;;
*)
	echo "usage: /etc/rc2.d/S90athena {start|stop}"
	;;
esac
