# This script creates the backout package for a patch package
#
# package format options - does not support multiple-package stream.
#
#       @(#)postinstall 1.2 95/08/29 SMI
#
# Copyright (c) 1995 by Sun Microsystems, Inc.
# All rights reserved
#

PATH=/usr/sadm/bin:$PATH

if [ "$PKG_INSTALL_ROOT" = "/" ]; then
	PKG_INSTALL_ROOT=""
fi

if [ -n "$PATCH_BUILD_DIR" -a -d "$PATCH_BUILD_DIR" ]; then
	BUILD_DIR="$PATCH_BUILD_DIR/$PKGINST"
else
	BUILD_DIR="$PKG_INSTALL_ROOT/var/tmp/$PKGINST"
fi

FILE_DIR=$BUILD_DIR/files
RELOC_DIR=$FILE_DIR/reloc
ROOT_DIR=$FILE_DIR/root
BO_Deletes=$FILE_DIR/deletes
THIS_DIR=`dirname $0`
PROTO_FILE=$BUILD_DIR/prototype

BOINFO_MSG="To back out this patch type the following."
SBOCMD_MSG="pkgadd -R $PKG_INSTALL_ROOT -d $PKGSAV/$SUNW_PATCHID/undo $PKG"
CBOCMD_MSG="pkgadd -d $PKGSAV/$SUNW_PATCHID/undo $PKG"

#
# At this point we either have a deletes file or we don't. If we do,
# we create a prototype entry.
#
if [ -f $BO_Deletes ]; then
	echo "i deletes=$BO_Deletes" >> $BUILD_DIR/prototype
fi

#
# Now delete everything in the deletes list after transferring
# the file to the backout package and the entry to the prototype
# file. Remember that the pkgmap will get the CLIENT_BASEDIR path
# but we have to actually get at it using the BASEDIR path. Also
# remember that removef will import our PKG_INSTALL_ROOT
#
Our_Deletes=$THIS_DIR/deletes
if [ -f $Our_Deletes ]; then
	cd $BASEDIR

	cat $Our_Deletes | while read path; do
		Reg_File=0

		if valpath -l $path; then
			Client_Path="$CLIENT_BASEDIR/$path"
			Build_Path="$RELOC_DIR/$path"
			Proto_Path=$BASEDIR/$path
		else	# It's an absolute path
			Client_Path=$path
			Build_Path="$ROOT_DIR$path"
			Proto_Path=$PKG_INSTALL_ROOT$path
		fi

		# Note: If the file isn't really there, pkgproto
		# doesn't write anything.
		LINE=`pkgproto $Proto_Path=$path`
		ftype=`echo $LINE | nawk '{ print $1 }'`
		if [ $ftype = "f" ]; then
			Reg_File=1
		fi

		if [ $Reg_File -eq 1 ]; then
			# Add source file to the prototype entry
			if [ "$Proto_Path" = "$path" ]; then
				LINE=`echo $LINE | sed -e s@$Proto_Path@$Build_Path@2`
			else
				LINE=`echo $LINE | sed -e s@$Proto_Path@$Build_Path@`
			fi

			DirName=`dirname $Build_Path`
			# make room in the build tree
			mkdir -p $DirName
			cp -p $Proto_Path $Build_Path
		fi

		# Insert it into the prototype file
		echo $LINE 1>>$PROTO_FILE 2>/dev/null

		# Remove the file only if it's OK'd by removef
		rm `removef $PKGINST $Client_Path` 1>/dev/null 2>&1
	done
	removef -f $PKGINST

	rm $Our_Deletes
fi

#
# Unless specifically denied, make the backout package.
#
if [ "$PATCH_NO_UNDO" != "true" ]; then
	cd $BUILD_DIR
	if [ -d $PKGSAV/$SUNW_PATCHID ]; then
		rm -r $PKGSAV/$SUNW_PATCHID
	fi
	mkdir $PKGSAV/$SUNW_PATCHID
	pkgmk -o -d $PKGSAV 1>/dev/null 2>&1
	pkgtrans -s $PKGSAV $PKGSAV/$SUNW_PATCHID/undo $PKG 1>/dev/null 2>&1
	rm -r $PKGSAV/$PKGINST

	cd ..

	rm -r $BUILD_DIR

	puttext "$BOINFO_MSG"
	echo "    $CBOCMD_MSG"
	if [ ${PKG_INSTALL_ROOT} ]; then
		echo on the client or 
		echo "    $SBOCMD_MSG"
		echo on the server.
	fi
fi

#
# Since this apparently worked, we'll mark the obsoleted stuff.
#
cd ${PKG_INSTALL_ROOT:-/}
cd var/sadm/pkg

INSTALLED_BASE=` pkginfo | nawk '
	{ printf ("%s ", $2) }
	END { printf("\n") } '`

#
# First, the explicit obsoletions
#
if [ -n "$ACTIVE_OBSOLETES" ]; then
	for patch in $ACTIVE_OBSOLETES; do
		for pkg in $INSTALLED_BASE; do
			if [ -f $pkg/save/$patch/undo ]; then
				mv $pkg/save/$patch/undo $pkg/save/$patch/obsolete
				echo $SUNW_PATCHID >> $pkg/save/$patch/obsoleted_by
			elif  [ -f $pkg/save/$patch/obsolete ]; then
				echo $SUNW_PATCHID >> $pkg/save/$patch/obsoleted_by
			fi
		done
	done
fi

#
# Now obsolete prior versions of this patch
#
active_base=`echo $SUNW_PATCHID | nawk '
	{ print substr($0, 1, match($0, "-")-1) } '`

List=`ls -d $PKGINST/save/${active_base}*`
if [ $? -ne 0 ]; then
	List=""
fi

for savedir in $List; do
	if [ `basename $savedir` = $SUNW_PATCHID ]; then
		break
	fi

	if [ -f $savedir/undo ]; then
		mv $savedir/undo $savedir/obsolete
		echo $SUNW_PATCHID >> $savedir/obsoleted_by
	elif  [ -f $savedir/obsolete ]; then
		echo $SUNW_PATCHID >> $savedir/obsoleted_by
	fi
done
 
# If additional operations are required for this package, place
# those package-specific commands here.

#XXXSpecial_CommandsXXX#

exit 0
