#!/bin/ksh -p

# script to update patches on a machine. See usage() below
# Written by Philip Brown


patchdir=`dirname $0`
cd $patchdir


usage()
{
	print "usage: patchupdate [-R /root/dir]"
	print "patchupdate uses patchcompare -l to figure out what patches are downrev,"
	print " and will then install them, using the patch_order file in the"
	print "same directory as patchupdate"
	print "(currently $patchdir) "
	print This means that you should copy this program into the same
	print directory as your patches, before using it.
	print You will also need "'patchcompare'" there.
	print "(You may want to use symlinks, over multiple copies)"
}


if [[ "$1" = "-h" ]] ; then
	usage
	exit
fi


if [[ "$1" = "-R" ]] ; then
	ROOTARGS="$1 $2"
	shift
	shift
fi

if [[ ! -f patch_order ]] ; then
	print ERROR: need a patch_order file
	exit 1
fi

######################################################################
# End of prep work. real stuff starts here

TMPFILE=/tmp/patchupdate.$$
rm -f $TMPFILE; touch $TMPFILE

./patchcompare $ROOTARGS -l |awk -F: '{print $1}' >$TMPFILE
count=`wc -w $TMPFILE`
if [[ ! -s $TMPFILE ]] ; then
	print No patches to add
	rm $TMPFILE
	exit 0
else
	print Attempting to apply $count patches
fi

patchadd $ROOTARGS -M . `fgrep -f $TMPFILE patch_order`


rm $TMPFILE


