#!/bin/sh 
# Script to bounce the packs on an Athena workstation
#
# $Header: /mit/sipb-athena/src/reactivate/RCS/reactivate,v 1.9 1996/02/19 02:35:42 ghudson Exp svalente $

trap "" 1 15

PATH=/bin:/bin/athena:/usr/bin:/usr/athena/bin:/usr/sbin; export PATH

umask 022
. /etc/athena/rc.conf

# Default options
dflags="-clean"


# Set various flags (based on environment and command-line)
if [ "$1" = "-detach" ]; then dflags=""; fi

if [ "${USER}" = "" ]; then
	exec 1>/dev/console 2>&1
	quiet=-q
else
	echo Reactivating workstation...
	quiet=
fi

case "${SYSTEM}" in
ULTRIX*)
	cp=/bin/cp
	;;
*)
	cp="/bin/cp -p"
esac



# Flush all NFS uid mappings
fsid $quiet -p -a

# Tell the Zephyr hostmanager to reset state
if [ -f /etc/athena/zhm.pid -a "${ZCLIENT}" = "true" ] ; then 
	kill -HUP `cat /etc/athena/zhm.pid`
fi

# Clean temporary areas (including temporary home directories)
case "${SYSTEM}" in
RSAIX)
	find /tmp -depth \( -type f -o -type l \) -print | xargs rm -f -
	find /tmp -depth -type d -print | xargs rmdir 1>/dev/null 2>&1
	;;

SUN4)
	cp -p /tmp/ps_data /usr/tmp/ps_data
	rm -rf /tmp/* > /dev/null 2>&1
        cp -p /usr/tmp/ps_data /tmp/ps_data
	rm -f /usr/tmp/ps_data
	;;
LINUX|NetBSD)
	rm -rf /tmp/*
#	Be very careful about erasing dotfiles in /tmp!
#	Do not lose /tmp/.X0-lock or the directory /tmp/.X11-unix
	if [ -f /tmp/.X0-lock ] ; then
	   X0lock=1; mv /tmp/.X0-lock /tmp/X0-lock
	fi
	mv /tmp/.X11-unix /tmp/X11-unix
	rm -rf /tmp/.??*
	case "$X0lock" in 1)
	  	mv /tmp/X0-lock /tmp/.X0-lock
	;; esac
	mv /tmp/X11-unix /tmp/.X11-unix
	;;
*)
	mv /tmp/.X11-unix /tmp/../.X11-unix
	rm -rf /tmp/ > /dev/null 2>&1
	mv /tmp/../.X11-unix /tmp/.X11-unix
	;;
esac

# Restore password and group files
case "${SYSTEM}" in
RSAIX)
	;;
SUN4)
        if [ -f /etc/passwd.local ] ; then
            ${cp} /etc/passwd.local /etc/ptmp && mv -f /etc/ptmp /etc/passwd
	fi
        if [ -f /etc/shadow.local ] ; then
            ${cp} /etc/shadow.local /etc/stmp && mv -f /etc/stmp /etc/shadow
	fi
        if [ -f /etc/group.local ] ; then
            ${cp} /etc/group.local /etc/gtmp && mv -f /etc/gtmp /etc/group
        fi
	;;
NetBSD)
	if [ -f /etc/master.passwd.local ] ; then
	    ${cp} /etc/master.passwd.local /etc/ptmp && pwd_mkdb -p /etc/ptmp
	fi
	if [ -f /etc/group.local ] ; then
	    ${cp} /etc/group.local /etc/gtmp && mv -f /etc/gtmp /etc/group
	fi
	;;
*)
	if [ -f /etc/passwd.local ] ; then
	    ${cp} /etc/passwd.local /etc/ptmp && mv -f /etc/ptmp /etc/passwd
	    rm -f /etc/passwd.dir /etc/passwd.pag
	fi
	if [ -f /etc/group.local ] ; then
	    ${cp} /etc/group.local /etc/gtmp && mv -f /etc/gtmp /etc/group
	fi
	;;
esac

# Reconfigure AFS state
if [ "${AFSCLIENT}" != "false" ]; then
    /etc/athena/config_afs > /dev/null &
fi

# punt any processes owned by users not in /etc/passwd
/etc/athena/cleanup -passwd

# Finally, detach all remote filesystems
detach -O -h -n   -a

# Now start activate again
/etc/athena/save_cluster_info

if [ -f /etc/athena/clusterinfo.bsh ] ; then
	. /etc/athena/clusterinfo.bsh
else
	if [ "${RVDCLIENT}" = "true" ]; then
		echo "Can't find library servers."
		exit 1
	fi
fi

if [ "${RVDCLIENT}" = "true" ]; then
	attach	$quiet -h -n -o hard  $SYSLIB
fi

# Perform an update if appropriate
if [ "${AUTOUPDATE}" = "true" -a -f /srvd/auto_update ]; then 
	/srvd/auto_update reactivate
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
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 the Student Information Processing
Board (SIPB), x3-7788, or netbsd-help@mit.edu 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

if [ -f /usr/athena/bin/access_off ]; then access_off; fi

if [ -f /etc/athena/reactivate.local ]; then
	/etc/athena/reactivate.local
fi

exit 0
