#!/bin/sh
#
# @(#)$Id: fore_upgrade,v 1.35 1995/05/17 20:20:56 bhargava Exp $
#

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

oldversion=`cat /atm/release/fore/version`

cd /tmp

if tar xf distribution ./fore/version ; then
	version=`cat ./fore/version`
else
	echo "Unable to determine version of upgrade ... aborting."
	echo "Please contact Fore Systems' technical support for assistance."
	exit 1
fi


if mount -o remount,rw /dev/sd0a / ; then
	echo "Installing version $version of the ForeThought switch control software."
else
	echo "I was unable to remount the root disk.  Upgrade aborted."
	echo "Please contact FORE Systems technical support for assistance"
	exit 1
fi

# Install the rc.ip file.  This file contains configuration
# information for netmask and broadcast addresses.  It should not be
# installed if it already exists.  This prevents the alteration of a
# previous configuration

if  tar xf distribution ./rc.ip ; then
	if [ ! -f /atm/etc/rc.ip ]; then
        cp rc.ip /atm/etc/rc.ip
	fi
else
	echo "The upgrade distribution is missing the rc.ip file, but"
	echo "I will try to continue with the upgrade anyway."
	echo "Please contact FORE Systems technical support for assistance."
fi


# Install the new gettytab and ttytab files.  The ttytab file goes
# in /atm/etc and a link is made from /etc.  This is done so that
# asxshell can edit the file to change the speed of the serial port.
# gettytab is modified to set the serial line to N,8,1.

if tar xf distribution ./gettytab ./ttytab  ; then
	cp ./gettytab /etc/gettytab
	cp ./ttytab /atm/etc/ttytab
	rm /etc/ttytab
	ln -s /atm/etc/ttytab /etc/ttytab
else
	echo "The upgrade is missing the ttytab or gettytab file, but"
	echo "I will try to continue with the upgrade anyway."
	echo "Please contact FORE Systems technical support for assistance."
fi


#
#  Install the new inetd.conf file
#  (This allows us to patch up a few security holes such as rexd)
# 
if  tar xf distribution ./inetd.conf ; then
        mv /etc/inetd.conf /etc/inetd.conf.old
        cp ./inetd.conf /etc/inetd.conf
else
	echo "The upgrade distribution is missing the inetd.conf file, but"
	echo "I will try to continue with the upgrade anyway."
	echo "Please contact FORE Systems technical support for assistance."
fi


#
#  We need to remove the "ifconfig -a netmask + broadcast + > /dev/null"
#  line from rc.local since this overwrites what our rc.ip does.
#  (The lines with "netmask" in them are the evil ones)
#
cp /etc/rc.local ./rc.local.old
egrep -v  netmask  ./rc.local.old  >  /etc/rc.local   


#
# We need to save the old configuration database (CDB) especially when
# upgrading from 2.3 to 3.0 and 3.2 so we can restore the channels and
# paths with pv2cdb and pv2cdb.tcl in rc.fore
#
cd /atm/etc

rm -f cdb*.*.*

if cp cdb.conf cdb$oldversion ; then
	ln -s cdb$oldversion cdb.pv2cdb
	echo "A backup of the configuration database has been saved in cdb$oldversion"
else
	echo "I cannot backup the configuration database"
fi


cd /atm/release

# There might not be enough room for more than 2 versions, so we first
# remove any old, saved versions. (We can tell that they are old saved
# versions since if we saved them with this install script,
# they get saved in the form foreXXX where XXX is a version with a "." in it)
#
# We then move the installed version to foreXXX.
# Lastly, we untar the distribution into the ./fore directory

rm -rf fore*.*

#
# Check if this is a themis board.
#
if [ -f /etc/themis ] ; then
	SYSTYPE=themis.sun4m
else
	SYSTYPE=`arch -k`
fi

if mv fore fore$oldversion ; then
	echo "The old distribution has been saved in fore$oldversion"
else
	echo "I cannot save the old distribution, upgrade terminated."
	exit 1;
fi

echo "Extracting new software..."

if tar xf /tmp/distribution ./fore ; then
	cd /
	rm -f /vmunix.*	
	tar xf /tmp/distribution ./vmunix.$version.$SYSTYPE
	if [ -f ./vmunix.$version.$SYSTYPE ] ; then
		mv /vmunix /vmunix.$oldversion
		mv ./vmunix.$version.$SYSTYPE /vmunix.$version
		ln /vmunix.$version /vmunix
	else 
		tar xf /tmp/distribution ./vmunix.$version.$SYSTYPE.Z
		if [ -f ./vmunix.$version.$SYSTYPE.Z ] ; then
			/usr/ucb/uncompress ./vmunix.$version.$SYSTYPE.Z
			mv /vmunix /vmunix.$oldversion
			mv ./vmunix.$version.$SYSTYPE /vmunix.$version
			ln /vmunix.$version /vmunix
		else
			echo ""
			echo " Unable to install new kernel! "
			echo "( " $version "." $SYSTYPE " )"
			echo "Please contact FORE Systems' technical support for assistance."	
			exit 1
		fi
	fi


#
# Make sure /etc/netmasks points to /atm/etc/netmasks
#

	if [ -f /atm/etc/netmasks ] ; then
		if [ -f /etc/netmasks ] ; then
			echo "Moving /etc/netmasks to /etc/netmasks.old"
			/usr/bin/mv /etc/netmasks /etc/netmasks.old
		fi
	else
		if [ ! -f /etc/netmasks ] ; then
			/usr/bin/touch /atm/etc/netmasks
		else
			/usr/bin/mv /etc/netmasks /atm/etc/netmasks
		fi
	fi
	echo "/etc/netmasks linked to /atm/etc/netmasks"
	/usr/bin/ln -s /atm/etc/netmasks /etc/netmasks


#
# Make sure the hostname.qaa* files for interfaces are created and valid
#

        if [ ! -f /atm/etc/hostname.qaa0 ]; then
                echo "UNCONFIGURED" > /atm/etc/hostname.qaa0
        fi
        if [ ! -f /atm/etc/hostname.qaa1 ]; then
                echo "UNCONFIGURED" > /atm/etc/hostname.qaa1
        fi
        if [ ! -f /atm/etc/hostname.qaa2 ]; then
                echo "UNCONFIGURED" > /atm/etc/hostname.qaa2
        fi
        if [ ! -f /atm/etc/hostname.qaa3 ]; then
                echo "UNCONFIGURED" > /atm/etc/hostname.qaa3
        fi

        if [ ! -f /etc/hostname.qaa0 ]; then
                ln -s /atm/etc/hostname.qaa0 /etc/hostname.qaa0
        fi
        if [ ! -f /etc/hostname.qaa1 ]; then
                ln -s /atm/etc/hostname.qaa1 /etc/hostname.qaa1
        fi
        if [ ! -f /etc/hostname.qaa2 ]; then
                ln -s /atm/etc/hostname.qaa2 /etc/hostname.qaa2
        fi
        if [ ! -f /etc/hostname.qaa3 ]; then
                ln -s /atm/etc/hostname.qaa3 /etc/hostname.qaa3
        fi


# 
# the minor and major device numbers for the asx* and qaa* devices
# depend on the architecture of the machines. 
#
#
        if [ $SYSTYPE = "sun4c" ]  || [ $SYSTYPE = "sun4e" ]; then
                rm -f /dev/asx0;  /etc/mknod /dev/asx0 c 37 104
                rm -f /dev/asx1;  /etc/mknod /dev/asx1 c 37 104
                rm -f /dev/asx2;  /etc/mknod /dev/asx2 c 37 104
                rm -f /dev/asx3;  /etc/mknod /dev/asx3 c 37 104
                rm -f /dev/qaa0;  /etc/mknod /dev/qaa0 c 37 104
                rm -f /dev/qaa1;  /etc/mknod /dev/qaa1 c 37 104
                rm -f /dev/qaa2;  /etc/mknod /dev/qaa2 c 37 104
                rm -f /dev/qaa3;  /etc/mknod /dev/qaa3 c 37 104
        elif [ $SYSTYPE = "sun4m" ]; then
                rm -f /dev/asx0;  /etc/mknod /dev/asx0 c 37 108
                rm -f /dev/asx1;  /etc/mknod /dev/asx1 c 37 108
                rm -f /dev/asx2;  /etc/mknod /dev/asx2 c 37 108
                rm -f /dev/asx3;  /etc/mknod /dev/asx3 c 37 108
                rm -f /dev/qaa0;  /etc/mknod /dev/qaa0 c 37 108
                rm -f /dev/qaa1;  /etc/mknod /dev/qaa1 c 37 108
                rm -f /dev/qaa2;  /etc/mknod /dev/qaa2 c 37 108
                rm -f /dev/qaa3;  /etc/mknod /dev/qaa3 c 37 108
        fi

#
# if /.rhosts file exists, remove it
#

        if [ -f /.rhosts ]; then
        	rm -f /.rhosts
        fi

	if [ -f /atm/etc/cdb.conf ]; then
		echo "Updating PVCs for UNI 3.0 compatibility..."
		/atm/release/fore/bin/pvc2upc
	fi

	echo ""
     	echo "Installed ForeThought switch control software version $version"

	echo "The system must be rebooted in order for the changes to"
	echo "take effect."
	echo ""
	echo -n "Press return in order to reboot the system:"
	read dummy
	reboot
	sleep 90
	echo "I could not reboot the system."
	echo "Please contact FORE Systems' technical support."	
else
	echo ""
	echo "I could not extract the new distribution, and am attempting"
	echo "to restore the old version."
	rm -rf ./fore
	if mv fore$oldversion fore ; then
		echo "I successfully restored the old version $oldversion"
	else
		echo "I could not restore the old version."
	fi
	echo "Please contact FORE Systems' technical support for assistance."
	exit 1
fi



 


