#!/bin/sh
#
# Update script for Project Athena workstations
#
# $Id: update_ws,v 1.35 94/07/08 14:34:34 vrt Exp $
#

trap "" 1 15

ROOT="${ROOT-}"; export ROOT
CONFDIR="${CONFDIR-/etc/athena}"; export CONFDIR
LIBDIR=/srvd/usr/athena/lib/update;	export LIBDIR
PATH=/bin:/srvd/bin:/srvd/etc:/srvd/etc/athena:/srvd/bin/athena:/srvd/usr/athena/etc:/srvd/usr/bin:/srvd/usr/etc:/srvd/usr/ucb:/usr/bin:/usr/ucb:$LIBDIR ; export PATH

if [ ! -d /srvd/bin ]; then
	exit 1
fi

if [ ! -f ${ROOT}/${CONFDIR}/version -a -f ${ROOT}/etc/version ]; then
	CONFDIR=/etc; export CONFDIR;
fi

if [ ! -f ${ROOT}/${CONFDIR}/version ]; then
	echo "Athena Update (???) Version 0.0A Mon Jan 1 00:00:00 EDT 0000" \
		> ${ROOT}/${CONFDIR}/version
fi

NEWVERS=`awk '{a=$5}; END{print a}' /srvd/.rvdinfo`
VERSION=`awk '{a=$5}; END{print a}' ${ROOT}/${CONFDIR}/version`
export NEWVERS VERSION

if [ -f ${ROOT}/${CONFDIR}/rc.conf ]; then
   . ${ROOT}/${CONFDIR}/rc.conf
else
   PUBLIC=true; export PUBLIC
   AUTOUPDATE=true; export AUTOUPDATE
fi

AUTO=false
if [ `expr $0 : '.*auto_update'` != "0" ]; then
	AUTO=true;
fi

case `echo $VERSION $NEWVERS | awk '(NR==1) { \
	if ($1 == "Update") {print "OOPS"} \
	else if ($1 >= $2) {print "OK"} \
	else if (substr($1,1,3) == substr($2,1,3)) {print "INCR"} \
	else {print "FULL"}}'` in
OOPS)
	if [ ! -f /usr/tmp/update.check -a -f /usr/ucb/logger ]; then
		/usr/ucb/logger -t `/bin/hostname` -p user.notice at revision $VERSION
		cp /dev/null /usr/tmp/update.check
	fi

	echo "This system is in the middle of an update.  Please contact"
	echo "Athena Operations at x3-1410. Thank you. -Athena Operations"
	exit 1
	;;
OK)
	if [ "${AUTO}" != "true" ]; then
		echo "It appears you already have this update."
	fi
	exit 0
	;;
INCR|FULL)
	;;
esac

if [ "${PUBLIC}" = "true" ]; then
	AUTOUPDATE=true
fi


if [ "${AUTO}" = "true" -a "${AUTOUPDATE}" != "true" ]; then
	/bin/cat <<EOF

	A new version of Athena software is now available.
	Please contact Athena Operations to get more information
	on how to update your workstation yourself, or to schedule
	us to do it for you. Thank you.  -Athena Operations
EOF
	if [ ! -f /usr/tmp/update.check -a -f /usr/ucb/logger ]; then
		/usr/ucb/logger -t `/bin/hostname` -p user.notice at revision $VERSION
		cp /dev/null /usr/tmp/update.check
	fi

	case "$1" in
	reactivate|rc|"") ;;
	*) sleep 20 ;;
	esac

	exit 1
fi

# Version-specific updating (forcing compatibility, if necessary)
case $VERSION in
7.3*)	;;
*)
	# For an update to succeed on the DECstation, svc.conf must be present
	# or the whoami will fail
	if [ -f /srvd/etc/svc.conf -a ! -f /etc/svc.conf ]; then
        	/srvd/bin/cp -p /srvd/etc/svc.conf /etc/svc.conf
	fi
	;;
esac

if [ "`/usr/ucb/whoami`" != "root" ];  then
	/bin/echo "You are not root.  This update script must be run as root."
	exit 1
fi

case `/bin/athena/machtype` in
decmips)
	SITE=/var
	MKSERV=/mit/mkserv/decmipsbin/mkserv
	;;
rsaix)
	SITE=/var
	MKSERV=/mit/mkserv/rsaixbin/mkserv
	;;
sun4)
	SITE=/var
#       MKSERV=/mit/mkserv/sun4bin/mkserv
	;;
vax|rt|*)
	SITE=/site
	MKSERV=/mit/mkserv/`/bin/athena/machtype`bin/mkserv
	;;
esac

if [ -d ${SITE}/server ] ; then
	# shutdown kills off named which we need.
	case `/bin/athena/machtype` in
	rsaix)
		/etc/athena/named
		;;
	*)
		/etc/named /etc/named.boot
		;;
	esac

	/bin/athena/attach -q -h -n -o hard mkserv
	if [ -s ${MKSERV} ]; then
		${MKSERV} updatetest
		if [ $? -ne 0 ]; then
			echo "Update cannot be performed as mkserv services cannot be found for all services"
			exit 1
		fi
	else
		echo "Update cannot be performed as mkserv binary not available"
	fi
fi

if [ ! -d ${ROOT}/.deleted ] ; then
	mkdir ${ROOT}/.deleted
fi

if [ "${AUTO}" = "true" -a "$1" = "reactivate" ]; then
	if [ -f /etc/athena/dm.pid ]; then
		kill -FPE `cat /etc/athena/dm.pid`
		ln ${ROOT}/etc/athena/dm ${ROOT}/.deleted/
	fi
	sleep 2
fi

FULLCOPY=true; export FULLCOPY

if [ ${AUTO}x = "truex" ]; then
	/bin/cat <<EOF

THIS WORKSTATION IS ABOUT TO UNDERGO AN AUTOMATIC SOFTWARE UPDATE.
THIS PROCEDURE MAY TAKE SOME TIME.

PLEASE DO *NOT* DISTURB IT WHILE THIS IS IN PROGRESS.

EOF
        rm /tmp/pids
        cat /etc/inetd.pid >/tmp/pids
        cat /etc/athena/snmpd.pid >>/tmp/pids
        cat /etc/athena/zhm.pid >>/tmp/pids
        cat /etc/syslog.pid >>/tmp/pids
	case "$1" in
	reactivate|activate|deactivate)
		(kill -TERM `cat /tmp/pids`) > /dev/null 2>&1
			;;
	esac

	/bin/sh $LIBDIR/do_update < /dev/null
	/bin/athena/detach -h -n -q -a
	/bin/sync
	case "${SYSTEM}" in
ULTRIX*)
		/bin/echo "Auto Update Done, System will now reboot."
		/bin/sync
		exec /etc/reboot
		;;
*)
		/bin/echo "Auto Update Done, System will reboot in 15 seconds."
		/bin/sync
		/bin/sleep 15
		exec /etc/reboot -q
		;;
	esac
else
	/bin/sh $LIBDIR/do_update
fi
echo "Done"
exit 0


# This file is the most convenient place to make changes that have to be
# applied to all workstations.
#
# The following is "filler", in case a live-RVD patch has to be performed.
# Because live RVD edits are easier if the files are the same length, we
# are allowing for some room to make changes to this file.
#
# 34567890123456789012345678901234567890123456789012345678901234567890
# 34567890123456789012345678901234567890123456789012345678901234567890
# 34567890123456789012345678901234567890123456789012345678901234567890
# 34567890123456789012345678901234567890123456789012345678901234567890
# 34567890123456789012345678901234567890123456789012345678901234567890
# 34567890123456789012345678901234567890123456789012345678901234567890
# 34567890123456789012345678901234567890123456789012345678901234567890
# 34567890123456789012345678901234567890123456789012345678901234567890
# 34567890123456789012345678901234567890123456789012345678901234567890
# 34567890123456789012345678901234567890123456789012345678901234567890
# 34567890123456789012345678901234567890123456789012345678901234567890
# 34567890123456789012345678901234567890123456789012345678901234567890
# 34567890123456789012345678901234567890123456789012345678901234567890
# 34567890123456789012345678901234567890123456789012345678901234567890
# 34567890123456789012345678901234567890123456789012345678901234567890
# 34567890123456789012345678901234567890123456789012345678901234567890
# 34567890123456789012345678901234567890123456789012345678901234567890
# 34567890123456789012345678901234567890123456789012345678901234567890
# 34567890123456789012345678901234567890123456789012345678901234567890
# 34567890123456789012345678901234567890123456789012345678901234567890
