osver=`uname -r`
patchlist=
finalsts=0
if [ $osver = "5.7" ]; then
  echo "Sun $osver is not a supported operating system version."
elif [ $osver = "5.8" ]; then
  echo "Sun $osver is not a supported operating system version."
  #patchlist="108434-14 111327-05 108991 108993-31 108528-29 113648-03 116602-01 111317-05 111023-03 115827-01"
elif [ $osver = "5.9" ]; then
  patchlist=
elif [ $osver = "5.10" ]; then
  patchlist=
fi

# Patch lines are of the form "Patch: x,y Obsoletes: a,b Requires: h,j Packages: c,d"
# we only want to check for our patch up to and including the Obsoletes: so we have
# to trim away the Requires onwards before we get our list of patches. eg. If we find the patch
# in either Patch: or Obsoletes: then we have the patch    
patchlines=`showrev -p | sed 's/Requires:.*//g' | sed 's/[A-Za-z,:]*//g'`

for p in $patchlist
do
  #look for exact match  
  echo "$patchlines" | /usr/xpg4/bin/grep "$p" > /dev/null
  sts=$?  
  if [ "$sts" -eq "0" ]; then
    echo "$PKG Required Patch $p found"
  else
    # look for superceded patch by splitting at the "-"
    # ie xxxx-yy is patch xxxx revision yy
    # look for all patch xxxx- patches and succeed if find one with part after - > yy
    patchid=`echo $p | sed -e "s/-[0-9]*//g"`
    patchrev=`echo $p | sed -e "s/[0-9]*-//g"`    
    echo "$patchlines" | /usr/xpg4/bin/grep "$patchid-" > /dev/null
    sts=$?
    if [ "$sts" -eq "0" ]; then # we haved one or more patches starting with $patchid-          
      # assume bad until proven otherwise
      sts=1
      for pl in $patchlines
      do
        #If we are the same patch base then compare the revision numbers and if we find
	#one that is greater then we have found what we want.        
        candidateid=`echo $pl | sed -e "s/-[0-9]*//g"`
	candidaterev=`echo $pl | sed -e "s/[0-9]*-//g"` 
        if [ "$candidateid" -eq "$patchid" ]; then # Same patch name
          if [ $candidaterev -gt $patchrev ]; then # found newer patch
            echo "$PKG Newer revision ($candidateid-$candidaterev) than required ($patchid-$patchrev) found"
            sts=0
	    break
          fi 
        fi 
      done
    fi # patches starting with $patchid-
  fi # no exact match
  if [ "$sts" -ne "0" ]; then
    echo "$PKG Required Patch $p missing"
    finalsts=1
  fi
done
if [ "$finalsts" -eq "0" ]; then
  echo "$PKG Required Patches Verified."
else
  #
  # patches missing section (finalsts != 0)
  #
  echo "$PKG 1 or more required patches not found."
  exit $finalsts
fi
