#!/bin/sh
# @(#)prePatch 1.15 04/05/26 SMI
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.

/usr/bin/mkdir /tmp/"$PatchNum"_$$

TMPDIR=/tmp/"$PatchNum"_$$
ADMIN="$TMPDIR"/admin_$$
PKG_STREAM="SUNWman.stream.pkg"

# This prePatch script is executed before $patchdir or $ROOTDIR are set.
PKGDIR="`/usr/bin/pwd`"
PATCH_INSTALL_ROOT="$1"

cat > $ADMIN << EOF
mail=
instance=unique
partial=nocheck
runlevel=nocheck
idepend=nocheck
rdepend=nocheck
space=quit
setuid=nocheck
conflict=nocheck
action=nocheck
EOF

# Is SUNWman installed?
pkginfo -R $PATCH_INSTALL_ROOT -q SUNWman
ret=$?

if [ $ret -eq 1 ]
then
	echo "SUNWman is not installed on this system, prePatch script done..."
	/usr/bin/rm -rf $TMPDIR
	exit 0
fi

# Is it the VERSION we want?
right_version="42.6,REV=6.1"
version_check="`/usr/bin/pkgparam -R $PATCH_INSTALL_ROOT -f $PATCH_INSTALL_ROOT/var/sadm/pkg/SUNWman/pkginfo VERSION`"

if [ "$right_version" = "$version_check" ]
then
	echo "SUNWman version 42.6 is installed, prePatch script done..."
	/usr/bin/rm -rf $TMPDIR
	exit 0
fi

ref_version="42"
installed_version=`echo $version_check|cut -d. -f1`

if [ $installed_version -gt $ref_version ]
then
	echo "SUNWman version newer than 42.6 is installed, prePatch script done..."
	/usr/bin/rm -rf $TMPDIR
	exit 0
fi

# Remove all instances of the package
# and, if that was successful, pkgadd the package stream

echo "Replacing SUNWman package, this will take several minutes..."
/usr/sbin/pkgrm -n -R $PATCH_INSTALL_ROOT -a $ADMIN -A SUNWman > /dev/null 2>&1
ret=$?

if [ $ret -eq 0 ]
then
	cp -p $PKGDIR/$PKG_STREAM.bz2 $TMPDIR
	/usr/bin/bunzip2 $TMPDIR/$PKG_STREAM.bz2
	/usr/sbin/pkgadd -R $PATCH_INSTALL_ROOT -a $ADMIN -S -d $TMPDIR/$PKG_STREAM all > /dev/null 2>&1
	[ $? != 0 ] && echo "SUNWman failed to install." \
            && "Please check the filesystem for corruption." \
            && /usr/bin/rm -rf $TMPDIR \
            && exit 1
	[ -d "$TMPDIR" ] && /usr/bin/rm -rf $TMPDIR
	exit 0
else
	echo "All existing SUNWman package instances were not sucessfully removed"
	/usr/bin/rm -rf $TMPDIR
	exit 1
fi
