#! /bin/sh
# 
# UPGRADE : script written to upgrade systems running 3.2, 3.3, 3.4,
#	    ,3.5ALPHA, 3.5BETA or 3.5BETA_EXPORT to 3.5.
#
#	    Supports
#		1) standalone with local tape drive
#		2) standalone with remote tape drive
#		3) homogeneous server with local tape drive
#		4) homogeneous server with remote tape drive
#		5) heterogeneous server with local tape drive
#		6) heterogeneous server with remote tape drive
#
#	@(#)  UPGRADE  1.11  87/11/03
#
#  	Copyright (c) 1987 by Sun Microsystems, Inc.
#

CURRENTRELEASE="3.5"
HOME=/; export HOME
PATH=/bin:/usr/bin:/etc:/usr/etc:/usr/etc/upgrade:/usr/ucb ; export PATH

OPTLIST="Networking Debugging Suntools_users Suntools_programmers \
         Suntools_source Text_processing Setup Stand_diag Fortran Usr_diag \
         Graphics Pascal Profiled Uucp System_V Man Demo Games Sunpro Vtroff"

SVLIST_stand="/.login /.cshrc /.rhosts /etc/passwd /etc/exports /etc/printcap \
              /etc/fstab /etc/hosts.equiv /etc/dumpdates /etc/gettytab \
	      /etc/hosts /etc/mtab /etc/magic /etc/rc.local /etc/rc.boot \
	      /etc/remote /etc/ttytypes /etc/ttys /usr/lib/crontab \
	      /usr/lib/sendmail.cf"

SVLIST_serve="/.login /.cshrc /.rhosts /etc/passwd /etc/exports /etc/printcap \
              /etc/fstab /etc/hosts.equiv /etc/dumpdates /etc/gettytab \
	      /etc/hosts /etc/mtab /etc/magic /etc/nd.local /etc/rc.boot \
	      /etc/rc.local /etc/remote /etc/ttytype /etc/ttys \
              /usr/hosts/MAKEHOSTS /private/usr/lib/crontab \
              /private/usr/lib/sendmail.cf"

SVLIST_yp="/etc/ethers /etc/netgroup /etc/services /etc/protocols /etc/group \
           /etc/networks /etc/rpc /etc/servers /usr/lib/aliases \
	   /usr/etc/yp/domain"

SVLIST_ypss="/usr/etc/yp/domain"

SVCLNT="/.login /.cshrc /.rhosts /etc/passwd /etc/printcap /etc/fstab \
        /etc/gettytab /etc/hosts.equiv /etc/magic /etc/mtab /etc/hosts \
	/etc/rc.local /etc/rc.boot /etc/remote /private/usr/lib/crontab \
	/private/usr/lib/sendmail.cf"

CLIENTLIST10=""
CLIENTLIST20=""
EXTRACTLIST10=""
EXTRACTLIST20=""

NDLOCAL=/a/etc/nd.local

CMDNAME=$0
#
# Check architecture type
#
if [ -f /bin/sun2 ]; then
        /bin/sun2
        if [ "$?" = 0 ]; then
                machtype="sun2"
        fi
fi
if [ -f /bin/sun3 ]; then
        /bin/sun3
        if [ "$?" = 0 ]; then
                machtype="sun3"
        fi
fi
case  "$machtype" in
        "sun2" )
                ARCHTYPE="MC68010" ;;
        "sun3" )
                ARCHTYPE="MC68020" ;;
        * )
                echo;echo "${CMDNAME}: invalid architecture type."
		exit 1 ;;
esac
#
# Specify system type : standalone or server
#
while true; do
	echo
	echo -n "Enter system type ? [standalone | server]: "
	read MACHINE;
	case "$MACHINE" in
		"standalone" )
			break ;;
		"server" )
			while true; do
        			echo
        			echo -n "Enter server type ? [homo | heter]: "
        			read SERVERTYPE;
        			case "$SERVERTYPE" in
                			"homo" )
                        			break ;;
                			"heter" )
						case "$ARCHTYPE" in
							"MC68010" )
								OTHERARCH="MC68020" ;;
							"MC68020" ) 
                                                                OTHERARCH="MC68010" ;;
						esac
                        			break ;;
                			* )
                        			echo "${CMDNAME}: invalid server type \"${SERVERTYPE}\"." ;;
        			esac
			done
			break ;;
		* )
			echo "${CMDNAME}: invalid machine type \"${MACHINE}\"." ;;
	esac
done
#
# Specify tape drive type : local or remote
# If remote, specify tape host and ethernet type : ec, ie or le
#
while true; do
        echo
        echo -n "Enter tape drive type ? [local | remote]: "
        read DRIVE;
        case "$DRIVE" in
                "local" )
                        break ;;
                "remote" )
                        echo;echo -n "Enter host of remote drive ? "
                        read TAPEHOST;
                        while true; do
                                echo
                                echo -n "Enter ethernet type of this system ? [ec | ie | le] :"
                                read ETHER;
                                case "$ETHER" in
                                        "ec" | "ie" | "le" )
                                                break ;;
                                        * )
                                                echo "${CMDNAME}: invalid ether type \"${ETHER}\"." ;;
                                esac
                        done
			while true; do
				echo
				echo -n "Using ${ETHER}0 or ${ETHER}1 ? [ 0 | 1 ] :"
				read ETHNO
				case "$ETHNO" in
					"0" | "1" )
						break ;;
					* )
						echo "${CMDNAME}: invalid ether number, \"${ETHNO}\"." ;;
				esac
			done
			ETH=${ETHER}${ETHNO}
			echo "Ethernet connects to $ETH"
                        REMOTE="rsh $TAPEHOST -n"
                        break ;;
                * )
                        echo "${CMDNAME}: invalid tape drive type \"${DRIVE}\"." ;;
        esac
done
#
# Specify tape type : ar, st, mt or xt
#
while true; do 
	echo
	echo -n "Enter tape type ? [ar | st | mt | xt]: "
	read TAPE;
	case "$TAPE" in
        	"ar" | "st" )
			Sys="2,2"
			Networking="2,3"
        		Debugging="2,4"
			User="2,5"
        		Suntools_users="3,2"
        		Suntools_programmers="3,3"
        		Suntools_source="3,4"
        		Text_processing="3,5"
        		Setup="3,6"
        		Stand_diag="3,7"
        		Fortran="3,8"
        		Usr_diag="3,9"
        		Graphics="3,10"
        		Pascal="3,11"
        		Profiled="3,12"
        		Uucp="3,13"
        		System_V="4,2"
        		Man="4,3"
        		Demo="4,4"
        		Games="4,5"
			Sunpro="4,6"
        		Vtroff="5,2"
			BS=126
			cd /dev
			/dev/MAKEDEV ${TAPE}0 2> /dev/null
			break ;;
		"mt" | "xt" )
			Sys="1,9"
			Networking="1,10"
        		Debugging="1,11"
			User="1,12"
        		Suntools_users="2,2"
        		Suntools_programmers="2,3"
        		Suntools_source="2,4"
        		Text_processing="2,5"
        		Setup="2,6"
        		Stand_diag="2,7"
        		Fortran="2,8"
        		Usr_diag="2,9"
        		Graphics="2,10"
        		Pascal="2,11"
        		Profiled="2,12"
        		Uucp="2,13"
        		System_V="3,2"
        		Man="3,3"
        		Demo="3,4"
        		Games="3,5"
        		Sunpro="3,6"
			Vtroff="3,7"
			BS=20 
			cd /dev
			rm -f *mt*
			/dev/MAKEDEV ${TAPE}0 2> /dev/null
			TAPE="mt"
			break ;;
        	* )
                	echo "${CMDNAME}: invalid tape type \"${TAPE}\"." ;;
	esac
done
#
# Specify yp type : master , slave, client or none
#
while true; do
	echo
	echo -n "Enter yp type for machine? [master | slave | client | none]: "
	read YPTYPE;
	case "$YPTYPE" in
        	"master" | "slave" | "client" | "none" )
                	break ;;
        	* )
                	echo "${CMDNAME}: invalid yp type \"${YPTYPE}\"." ;;
	esac
done

#
# Determine the files to be saved
#
if [ $MACHINE = "standalone" ] ; then
	SVLIST=$SVLIST_stand
else
	SVLIST=$SVLIST_serve
fi
case "$YPTYPE" in
	"master" | "none" )
		SVLIST="$SVLIST $SVLIST_yp" ;;
	"slave" )
		SVLIST="$SVLIST $SVLIST_ypss" ;;
	* )
		;;
esac

#
# Gather information about the file systems
#
while true; do
        echo
        echo -n "Enter root disk partition for ${ARCHTYPE} architecture (e.g. xy0a) ? "
        read ROOT;
        case "$ROOT" in
                "" )
                        ;;
		"ip0a" )
			if [ "$ARCHTYPE" = "MC68010" ]; then
				break
			else
				echo "Interphase disk controller(ip) can only be used on Sun-2 machines."
			fi ;;
                "xy0a" | "sd0a" )
                        break ;;
		* )
			;;
        esac
done
cd /dev
ROOT_DISK=`expr ${ROOT} : '\([a-z][a-z][0-9]\)[a-z]'`
/dev/MAKEDEV ${ROOT_DISK} 2> /dev/null
echo
fsck -p /dev/r${ROOT}
mkdir /a > /dev/null 2>&1
if mount /dev/${ROOT} /a
then
        :
else
        echo "${CMDNAME}: can't mount /dev/${ROOT}, get help!"
        exit 1
fi

grep "^/dev/*" /a/etc/fstab > /tmp/FSTAB
if [ "$MACHINE" = "standalone" ]; then
	cat /tmp/FSTAB|awk '$2 == "/usr" && $3 == "4.2" { print $1 }' > /tmp/user
	TMP=`cat /tmp/user`
	if [ "$TMP" = "" ]; then
		echo "${CMDNAME}: can't find /usr in fstab !"
        	exit 1
	fi
elif [ "$ARCHTYPE" = "MC68010" -a "$MACHINE" = "server" ]; then
	cat /tmp/FSTAB|awk '$2 == "/usr.MC68010" && $3 == "4.2" { print $1 }' > /tmp/user
	TMP=`cat /tmp/user`
	if [ "$TMP" = "" ]; then
		cat /tmp/FSTAB|awk '$2 == "/usr" && $3 == "4.2" { print $1 }' > /tmp/user
		TMP=`cat /tmp/user`
		if [ "$TMP" = "" ]; then
                	echo "${CMDNAME}: can't find /usr.${ARCHTYPE} in fstab !"
                	exit 1 
        	fi
	fi
elif [ "$ARCHTYPE" = "MC68020" -a "$MACHINE" = "server" ]; then
	cat /tmp/FSTAB|awk '$2 == "/usr.MC68020" && $3 == "4.2" { print $1 }' > /tmp/user
	TMP=`cat /tmp/user`
	if [ "$TMP" = "" ]; then
                cat /tmp/FSTAB|awk '$2 == "/usr" && $3 == "4.2" { print $1 }' > /tmp/user
		TMP=`cat /tmp/user`
		if [ "$TMP" = "" ]; then 
                        echo "${CMDNAME}: can't find /usr.${ARCHTYPE} in fstab !"
                        exit 1  
                fi
	fi
fi
USER=`expr ${TMP} : '/dev/\([a-z][a-z][0-9][a-z]\)' '|' ${TMP}`
rm -f /tmp/user 2>/dev/null

if [ "$MACHINE" = "server" ]; then
	if [ "$ARCHTYPE" = "MC68010" ]; then
        	cat /tmp/FSTAB|awk '$2 == "/pub.MC68010" && $3 == "4.2" { print $1 }' > /tmp/pub
		TMP=`cat /tmp/pub`
                if [ "$TMP" = "" ]; then
			cat /tmp/FSTAB|awk '$2 == "/pub" && $3 == "4.2" { print $1 }' > /tmp/pub
			TMP=`cat /tmp/pub`
                	if [ "$TMP" = "" ]; then
                        	echo "${CMDNAME}: can't find /pub.${ARCHTYPE} in fstab !"
                        	exit 1
			fi
                fi
	elif [ "$ARCHTYPE" = "MC68020" ]; then
        	cat /tmp/FSTAB|awk '$2 == "/pub.MC68020" && $3 == "4.2" { print $1 }' > /tmp/pub
		TMP=`cat /tmp/pub`
                if [ "$TMP" = "" ]; then
                        cat /tmp/FSTAB|awk '$2 == "/pub" && $3 == "4.2" { print $1 }' > /tmp/pub
                        TMP=`cat /tmp/pub`
                        if [ "$TMP" = "" ]; then
                                echo "${CMDNAME}: can't find /pub.${ARCHTYPE} in fstab !"
                                exit 1
                        fi
                fi
	fi
	PUB=`expr ${TMP} : '/dev/\([a-z][a-z][0-9][a-z]\)' '|' ${TMP}`
	rm -f /tmp/pub 2>/dev/null

	if [ "$SERVERTYPE" = "heter" ]; then
		if [ "$ARCHTYPE" = "MC68010" ]; then
			cat /tmp/FSTAB|awk '$2 == "/usr.MC68020" && $3 == "4.2" { print $1 }' > /tmp/otheruser
			TMP=`cat /tmp/otheruser`
			if [ "$TMP" = "" ]; then
                                echo "${CMDNAME}: can't find /usr.MC68020 in fstab !"
                                exit 1
                        fi
			cat /tmp/FSTAB|awk '$2 == "/pub.MC68020" && $3 == "4.2" { print $1 }' > /tmp/otherpub
			TMP=`cat /tmp/otherpub`
			if [ "$TMP" = "" ]; then 
                                echo "${CMDNAME}: can't find /pub.MC68020 in fstab !"
                                exit 1 
                        fi
        	elif [ "$ARCHTYPE" = "MC68020" ]; then    
			cat /tmp/FSTAB|awk '$2 == "/usr.MC68010" && $3 == "4.2" { print $1 }' > /tmp/otheruser
			TMP=`cat /tmp/otheruser`
			if [ "$TMP" = "" ]; then
                                echo "${CMDNAME}: can't find /usr.MC68010 in fstab !"                              
                                exit 1
                        fi
                	cat /tmp/FSTAB|awk '$2 == "/pub.MC68010" && $3 == "4.2" { print $1 }' > /tmp/otherpub
			TMP=`cat /tmp/otherpub` 
                        if [ "$TMP" = "" ]; then  
                                echo "${CMDNAME}: can't find /pub.MC68010 in fstab !" 
                                exit 1  
                        fi
        	fi
		TMP=`cat /tmp/otheruser`
		OTHERUSER=`expr ${TMP} : '/dev/\([a-z][a-z][0-9][a-z]\)' '|' ${TMP}`
		TMP=`cat /tmp/otherpub`
        	OTHERPUB=`expr ${TMP} : '/dev/\([a-z][a-z][0-9][a-z]\)' '|' ${TMP}`
        	rm -f /tmp/otheruser /tmp/otherpub 2>/dev/null
	fi
fi
rm -f /tmp/FSTAB
#
# Make devices
#
cd /dev
USER_DISK=`expr ${USER} : '\([a-z][a-z][0-9]\)[a-z]'`
if [ "$ROOT_DISK" != "$USER_DISK" ]; then
	/dev/MAKEDEV ${USER_DISK} 2> /dev/null
fi
if [ "$MACHINE" = "server" ]; then 
	PUB_DISK=`expr ${PUB} : '\([a-z][a-z][0-9]\)[a-z]'`
	if [ "$ROOT_DISK" != "$PUB_DISK" -a "$USER_DISK" != "$PUB_DISK" ]; then
		/dev/MAKEDEV ${PUB_DISK} 2> /dev/null
	fi
	if [ "$SERVERTYPE" = "heter" ]; then
		OTHERUSER_DISK=`expr ${OTHERUSER} : '\([a-z][a-z][0-9]\)[a-z]'`
		if [ "$ROOT_DISK" != "$OTHERUSER_DISK" \
		  -a "$USER_DISK" != "$OTHERUSER_DISK" \
		  -a "$PUB_DISK"  != "$OTHERUSER_DISK" ]; then
			/dev/MAKEDEV ${OTHERUSER_DISK} 2> /dev/null
		fi
        	OTHERPUB_DISK=`expr ${OTHERPUB} : '\([a-z][a-z][0-9]\)[a-z]'`
		if [ "$ROOT_DISK" != "$OTHERPUB_DISK" \
                  -a "$USER_DISK" != "$OTHERPUB_DISK" \
                  -a "$PUB_DISK"  != "$OTHERPUB_DISK" \
	  	  -a "$OTHERUSER_DISK" != "$OTHERPUB_DISK" ]; then 
               		/dev/MAKEDEV ${OTHERPUB_DISK} 2> /dev/null
        	fi
        fi
fi
#
# Check and mount file systems
#
fsck -p /dev/r${USER}
if mount /dev/${USER} /a/usr
then
	:
else
	echo "${CMDNAME}: can't mount /dev/${USER}, get help!"
	exit 1
fi
if [ "$MACHINE" = "server" ]; then
       	fsck -p /dev/r${PUB}
	if mount /dev/${PUB} /a/pub
	then
       		:
	else
     		echo "${CMDNAME}: can't mount /dev/${PUB}, get help!"
     		exit 1
	fi
	if [ "$SERVERTYPE" = "heter" ]; then
		fsck -p /dev/r${OTHERUSER}
		if mount /dev/${OTHERUSER} /a/usr.${OTHERARCH}
		then
        		:
		else   
        		echo "${CMDNAME}: can't mount /dev/${OTHERUSER}, get help!"
        		exit 1
		fi
       		fsck -p /dev/r${OTHERPUB}
       		if mount /dev/${OTHERPUB} /a/pub.${OTHERARCH}
       		then
               		:
       		else
               		echo "${CMDNAME}: can't mount /dev/${OTHERPUB}, get help!"
               		exit 1
		fi
	fi
fi

#
# Check for usr.ARCHTYPE on servers
#
if [ $MACHINE = "server" ] ; then
	if [ ! -d /a/usr.${ARCHTYPE} ] ; then
		cd /a
		ln -s usr usr.${ARCHTYPE} > /dev/null
	fi
fi

#
# Determine current release : 3.2, 3.3, 3.4, 3.5ALPHA, 3.5BETA or 
#      3.5BETA_EXPORT
set `grep "UNIX" /a/etc/motd` >/dev/null
RELEASE=$5
if [ "$RELEASE" != "3.2" -a "$RELEASE" != "3.2EXPORT" -a \
     "$RELEASE" != "3.3" -a "$RELEASE" != "3.3EXPORT" -a \
     "$RELEASE" != "3.4" -a "$RELEASE" != "3.4EXPORT" -a \
     "$RELEASE" != "3.5ALPHA" -a "$RELEASE" != "3.5BETA" -a \
     "$RELEASE" != "3.5BETA_EXPORT" -a "$RELEASE" != "3.5" -a \
     "$RELEASE" != "3.5EXPORT" ]; then
	RELEASE=`cat /a/usr/sys/conf/RELEASE`
fi
if [ "$RELEASE" != "3.2" -a "$RELEASE" != "3.2EXPORT" -a \
     "$RELEASE" != "3.3" -a "$RELEASE" != "3.3EXPORT" -a \
     "$RELEASE" != "3.4" -a "$RELEASE" != "3.4EXPORT" -a \
     "$RELEASE" != "3.5ALPHA" -a "$RELEASE" != "3.5BETA" -a \
     "$RELEASE" != "3.5BETA_EXPORT" -a "$RELEASE" != "3.5" -a \
     "$RELEASE" != "3.5EXPORT" ]; then
	echo "${CMDNAME}: can't upgrade \"${RELEASE}\"."
	echo "${CMDNAME}: upgrade procedure terminates."
	exit 1
fi

#
# Determine and Select optional software lists
#
rm -f /tmp/EXTRACTLIST10 /tmp/EXTRACTLIST20
if [ "$SERVERTYPE" = "homo" -o "$MACHINE" = "standalone" ]; then
	opt_software ${ARCHTYPE}
elif [ "$SERVERTYPE" = "heter" ]; then
	opt_software ${ARCHTYPE} ${OTHERARCH}
fi
if [ -f /tmp/EXTRACTLIST10 ]; then
        for OPT in `cat /tmp/EXTRACTLIST10`; do
                EXTRACTLIST10="${EXTRACTLIST10} ${OPT}"
        done
        rm -f /tmp/EXTRACTLIST10
fi
if [ -f /tmp/EXTRACTLIST20 ]; then
        for OPT in `cat /tmp/EXTRACTLIST20`; do
                EXTRACTLIST20="${EXTRACTLIST20} ${OPT}"
        done
        rm -f /tmp/EXTRACTLIST20
fi
#
# Preliminary work before upgrade starts
#
HOST=`awk -F= '/hostname=/ { print $2 }' < /a/etc/rc.boot`

if [ -f /a/etc/hosts ]; then
        cp /a/etc/hosts /etc/hosts
        sync;sync
fi

if [ "$DRIVE" = "remote" ]; then
	grep $TAPEHOST /a/etc/hosts > /dev/null 2>&1
	if [ "$?" != 0 ]; then
		echo "${CMDNAME}: can't reach tapehost \"${TAPEHOST}\" !!"
		exit 1
	fi
	/etc/ifconfig ${ETH} ${HOST} -trailers up
fi

if [ "$MACHINE" = "server" ]; then
        if [ -f /a/etc/ethers ]; then
                cp /a/etc/ethers /etc/ethers
                sync;sync
        fi
	#
        # Create CLIENTLIST from /etc/nd.local
        # Looks ugly but handles all possible cases
        # nd.local format
        #   user hostname hisunit mydev myoff mysize mylunit maxpkts
        #
        endsym=$
        ind=0
	CLIENTLIST=""
        while true; do
		grep " $ind $endsym" $NDLOCAL > /dev/null 2>&1
                case "$?" in
                   0)
                      set `grep " $ind $endsym" $NDLOCAL` ;;
                   1)
                      grep " $ind    $endsym" $NDLOCAL > /dev/null 2>&1
                      case "$?" in
                         0)
                            set `grep " $ind $endsym" $NDLOCAL` ;;
                         1)
                            grep " $ind$endsym" $NDLOCAL > /dev/null 2>&1
                            case "$?" in
                               0)
                                  set `grep " $ind$endsym" $NDLOCAL` ;;
                               1)
                                  grep " $ind [0-9]$endsym" $NDLOCAL > /dev/null 2>&1
                                  case "$?" in
                                     0)
                                        set `grep " $ind [0-9]$endsym" $NDLOCAL` ;;
                                     1)
		   			grep " $ind  [0-9]$endsym" $NDLOCAL > /dev/null 2>&1
                                        case "$?" in
                                           0)
                                              set `grep " $ind       [0-9]$endsym" $NDLOCAL` ;;
                                           1)
                                              break ;;
                                        esac ;;
                                  esac ;;
                            esac ;;
                      esac ;;
                esac
		if [ "$1" = "version" ]  ; then
			shift 2
		fi
                CLIENT=$2
                case "$1" in
                     "#user" )
                             ;;
                     "user"  )
			     mkdir /${CLIENT} 2> /dev/null
                             CLIENTLIST="${CLIENTLIST} ${CLIENT}" ;;
                     * )
                             ;;
                esac
                ind=`expr ${ind} + 1`
        done
	for CLIENT in $CLIENTLIST
        do
                set `grep "user $CLIENT 0" $NDLOCAL | sed s,\/," ",g`
                ndl=ndl${8}
                cd /dev
                /dev/MAKEDEV ${ndl} 2> /dev/null
        done
	/etc/nd < $NDLOCAL > /dev/null 2>&1
	if [ "$SERVERTYPE" = "homo" ]; then
		if [ "$ARCHTYPE" = "MC68010" ]; then
                        CLIENTLIST10=${CLIENTLIST}
                elif [ "$ARCHTYPE" = "MC68020" ]; then
                        CLIENTLIST20=${CLIENTLIST}
                fi
	elif [ "$SERVERTYPE" = "heter" ]; then
		for HOSTNAME in ${CLIENTLIST}; do
                        set `grep "user $HOSTNAME 0" $NDLOCAL | sed s,\/," ",g`
                        DISK=ndl${8}
			fsck -p /dev/${DISK} > /dev/null
                        if mount /dev/${DISK} /${HOSTNAME}; then
                                cd /${HOSTNAME}
				grep "^$HOST" /${HOSTNAME}/etc/fstab > /tmp/CLIENTFSTAB
				set `grep "/usr nfs" /tmp/CLIENTFSTAB | sed s,\/," ",g`
				user_file_system=$2
				case "$user_file_system" in
					"usr.MC68010" | "usr.MC68020" )
						
						TYPE=`expr $2 : 'usr.\(.*\)' '|' $2`
						if [ "$TYPE" = "MC68010" ]; then
							CLIENTLIST10="${CLIENTLIST10} ${HOSTNAME}"
						elif [ "$TYPE" = "MC68020" ]; then
                                        		CLIENTLIST20="${CLIENTLIST20} ${HOSTNAME}"
						fi ;;
					"usr" )
						if [ "$ARCHTYPE" = "MC68010" ]; then
							CLIENTLIST10="${CLIENTLIST10} ${HOSTNAME}"
                                                elif [ "$ARCHTYPE" = "MC68020" ]; then
                                                        CLIENTLIST20="${CLIENTLIST20} ${HOSTNAME}"
                                                fi ;;
				esac
                                /bin/sync; cd /
                                umount /${HOSTNAME} 2>&1
                        else
                                echo "${CMDNAME}: can't reach client \"${HOSTNAME}\" disk partition /dev/${DISK}."
                                echo "${CMDNAME}: upgrade procedure aborting."
                                exit 1
        
                        fi
			rm -f /tmp/CLIENTFSTAB
		done
	fi
fi
#
# Prompt user attention last time before starting to upgrade
#
while true; do
        echo;echo -n "Are you ready to start the upgrade ? [y/n] :"
        read READY;
        case "${READY}" in
                y | yes )
                        break ;;
                n | no )
                        echo;echo "Upgrade procedure terminates."
                        exit 1 ;;
                * )
                        echo;echo "Please answer only yes or no."
        esac
done

#
# Upgrade starts
#
if [ "$MACHINE" = "server" -a "$SERVERTYPE" = "heter" ]; then
	ARCHLIST="${ARCHTYPE} ${OTHERARCH}"
else
	ARCHLIST="${ARCHTYPE}"
fi	

for CURRENTARCH in ${ARCHLIST}; do
	echo
	echo "Beginning ${RELEASE} to ${CURRENTRELEASE} upgrade for the ${CURRENTARCH} architecture."
	if [ "$CURRENTARCH" = "$ARCHTYPE" ]; then 
		#
		# save the admin files
		#
		for i in ${SVLIST} ; do
			if [ -s "/a${i}" ] ; then
				mv "/a${i}" "/a${i}.OLD"
			fi
		done
		/bin/sync; /bin/sync
        	#
        	# extract root from release tape
        	#
        	echo
        	echo "Changing directory to \"/\". "
        	cd /a
		#
		# check the inserted tape for machine type and tape #
		#
		verify_tapevol_arch ${CURRENTARCH} 1 /dev/nr${TAPE}0 ${TAPEHOST}
		extracting /dev/nr${TAPE}0 6 ${BS} "root" ${TAPEHOST}
		if [ "$MACHINE" = "server" ]; then
			echo
			echo "Changing directory to \"/pub\"."
			cd /a/pub
		fi
	elif [ "$CURRENTARCH" = "$OTHERARCH" ]; then
		if [ "$MACHINE" = "server" ]; then
                        echo
                        echo "Changing directory to \"/pub.${OTHERARCH}\"."
                        cd /a/pub.${OTHERARCH}
                fi
	fi
	#
        # extract pub from release tape
        #
	verify_tapevol_arch ${CURRENTARCH} 1 /dev/nr${TAPE}0 ${TAPEHOST}
	extracting /dev/nr${TAPE}0 7 ${BS} "pub" ${TAPEHOST}
	
	if [ "$MACHINE" = "server" ]; then
		if [ "$CURRENTARCH" = "$ARCHTYPE" ]; then
			#
			# For safty reason upgrade boot under "pub"
			#
			if [ -f ./pub.boot ]; then
				mv ./pub.boot ./boot > /dev/null 2>&1
			fi
			#
			# must copy boot, vmunix and stand from "pub" back to
			# root and move tftpboot back to root, so the server
			# can use them too.
			#
			cp boot vmunix /a
			cp -r stand /a
			mkdir /a/tftpboot > /dev/null 2>&1
			cd tftpboot;tar cf - .|(cd /a/tftpboot;tar xpf -) 2>&1
			cd /a/pub
			rm -fr tftpboot > /dev/null 2>&1
		elif [ "$CURRENTARCH" = "$OTHERARCH" ]; then
                        if [ -f ./pub.boot ]; then
                                mv ./pub.boot ./boot > /dev/null 2>&1
                        fi
			if [ -f /a/tftpboot/in.tftpd ]; then
				mv /a/tftpboot/in.tftpd /a/tftpboot/in.tftpd.save
			fi
			cd tftpboot;tar cf - .|(cd /a/tftpboot;tar xpf -) 2>&1
			if [ -f /a/tftpboot/in.tftpd.save ]; then
                                mv /a/tftpboot/in.tftpd.save /a/tftpboot/in.tftpd
                        fi
                        cd /a/pub.${OTHERARCH}
                        rm -rf tftpboot > /dev/null 2>&1
		fi
		#
		# Upgrade client partitions 
		#
        	echo
        	echo "Beginning ${RELEASE} to ${CURRENTRELEASE} upgrade on ${CURRENTARCH} diskless clients."
		#
		# extract client files from the release tape
		#
                # Because mt does not always skip right number of files,
                # we have to rewind to be safe.
                #
               	verify_tapevol_arch ${CURRENTARCH} 1 /dev/nr${TAPE}0 ${TAPEHOST}
		${REMOTE} mt -f /dev/nr${TAPE}0 fsf 8
		if [ "$CURRENTARCH" = "$ARCHTYPE" ]; then
			cd /a/usr
		else
			cd /a/usr.${CURRENTARCH}
		fi
		if [ "$DRIVE" = "local" ]; then
			dd if=/dev/nr${TAPE}0 of=client_image bs=${BS}b > /dev/null 2>&1
		elif [ "$DRIVE" = "remote" ]; then
       			${REMOTE} dd if=/dev/nr${TAPE}0 ibs=${BS}b 2>/dev/null | dd of=client_image obs=${BS}b 2>/dev/null
		fi
	
		CLIENTLIST=""
		if [ "$CURRENTARCH" = "MC68010" ]; then
			CLIENTLIST=${CLIENTLIST10}
		elif [ "$CURRENTARCH" = "MC68020" ]; then 
                	CLIENTLIST=${CLIENTLIST20}
		fi
        	for HOSTNAME in ${CLIENTLIST}; do
			set `grep "user $HOSTNAME 0" ${NDLOCAL}.OLD | sed s,\/," ",g`
                	DISK=ndl${8}
                	if mount /dev/${DISK} /${HOSTNAME}; then
	
				#
				#  Save client admin files
				#
				for i in ${SVCLNT} ; do
					if [ -s "/${HOSTNAME}${i}" ] ; then
						mv "/${HOSTNAME}${i}" "/${HOSTNAME}${i}.OLD"
					fi
				done
				cd /${HOSTNAME}
                        	echo
                        	echo "Beginning ${RELEASE} to ${CURRENTRELEASE} upgrade on client ${HOSTNAME}."
				if [ "$CURRENTARCH" = "$ARCHTYPE" ]; then
					tar xvpfb /a/usr/client_image ${BS}
				else 
					tar xvpfb /a/usr.${CURRENTARCH}/client_image ${BS}
				fi
				#
				# restore client admin files
				#	
				for i in ${SVCLNT} ; do
					if [ -s "/${HOSTNAME}${i}.OLD" ] ; then
						mv "/${HOSTNAME}${i}.OLD" "/${HOSTNAME}${i}"
				fi
				done
				if [ "$YPTYPE" = "none" ]; then
					if [ -f /${HOSTNAME}/etc/ypbind ]; then
						mv /${HOSTNAME}/etc/ypbind /${HOSTNAME}/etc/ypbind-
					fi
				fi
				/bin/sync; /bin/sync
				cd / 
	
                        	umount /${HOSTNAME} 2>&1
				fsck -p /dev/r${DISK} > /dev/null

                        	echo
                        	echo "Completed ${RELEASE} to ${CURRENTRELEASE} upgrade on client ${HOSTNAME}."
	
                	else
	
                        	echo "${CMDNAME}: can't mount client \"${HOSTNAME}\" disk partition /dev/${DISK}."
				rm -rf /${HOSTNAME}  > /dev/null 2>&1
				cd /usr/etc/upgrade
				#
				# restore admin files
				#
				for i in ${SVLIST} ; do
					if [ -s "/a${i}.OLD" ] ; then
						mv "/a${i}.OLD" "/a${i}"
					fi
				done
				/bin/sync; /bin/sync
                        	echo "${CMDNAME}: upgrade procedure aborting."
                        	exit 1
	
                	fi
			rm -rf /${HOSTNAME} > /dev/null 2>&1
		done
		if [ "$CURRENTARCH" = "$ARCHTYPE" ]; then
			rm -f /a/usr/client_image
                else
			rm -f /a/usr.${CURRENTARCH}/client_image
                fi
	fi
	#
	# remove old files from /usr
	#
	if [ "$CURRENTARCH" = "$ARCHTYPE" ]; then
		echo
		echo "Changing directory to \"/usr\"."
		cd /a/usr
	elif [ "$CURRENTARCH" = "$OTHERARCH" ]; then
		echo
		echo "Changing directory to \"/usr.${OTHERARCH}\"."
                cd /a/usr.${OTHERARCH}
	fi

	#
	# extract sys and usr from release tape 
	#
	if [ "$TAPE" = "st" -o "$TAPE" = "ar" ]; then
		# 
                # Because mt does not always skip right number of files,
                # we have to rewind to be safe.
                #
		verify_tapevol_arch ${CURRENTARCH} 2 /dev/nr${TAPE}0 ${TAPEHOST}
        	extracting /dev/nr${TAPE}0 2 ${BS} "sys" ${TAPEHOST}
		verify_tapevol_arch ${CURRENTARCH} 2 /dev/nr${TAPE}0 ${TAPEHOST}
        	extracting /dev/nr${TAPE}0 5 ${BS} "user" ${TAPEHOST}
	elif [ "$TAPE" = "mt" -o "$TAPE" = "xt" ]; then 
		#
                # Because mt does not always skip right number of files,
                # we have to rewind to be safe.
                #
		verify_tapevol_arch ${CURRENTARCH} 1 /dev/nr${TAPE}0 ${TAPEHOST}
		extracting /dev/nr${TAPE}0 9 ${BS} "sys" ${TAPEHOST}
		verify_tapevol_arch ${CURRENTARCH} 1 /dev/nr${TAPE}0 ${TAPEHOST}
		extracting /dev/nr${TAPE}0 12 ${BS} "user" ${TAPEHOST}
	fi
	#
	# extract optional software from release tape
	#
	if [ "$CURRENTARCH" = "MC68010" ]; then
		EXTRACTLIST=${EXTRACTLIST10}
	elif [ "$CURRENTARCH" = "MC68020" ]; then
		EXTRACTLIST=${EXTRACTLIST20}
	fi
	newf_skip=0
	newtape_n=0
	for OPT in ${EXTRACTLIST}; do
        	INFO=`eval echo '$'${OPT}`
        	newtape_n=`echo ${INFO} | awk '{FS=","; print $1}'`
        	newf_skip=`echo ${INFO} | awk '{FS=","; print $2}'`
		#
                # Because mt does not always skip right number of files,
                # we have to rewind to be safe.
                #
               	verify_tapevol_arch ${CURRENTARCH} ${newtape_n} /dev/nr${TAPE}0 ${TAPEHOST}
	        extracting /dev/nr${TAPE}0 ${newf_skip} ${BS} ${OPT} ${TAPEHOST}
		if [ "$OPT" = "Stand_diag" ]; then
			if [ "$CURRENTARCH" = "$ARCHTYPE" ]; then
				cp ./stand/* /a/stand
				if [ "$MACHINE" = "server" ]; then
					cp ./stand/* /a/pub/stand
				fi
        		elif [ "$CURRENTARCH" = "$OTHERARCH" ]; then
				cp ./stand/* /a/pub.${CURRENTARCH}/stand
        		fi
		fi
	done
done
#
# restore saved administrative files back to the system
#
for i in ${SVLIST} ; do
	if [ -s "/a${i}.OLD" ] ; then
		mv -f "/a${i}.OLD" "/a${i}"
	fi
done
/bin/sync; /bin/sync
if [ "$MACHINE" = "standalone" ]; then
 	/a/etc/chown 1 /a/usr/spool/at 2>&1
	rm -rf /a/usr/etc/upgrade
elif [ "$MACHINE" = "server" ]; then 
        /a/etc/chown 1 /a/private/usr/spool/at 2>&1
	rm -rf /a/usr/etc/upgrade
	if [ "$SERVERTYPE" = "heter" ]; then
		rm -rf /a/usr.${OTHERARCH}/etc/upgrade
	fi
fi
if [ "$YPTYPE" = "none" ]; then
	if [ -f /a/etc/ypbind ]; then
		mv /a/etc/ypbind /a/etc/ypbind-
	fi
fi

#
# Unmount files systems
#
echo
echo "Checking filesystems:"

cd /
umount /dev/${USER} 2>&1
fsck -p /dev/r${USER}
if [ "$MACHINE" = "server" ]; then
	umount /dev/${PUB} 2>&1
	fsck -p /dev/r${PUB}
	if [ "$SERVERTYPE" = "heter" ]; then
        	umount /dev/${OTHERUSER} 2>&1
		fsck -p /dev/r${OTHERUSER}
        	umount /dev/${OTHERPUB} 2>&1
		fsck -p /dev/r${OTHERPUB}
	fi
fi
umount /dev/${ROOT} 2>&1
#
# Check file systems
#
fsck -p /dev/r${USER} > /dev/null
if [ "$MACHINE" = "server" ]; then
	fsck -p /dev/r${PUB}
	if [ "$SERVERTYPE" = "heter" ]; then 
                fsck -p /dev/r${OTHERUSER} > /dev/null
                fsck -p /dev/r${OTHERPUB} > /dev/null
        fi
fi
fsck -p /dev/${ROOT}
${REMOTE} mt -f /dev/nr${TAPE}0 rew
echo
echo "${RELEASE} to ${CURRENTRELEASE} upgrade completed."
echo "Reboot your system and configure a kernel for your system."
exit 0
