#!/bin/sh

PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/contrib/bin
BLOCKSIZE=1K
KILLBSD=no
INSTALLSHLIBS=yes
export PATH BLOCKSIZE

die() {
	# Leave this in place at Karels' request
	# rm -rf /var/tmp/UPGRADE.4.3
	mv /etc/rc.configure/0.upgrade.1 /etc/rc.configure/0.upgrade.1.USED
	exit $1
}

yesno() {
	_RESPONSE=maybe
	while [ $_RESPONSE = maybe ] ; do
		echo -n "$1 [$2] "
		read _RESPONSE
		case "$_RESPONSE" in
		"")		_RESPONSE=$2 ;;
		[Yy]*)		_RESPONSE=yes ;;
		[Nn]*)		_RESPONSE=no ;;
		*)		_RESPONSE=maybe ;;
		esac
	done
	if [ $_RESPONSE = "yes" ] ; then
		return 0
	else
		return 1
	fi
}

askum() {
	_RESPONSE=
	while [ "$_RESPONSE" = "" ] ; do
		if [ "$2" = "" ] ; then
			echo -n "$1 " 1>&2
		else
			echo -n "$1 [$2] " 1>&2
		fi
		read _RESPONSE
		if [ "$_RESPONSE" = "" ] ; then
			_RESPONSE="$2";
		fi
	done
	echo $_RESPONSE
}

# The UPGRADE script should have left the pieces for us...
if [ ! -d /var/tmp/UPGRADE.4.3 ]; then
	echo
	echo "No UPGRADE.4.3 directory???"
	echo "Upgrade aborted"
	die 1
fi
cd /var/tmp/UPGRADE.4.3

if [ ! -f SHELLVARS ]; then
	echo
	echo "No SHELLVARS file???"
	echo "Upgrade aborted"
	die 1
fi
. SHELLVARS

cat <<!

Welcome to the continuation of the BSD/OS 4.3 Upgrade Procedure.

The upgrade procedure will now run any /etc/rc.hardware scripts
the system uses to ensure any network or disk devices (e.g., PCMCIA)
will be available.

!

if [ -d /etc/rc.hardware ]
then
	cd /etc/rc.hardware
	echo -n system specific hardware configuration:
	for i in *
	do
		case $i in
		[0-9]*.*.*[0-9])
			sh $i
			;;
		*)
			;;
		esac
	done
	echo .
	cd /var/tmp/UPGRADE.4.3
fi

if [ X$LOC = Xremote ]; then
	cat <<!
The upgrade was previously using a remote CDROM drive to access 
the 4.3 installation media.  We will now re-start the network
using the existing netstart script.

!
	. /etc/netstart
else
	if [ ! -f $RDIR/.version ]; then
		cat <<!
The upgrade was previously using a local CDROM drive which was 
not mounted automatically.  We will now attempt to re-mount the 
directory we were using before.

!
		mount $RDIR
		if [ ! -f $RDIR/.version ]; then
			echo "Mount failed -- upgrade aborted"
			die 1
		fi
	fi
fi

cat <<!
You may choose either the Express Upgrade or the Custom Upgrade.

The Express Upgrade will automatically upgrade the packages that
were previously installed on this BSD/OS 4.2 system.

The Custom Upgrade allows you to choose which packages to upgrade.
It allows you to install packages that were not previously installed.

!

MODE=
while [ "$MODE" = "" ] ; do
	TMP="$(askum "Express or Custom upgrade?" express)"
	case "$TMP" in
	[Cc]*)		MODE=custom ;;
	"" | [Ee]*)	MODE=express ;;
	*)		echo
			echo "$MODE: invalid response."
			echo "Please respond with 'express' or 'custom'"
			echo
			MODE=""
			;;
	esac
done

if [ $MODE = "express" ] ; then
	IARGS=-Usu
else
	IARGS=-U
fi
if [ $HOST ] ; then
	installsw -d / -m cdrom -E -h $HOST -l $USER -c $RDIR $IARGS
else
	installsw -d / -m cdrom -E -L -c $RDIR $IARGS
fi

if [ $? -ne 0 ] ; then 
		cat <<!
The upgrade has failed for some reason.  This may leave your system
in an inconsistent state.  You may need to either install using the 
normal new installation procedure or restore the system from backups.
!
		die 1
fi

# Update install.log to reflect that we did an upgrade...
echo "package CORE_VAR-4.3 installed-upg $(date)" >>/var/db/install.log
echo "package CORE_ROOT_CONFIG-4.3 installed-upg $(date)" >>/var/db/install.log

if [ $INSTALLSHLIBS = "yes" ] ; then
	for i in shlib/* ; do
		if [ -h $i ] ; then
			echo "Installing /$i"
			if [ -f /$i ] ; then
				chflags nouchg,noschg /$i
			fi
			if ! cp -Rp $i /$i.new ; then
				echo "Failed to install new /$i"
			elif [ ! -f /$i.new ] ; then
				rm -f /$i.new
				echo "The new version of /$i was not installed"
			elif ! mv -f /$i.new /$i ; then
				rm -f /$i.new
				echo "Failed to install /$i"
			fi
		fi
	done
fi

# Build the dynamic library cache
ldconfig >/dev/null 2>&1

(cd binaries ; find . -type f -print) | while read FILE ; do
	FILE=$(echo $FILE | sed -e s/..//)
	echo "Removing /$FILE.4.2"
	rm -f /$FILE.4.2
done

# Get patches off the CDROM (if any)
if [ $LOC = "remote" ] ; then
	cmd="rsh -l $USER $HOST sh -c 'test -f '$RDIR'/PATCHES.tar; echo \$?' "
	tmp=`$cmd`
	if [ $tmp -eq 0 ]; then
		echo "Copying patches from installation cdrom..."
		mkdir /var/tmp/PATCHES
		rsh -l $USER $HOST cat $RDIR/PATCHES.tar | \
			(cd /var/tmp/PATCHES; pax -rpe)
	fi
else
	if [ -f $RDIR/PATCHES.tar ]; then
		echo "Copying patches from installation cdrom..."
		mkdir /var/tmp/PATCHES
		(cd /var/tmp/PATCHES; pax -rpe < $RDIR/PATCHES.tar)
	fi
fi
if [ -x /var/tmp/PATCHES/install.cdrom ]; then
	echo "Installing patches..."
	/var/tmp/PATCHES/install.cdrom /
fi

# Install patches from the floppy (if any)
if [ -d /var/tmp/PATCHES ]; then
	echo "Installing INSTALL floppy patches..."
	/var/tmp/PATCHES/install.floppy /
fi

cat <<!

PLEASE READ THE 4.3 RELEASE NOTES ON CHANGES IN THE SYSTEM.

You should now reboot your machine to switch to the freshly installed
4.3 utilities.  When the system checks the filesystems it will most likely
clean up a few unreferenced files left behind by the upgrade (from
unlinked shared libraries).  This is normal and expected.

!
echo -n "Press <ENTER> to reboot: "
read _RESPONSE

rm -rf /var/tmp/UPGRADE.4.3
mv /etc/rc.configure/0.upgrade.1 /etc/rc.configure/0.upgrade.1.USED
sync; sync
reboot
