#!/bin/ksh

complain()
{
#	$1 = PATCHID
#	$2 = PATCHREV
    echo
    echo "This patch may not be installed on this version of Solaris without"
    echo "first installing version $2 or greater of patch $1."
    echo
}

check_for_patch_install()
#	$1 = PATCHID
#	$2 = PATCHREV
{
	#       Look for the patch ID in either SUNW_PATCHID or
	#       SUNW_OBSOLETES (one at a time since they have
	#       different content).
	#
	if egrep -s "SUNW_PATCHID.*$1" $INFO
	then
		#
		#       Some version of the patch we depend on has
		#       been installed.  Extract the patch version
		#       number and see whether it's large enough.
		#
		for patch in `egrep "SUNW_PATCHID.*$1" $INFO |
			cut -d= -f2 | sort -u`
		do
                	ver=`echo $patch | sed -e "s/.*$1-\([0-9]*\).*/\1/"`
                	if [ $ver -ge $2 ]
                	then
				exit 0
			fi
		done
        	#
        	#       Installed version of the patch is too small
        	#       to contain
        	#
        		complain $1 $2
        		exit 1
		else
        	#
        	#       The patch version number is not guaranteed to
        	#       be present in the SUNW_OBSOLETES entry.  Look
        	#       only for the patch ID itself.
        	#
		if egrep -s "SUNW_OBSOLETES.*$1" $INFO
		then
			#
			#       Patch has been obsoleted.  Its changes,
			#       including the one we care about, must
			#       have been subsumed in the new patch.
			#
			exit 0
		else
			#
			#       No patch obsoleting the one we depend
			#       on has been installed.
			#
			complain $1 $2
			exit 1
		fi
	fi
}

if [ `uname -r` == "5.4" ] ;
then
	#
	# This patch requires the KJP 101946-50 and the
	# libnsl patch 101974-40 for Solaris 2.4
	#
	#	Each of these patches contain SUNWcsu package.
	#       There may be multiple SUNWcsu packages installed (SUNWcsu,
	#       SUNWcsu.1 and so on).  We must inspect each of them.
	#
	INFO="$ROOTDIR/var/sadm/pkg/SUNWcsu*/pkginfo"
	
	check_for_patch_install 101946 50
	check_for_patch_install 101974 40
	
fi

if [ `uname -r` == "5.5" ] ;
then
	#
	# This patch requires the libnsl patch 103188-44 for Solaris 2.5
	#
	#	This patch contains SUNWcsu package.
	#       There may be multiple SUNWcsu packages installed (SUNWcsu,
	#       SUNWcsu.1 and so on).  We must inspect each of them.
	#
	INFO="$ROOTDIR/var/sadm/pkg/SUNWcsu*/pkginfo"

	check_for_patch_install 103188 44
fi

if [ `uname -r` == "5.5.1" ] ;
then
	#
	# This patch requires the libnsl patch 103641-33 for Solaris 2.5.1
	#
	#	This patch contains SUNWcsu package.
	#       There may be multiple SUNWcsu packages installed (SUNWcsu,
	#       SUNWcsu.1 and so on).  We must inspect each of them.
	#
	INFO="$ROOTDIR/var/sadm/pkg/SUNWcsu*/pkginfo"

	check_for_patch_install 103641 33
fi
exit 0
