#!/bin/sh
#
# $Id: auto_install,v 1.6 1995/03/07 14:17:06 casper Exp $
#
# Autoupgrade script.  To be executed when a running system needs
# to be upgraded
#
# A system with security-mode set to none can be rebooted easily with
# ``reboot "net - install w''.
#
# A system with security-mode full or command cannot be rebooted
# this way, to work around this, the following steps are taken:
#
#		* security-mode is switched to command
#		* diag-switch? is set to true
#		- diag-device will be set to net
#		- change the diag boot args to install w
#
#	actions marked with * must be undone in the install procedure.
#
# Casper Dik (casper@fwi.uva.nl)


# The diag file that is used.
bootfile='- install w'

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

seteeprom()
{
    eeprom "$1=$2" || {
	echo "can't set EEPROM parameter '$1' to '$2'" 1>&2
	exit 1
    }
    echo "EEPROM parameter '$1' set to '$2'"
}

geteeprom()
{
    expr "`eeprom $1`" : "$1="'\(.*\)'
}

#
# We can't auto-reboot of security mode is full, switch to command in
# that case.  We can't switch to none, because we have to re-enter
# the password and we want to automate it all.
#
mode=`geteeprom security-mode`
if [ "$mode" = full ]
then
    seteeprom security-mode command
fi
#
# At this point, security-mode is either command or none.
# We can't pass a boot device and file to reboot if we have security mode
# set to command.  We use the following trick: we define a diag-device
# and diag-file to boot ``net - install'' and set diag-switch? to true.
# We undo the setting of diag-switch? in the install procedure.
# We leave the diag-file and diag-device setting for future installs.
# The EEPROM should be written as little as possible.
#
if [ $mode != none ]
then
    if [ "`geteeprom diag-device`" != net ]
    then
	seteeprom diag-device net
    fi
    if [ "`geteeprom diag-device`" != net ]
    then
	# Use alternate method.
	if [ -f /etc/hostname.le0 ]; then ether=le ; else ether=ie ; fi
	if [ "`geteeprom boot-from-diag`" != "$ether()$bootfile" ]
	then
	    seteeprom boot-from-diag "$ether()$bootfile"
	fi
    elif [ "`geteeprom diag-file`" != "$bootfile" ]
    then
	seteeprom diag-file "$bootfile"
    fi
    seteeprom diag-switch? true
    reboot
else
    # Normal reboot install.
    reboot  "net $bootfile"
fi
