#!/bin/ksh -hp

# Copyright (c) 2004 by Sun Microsystems, Inc.
# All rights reserved

# $1 - $RootDir
# current directory is the patch directory
#
# do a pkgadd if zfs pkgs are not present
#

#
# Use $2 for basedir if it exists
#
RootDir=${2:-$1}
PatchNum="`basename $PWD`"

ZFSPKGS="SUNWsmapi"

install_pkg() {
	pkg="$1"
	pkginfo -q -R $RootDir $pkg > /dev/null
	stat=$?
	if [ $stat -ne 0 ]; then
		#  pkg doesn't exist
		pkgadd -d `pwd`/payload -R $RootDir -a `pwd`/admin.quiet $pkg
		touch $RootDir/var/sadm/pkg/$ZFSPKGS/save/$ZFSPKGS.$PatchNum
	fi
	
}

verify_pkg(){	
	pkg="$1"
	#
	# if the VERSION is different between the existence
	# package on the system and the package of the patch,
	# installation will abbort.
	#
	patchdir=`pwd`
	
	pkg_version=`pkginfo -R $RootDir -l $pkg |grep VERSION|awk '{print $2}'`
	patch_version=`grep VERSION $patchdir/$pkg/pkginfo|sed 's/VERSION=//'`
	
	if [ "$pkg_version" != "$patch_version" ]
	then
		cat <<-eof
		Installed pkg $pkg is different from the
		one in this patch. They both must contain the same
		VERSION string as found in the pkginfo file.
	
		Patch install is aborting...
		eof
	
		exit 1
	fi
}

for zfspkg in $ZFSPKGS ; do
	install_pkg $zfspkg
	verify_pkg  $zfspkg
done

