#!/bin/sh
#
#	request: SunPC 4.2 package add request script.
#
#       Copyright (c) 1994, Sun Microsystems, Inc.  All Rights Reserved
#
#	This file is intended to run before the package add actually
#	begins. We are going to check for existing instances of SunPC
#	packages and fail if they are. All instances of SunPC need to
#	be removed so that we can assure all patches are removed.
#
#	Also, before we inform user to remove the packages, we copy the
#	contents of /opt/SUNWsunpc/dos to /usr/temp. postinstall will look
#	and see if this directory exists and copy it back into the 4.1 
#	package install area. This allows 4.0 and 4.0.1 users who are
#	running with DOS 4.0.1 to boot normally without loosing their
#	dos external command area.
#	
#	We also preserve the all DOS files, including bootable disk templet
#	external DOS commands to /usr/temp. This allow previous sunpc owner
#	can still use their DOS.
#
# set -x

# The save area used to copy out the DOS4 files from a previous SunPC 4.X package.
DOS4_SAVE_AREA="/usr/dos_temp/dos4.0.1"
DOS_SAVE_AREA="/usr/dos_temp/dos_save"

# The amount of space required, 10 Megabytea
TMPREQD=10000

/usr/bin/pkginfo -q SUNWsunpc
PKGSTATUS=$?

if [ "$PKGSTATUS" -eq "0" ]
then

	echo " "

	#
	# Rerun pkginfo to get the installation base directory.
	#

	PKGTEXT=`/usr/bin/pkginfo -l SUNWsunpc`
	OLDBASEDIR=`/usr/bin/echo "$PKGTEXT" | /usr/bin/grep BASEDIR | /bin/awk -F" " '{ print $2 }'`
	OLDBASEVER=`/usr/bin/echo "$PKGTEXT" | /usr/bin/grep VERSION | /bin/awk -F" " '{ print $2 }'`

	#
	# Now Save the users dos 4.0.1 area.
	#

	if [ $OLDBASEVER = "4.0" -o $OLDBASEVER = "4.0.1" ]
	then

		if [ ! -d $DOS4_SAVE_AREA ]
		then

			# Check for minimum amount of space in /usr/temp.
			DF=`/bin/df -k /usr`
			TMPAVAIL=`echo $DF | /bin/awk -F" " '{ print $(NF - 2) }'`
			if [ "$TMPAVAIL" -lt "$TMPREQD" ]
			then
				echo "  There is not enough space in directory //usr to save your DOS 4.0.1 files."
				echo "  "$TMPAVAIL" Kbytes are available, "$TMPREQD" Kbytes are required."
				exit 1
			fi

			echo "  Running: /usr/bin/cp -p -r $OLDBASEDIR/SUNWsunpc/dos $DOS4_SAVE_AREA"
                        /usr/bin/mkdir -p $DOS4_SAVE_AREA
			/usr/bin/cp -p -r $OLDBASEDIR/SUNWsunpc/dos $DOS4_SAVE_AREA
			echo "  DOS4.0.1 area saved"
			echo " "
		fi

	fi
	#
	# Now Save the users DOS area.
	#
# for DOS 4.01
	if [ -f $OLDBASEDIR/SUNWsunpc/defaults/C.4.01.sunpc3 ]
	then

		if [ ! -d $DOS_SAVE_AREA ]
		then

			# Check for minimum amount of space in /usr/temp.
			DF=`/bin/df -k /usr`
			TMPAVAIL=`echo $DF | /bin/awk -F" " '{ print $(NF - 2) }'`
			if [ "$TMPAVAIL" -lt "$TMPREQD" ]
			then
				echo "  There is not enough space in directory //usr to save your DOS files."
				echo "  "$TMPAVAIL" Kbytes are available, "$TMPREQD" Kbytes are required."
				exit 1
			fi

			echo "  Copying DOS files "
			/usr/bin/mkdir -p $DOS_SAVE_AREA/dos6
			/usr/bin/cp -p -r \
	$OLDBASEDIR/SUNWsunpc/dos/msdos $DOS_SAVE_AREA/dos6/msdos
                        /usr/bin/mkdir -p $DOS_SAVE_AREA/defaults/6.22
			/usr/bin/cp -p \
	$OLDBASEDIR/SUNWsunpc/defaults/C.4.01.sunpc3 \
	$DOS_SAVE_AREA/defaults/C.6.22.sunpc3
			/usr/bin/cp -p \
	$OLDBASEDIR/SUNWsunpc/defaults/4.01/command.com \
	$DOS_SAVE_AREA/defaults/6.22/command.com
			/usr/bin/cp -p -r \
	$OLDBASEDIR/SUNWsunpc/defaults/4.01/dos \
	$DOS_SAVE_AREA/defaults/6.22/dos
			echo "  DOS files saved"
			echo " "
		fi

	fi

# for other DOS (include boundled 6.22 or user installed versions
	if [ -f $OLDBASEDIR/SUNWsunpc/defaults/C.6.22.sunpc3 ]
	then

		if [ ! -d $DOS_SAVE_AREA ]
		then

			# Check for minimum amount of space in /usr
			DF=`/bin/df -k /usr`
			TMPAVAIL=`echo $DF | /bin/awk -F" " '{ print $(NF - 2) }'`
			if [ "$TMPAVAIL" -lt "$TMPREQD" ]
			then
				echo "  There is not enough space in directory /usr to save your DOS files."
				echo "  "$TMPAVAIL" Kbytes are available, "$TMPREQD" Kbytes are required."
				exit 1
			fi

			echo "  Copying DOS files "
	                /usr/bin/mkdir -p $DOS_SAVE_AREA/dos6
			/usr/bin/cp -p -r \
	$OLDBASEDIR/SUNWsunpc/dos6/msdos $DOS_SAVE_AREA/dos6/msdos
                        /usr/bin/mkdir -p $DOS_SAVE_AREA/defaults/6.22	
			/usr/bin/cp -p \
	$OLDBASEDIR/SUNWsunpc/defaults/C.6.22.sunpc3 \
	$DOS_SAVE_AREA/defaults/C.6.22.sunpc3
			/usr/bin/cp -p \
	$OLDBASEDIR/SUNWsunpc/defaults/6.22/command.com \
	$DOS_SAVE_AREA/defaults/6.22/command.com
			/usr/bin/cp -p -r \
	$OLDBASEDIR/SUNWsunpc/defaults/6.22/dos \
	$DOS_SAVE_AREA/defaults/6.22/dos
			echo "  DOS files saved"
			echo " "
		fi

	fi
	#
	# Last step -- tell user that the SUNWsunpc package needs to be removed.
	#
	echo "  --------------------------------------------------------------"
	echo "  All instances of existing SunPC package(s) need to be removed."
	echo "  As root, run:"
	echo " "
	echo "                   /usr/sbin/pkgrm SUNWsunpc.*"
	echo " "
	echo "  Please make sure that you specify 'SUNWsunpc.*'"
	echo "  --------------------------------------------------------------"
	echo "  "

fi

exit 0

