#! /bin/sh
#
#
# Copyright (c) 1993 SunService, Inc. A Sun Microsystems Business
#
# Semi-automate the installation of a patch
#

PATH=/usr/ucb:/usr/bin:${PATH}
export PATH

TODO=""
PATCHID=""
SUNOS=""


cleanup ()
{
	# remove the tmp directory files
	if [ x"$TODO" = x ]; then
		return
	else
		for f in $TODO; do
			rm -rf $TMPDIR/$TODO
		done
	fi
}

trap "cleanup; exit" 0 1 2 10 12 13

#
# Solaris provides a standardized way to install patches, so we'll
# do as many cool things as possible
#
install_solaris_patch ()
{

	clear
	cat <<EOF

The installation for Solaris 2.x patches provides the option of
saving the original versions of the software being patched. 
Unfortunately, this occasionally will cause your /var/sadm/patch directory
to grow too large. By default, this installation will NOT save
your original versions of software.

EOF

	echo -n "Would you like to save the original versions of the software? [no] "
	read SAVEOLD

	case x"$SAVEOLD" in
		x|xn*|xN*)	SAVEOLD=FALSE 
					options="-d" ;;
		*)			SAVEOLD=TRUE 
					options="" ;;
	esac

	clear

	if [ ! -d /var/sadm/patch ]; then
		echo "No patches installed!"
	else
		echo "Patches already installed:"
		ls /var/sadm/patch
	fi
	echo ""; echo ""

	cat <<EOF

Next you will be prompted for the patches you wish to install. If you
would like to install the suggested patches, enter "suggested". Otherwise
enter the patch id. 

To see a list of the patches you have entered, type a '?' <Return>

To start the installation, press <Return> when prompted for a patch id.

EOF

	while [ 1 ]; do
		echo -n "Patch to install (patchid, suggested, ?): "
		read PATCHID

		case x"$PATCHID" in
		x?) echo "Currently selected patches:"
			echo "$TODO"
			echo ""
			;;
		xsuggested)	TODO=suggested 
					break ;;
		x/) echo "Invalid patch id!" ;;
		x)	if [ x"$TODO" = x ]; then
				echo "You haven't selected anything to install yet!"
				continue
			else
				break
			fi
			;;
		*)	if [ ! -f files/$PATCHID.tar.Z ]; then
				NEWID=`echo files/$PATCHID-??.tar.Z`
				if [ "$NEWID" = "files/$PATCHID-??.tar.Z" ]; then
					echo "$PATCHID not found"
					continue
				else
					PATCHID=`echo $NEWID | sed 's/\..*//' | sed 's/files\///'`
				fi
			fi
			if [ -d /var/sadm/patch/$PATCHID ]; then
				echo "$PATCHID has already been installed, ignoring."
				continue
			else
				if [ x"$TODO" = x ]; then 
					TODO=$PATCHID 
				else 
					TODO="$TODO $PATCHID" 
				fi 
			fi
			;;
		esac
	done
	
	clear
	cat <<EOF

Patch installation setup:

	Temporary directory: $TMPDIR
	Save old versions of files: $SAVEOLD
	Patches to install: $TODO

EOF
	echo -n "Is this correct? [y] "
	read CONT
	
	case x"$CONT" in
	x|xy*|xY*) ;;
	*) echo "Aborting patch installation!"
		exit 1 ;;
	esac

	here=`pwd`

	if [ $TODO = suggested ]; then
		REL=`/bin/uname -r`
		echo "Installing suggested patches for release $REL"
		SUGGESTED=.sunos.${REL}.suggestedpatches
		if [ ! -f $SUGGESTED ]; then
			echo "Couldn't find a suggested configuration file for $REL"
			exit 1
		fi
		TODO=`cat $SUGGESTED`
	fi
	
	cd $TMPDIR
	for f in $TODO; do
		echo "Copying $f to $TMPDIR"
		zcat $here/files/$f.tar.Z | tar xvf -
		if [ ! -f $TMPDIR/$f/installpatch ]; then
			echo "This patch is not a valid Solaris patch. Aborting installation of $f"
		else
			echo "Beginning installation..."
			echo "Options: $options"
			$TMPDIR/$f/installpatch $options $TMPDIR/$f
			echo "Installation of $f complete."
		fi
		echo "Removing temporary files..."
		rm -rf $TMPDIR/$f
	done
	exit 0
}

#
#
# This won't really install a patch - it will at most copy the patch installation files
# to the temp directory specified, display the README then exit.
#
#

install_a_patch()
{
	clear
	cat <<EOF



SunOS4.x doesn't provide a standardized way of installing patches, therefore,
this script will do only the first part of the installation (copy to the temp
directory, uncompress and untar the patch). To complete the installation
you will be required to read the README file in the patch directory and follow
the installation instructions provided.

EOF

	while [ 1 ]; do

		echo -n "Enter the patch id to install: "
		read PATCHID
		if [ x"$PATCHID" = x ]; then
			echo "Nothing selected!"
			continue
		fi
		if [ ! -f files/$PATCHID.tar.Z ]; then
			NEWID=`echo files/$PATCHID-??.tar.Z`
			if [ "$NEWID" = "files/$PATCHID-??.tar.Z" ]; then
				echo "$PATCHID not found"
				continue
			else
				PATCHID=`echo $NEWID | sed 's/\..*//' | sed 's/files\///'`
				break
			fi
		else
			break
		fi
	done

	echo "Ready to copy $PATCHID to $TMPDIR"
	echo -n "Continue? [y] "
	read CONT
	case x"$CONT" in
	xy*|x|xY*) ;;
	*) echo "Aborting installation"
		exit 1 ;;
	esac

	here=`pwd`
	cd $TMPDIR
	zcat $here/files/$PATCHID.tar.Z | tar xvf -
	more $TMPDIR/$PATCHID/README*
	echo ""; echo ""; echo ""
	cat <<EOF

To complete installation of this patch, cd to $TMPDIR/$PATCHID, and
follow the instructions provided in the README file located there.

Be sure to remove the $TMPDIR/$PATCHID directory once your patch installation
has been completed.

EOF
	exit 0
}

copyright()
{
#
# Display the copyright notice first
# 

clear
cat <<EOF | more
=============================================================================

	patchinstall - install a patch

     Copyright (c) 1993 Sun Microsystems, Inc. All Rights Reserved.
            Printed in the United States of America.
2550 Garcia Avenue, Mountain View, California, 94043-1100 U.S.A.

Patches are distributed to SunService Contract Customers ONLY. Redistribution
to unauthorized parties is prohibited by the SunService Contract.

Installation of all patches is not suggested as some patches may conflict
with one another. Please make sure that the patch you are installing
is necessary before actually installing the patch.

=============================================================================

During the installation, default answers will be provided inside brackets '[]'
Pressing the <Return> key will select the default provided.

Press <Ctrl-C> at any time to stop the installation.


EOF

echo -n "Continue with patch installation? [Y] "
read CONT

case x"$CONT" in
xY|x|xy|xy*|xY*) ;;
*) echo "Aborting patch installation"
	exit -1 ;;
esac
clear
}

check_os_level ()
{
	case "`/bin/uname -r`" in
		5.*) SUNOS=FALSE ;;
		*) 	 SUNOS=TRUE ;;
	esac

}

#
# get_temp_directory
# store the name of a valid temp directory in TMPDIR
#

get_temp_directory () {
	cat <<EOF

Installation of patches will use several temporary files, and may
need several megabytes of space. Please enter the name of a directory 
that can be used for these temporary files (the directory must exist 
before running the installation!)

EOF

	while [ 1 ]; do
		PROTO=/tmp
		echo -n "Where should I store temporary files? [$PROTO] "
		read TMPDIR
		if [ x"$TMPDIR" = x ]; then
			TMPDIR=$PROTO
		fi
		if [ ! -d $TMPDIR ]; then
			echo "$TMPDIR isn't a directory, try again!"
		else 
			if [ ! -w $TMPDIR ]; then
				echo "$TMPDIR isn't writable, try again!"
			else
				break
			fi
		fi
	done
}

#
#
# MAIN PROGRAM
#
#

#
# Display the copyright notice first
# 

copyright

#
# Check the OS level
#

check_os_level

#
# Check the user 
#

/usr/bin/id | grep -s '^uid=0(root)' > /dev/null
if [ $? -ne 0 ]; then
	 	echo "ERROR:"
		echo "You must be root to install patches!"
		echo "Aborting installation!"
		exit 1
fi

#
# Get the temporary directory
#

get_temp_directory

#
# Do the work...
#

if [ $SUNOS = TRUE ]; then
	install_a_patch
else
	install_solaris_patch
fi
