#!/bin/sh
# $Id: start-update.sh,v 1.1 1998/12/30 19:53:23 danw Exp $

. /srvd/usr/athena/lib/update/update-environment

# We get three arguments: the method by which the machine was rebooted.
# (possible values are Auto, Manual, and Remote), the current workstation
# version, and the new version.
method="$1"
version="$2"
newvers="$3"

echo "Starting update"

if [ "$version" != "$newvers" ]; then
	echo "Checking for new kernel."
	upvers -a preupdate "$version" "$newvers" "$LIBDIR"
fi

. $CONFDIR/rc.conf
if [ -f $CONFVARS ]; then
	. $CONFVARS
fi

if [ "$NEWUNIX" = true ]; then
	if [ -f /netbsd.custom ]; then
		echo "Custom kernel present."
		if [ -f /netbsd.custom.update ]; then
			echo "Using new custom kernel."
			mv /netbsd.custom.update /netbsd-postupdate
		else
			if [ "$NEWOS" = true ]; then
				cat <<EOF
You are running with a custom kernel but have not provided a new
kernel for the update. This update to version $newvers will not
proceed without a new kernel. Please contact SIPB (at
netbsd-help@mit.edu or x3-7788) for information on having your
workstation updated.
EOF
				exit 1
			fi
		fi
	else
		cp /srvd/netbsd /netbsd-postupdate
	fi
	
	if [ -f /netbsd-postupdate ]; then
		ln -f /netbsd /netbsd-preupdate
		mv /usr/vice/etc/dkload/libafs.o \
			/usr/vice/etc/dkload/libafs.o-preupdate
		cp /srvd/usr/vice/etc/dkload/libafs.o /usr/vice/etc/dkload
		echo "Athena Workstation ($HOSTTYPE) Version Reboot newkernel"\
			"$method $newvers $version `date`">> "$CONFDIR/version"
		mv /netbsd-postupdate /netbsd
		echo "Kernel replaced, system will reboot in 15 seconds."
		sync
		sleep 15
		# Redirection prevents reboot from getting SIGPIPE and dying.
		exec reboot > /dev/console 2>&1
	fi
fi

# Hand off to do-update. If a new kernel was installed, it'll
# catch up with us there.
exec sh "$LIBDIR/do-update" $method $version $newvers
