#!/bin/ksh
#
# This patch requires several other OpenWindows patches
#
PATID=
PATVER=

#
#       There may be multiple SUNWcar packages installed (SUNWcar, SUNWcar.1,
#       and so on).  We must inspect each of them.
#
INFO="$ROOTDIR/var/sadm/pkg/SUNW*/pkginfo"

complain() {
    echo
    echo "This patch may not be installed on this version of Solaris without"
    echo "first installing version $PATVER or greater of patch $PATID."
    echo
}

newerver()
{
    PATID=$1
    PATVER=$2
    #
    #       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.*$PATID" $INFO |
            cut -d= -f2 | sort -u`
    do
            ver=`echo $patch | sed -e "s/.*$PATID-\([0-9]*\).*/\1/"`
            if [ $ver -ge $PATVER ]
            then
                    return 1
            fi
    done
    #
    #       Installed version of the patch is too small
    #       to contain
    #
    return 0
}

ERROR=0
for patch in 102057-42 106672-01 106671-01 101878-17
do
    PATID=`echo $patch | cut -d'-' -f1`
    PATVER=`echo $patch | cut -d'-' -f2`
#       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.*$PATID" $INFO
    then 
	if newerver $PATID $PATVER
	then
		ERROR=1
		complain
	fi
    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.*$PATID" $INFO
        then
                #
                #       Patch has been obsoleted.  Its changes,
                #       including the one we care about, must
                #       have been subsumed in the new patch.
                #
		echo
		echo "This patch has been obsoleted by a newer patch "
		echo "that is already installed."
		echo
                exit 0
        else
                #
                #       No patch obsoleting the one we depend
                #       on has been installed.
                #
                ERROR=1
                complain
        fi
    fi
done

exit $ERROR
