#!/bin/sh
#

#
# See what revision of a patch is installed for a package
# Or what revision of the patch is obsoleted by another patch.
# Arguments: package patch
# Returns revision of specified patch
# Returns 0 if no patch or package not installed
#
# Assumptions: pkginfo is in /var/sadm/pkg
# Package's patches are listed in pkginfo in the PATCHLIST line
# Obsoleted patches are listed in a PATH_INFO line
#
get_patchrev()
{
	pkginfo="/var/sadm/pkg/$1/pkginfo"
	if [ ! -f $pkginfo ]; then
		# Package not installed
		return 0
	fi;

	# See if the patch is installed
	x=`grep "PATCHLIST" $pkginfo | \
		grep $2 | sed "s/.*$2-//" | sed 's/ .*//'`
	if [ "$x" -ne 0 ]; then
		return $x;
	fi;

	# See if the patch has been obsoleted by another patch
	x=`grep "^PATCH_INFO" $pkginfo | sed 's/.*Obsoletes://' | \
		sed 's/Requires.*//' | \
		grep $2 | sed "s/.*$2-//" | sed 's/ .*//'`
	if [ "$x" -ne 0 ]; then
		return $x;
	fi;

	# Patch is not installed or obsoleted
	return 0;
}

# Check that the patch dependencies are satisfied
# The dependency rules are too complex for a hard
# dependency in the patch, so we test here.
# In particular, for Solaris 8, patch 110648-14 or later must
# be installed.

OS_VERSION=`uname -r`
if [ $OS_VERSION = "5.8" ]; then
	get_patchrev SUNWscu 110648
	patchrev=$?
	if [ $patchrev -lt 14 ]; then
		echo "\n\n"
		echo "*** Patch Dependency is not satisfied"
		echo "Patch 110648-14 or later must be installed before installing OPS."
		echo "\n\n"
		exit 1
	fi;
fi;

exit 0
