#!/bin/ksh

###############################################
# Test version
###############################################
TestVersion() {
  local PACKAGE="$1"

  /usr/bin/pkginfo -R ${ROOTDIR} -q $PACKAGE
  if [ $? -eq 1 ]; then
    /usr/bin/echo "$PACKAGE package not installed."
    /usr/bin/echo 'Aborting this patch.'
    exit 1
  fi

  ANSWER=`/usr/bin/pkgparam -R ${ROOTDIR} $PACKAGE SUNW_PRODVERS`
  OLD_IFS="$IFS"
  IFS='.'
  set $ANSWER
  MAJOR_VERSION=$1
  MINOR_VERSION=$2
  SUB_VERSION=${3:-0}
  IFS="$OLD_IFS"

  ABORT='n'

  if [ $MAJOR_VERSION -gt 6 ]; then
    ABORT='y'
  elif [ $MAJOR_VERSION -eq 6 ]; then
    if [ $MINOR_VERSION -gt 3 ]; then
      ABORT='y'
    elif [ $MINOR_VERSION -eq 3 ]; then
      if [ $SUB_VERSION -gt 1 ]; then
	ABORT='y'
      fi
    fi
  fi

  if [ "$ABORT" = 'y' ]; then
    /usr/bin/echo 'A newer version of Portal Server has been detected.'
    /usr/bin/echo 'Aborting this patch.'
    exit 1
  fi
}

###############################################
# Main
###############################################

INSTALLED_PATCHES=`/usr/sbin/patchadd -p -R ${ROOTDIR} | /usr/bin/cut -f2 -d' '`
/usr/bin/echo $INSTALLED_PATCHES | /usr/bin/grep '118128' > /dev/null 2>&1
if [ $? -eq 1 ]; then
  /usr/bin/echo $INSTALLED_PATCHES | /usr/bin/grep '118129' > /dev/null 2>&1
  if [ $? -eq 1 ]; then
    /usr/bin/echo 'Either patch 118128 (Sparc) or 118129 (x86) must be installed first.'
    /usr/bin/echo 'Aborting this patch.'
    exit 1
  fi
fi

TestVersion 'SUNWps'

exit 0

