#!/bin/ksh
# Copyright 01/12/03 Sun Microsystems, Inc. All Rights Reserved.
# @(#)prepatch        1.15 01/12/03 Sun Microsystems


if [ ${ROOTDIR} != "/" ]; then
  echo "ERROR: This patch does not support alternate root install through the use of the '-R <client_root_path>' flag"
  exit 1;
fi

PROC=`uname -p`
if [ "$PROC" = "i386" ]; then
  PATCHBASE="118196"
else
  PATCHBASE="118195"
fi

GREP=/usr/bin/grep
SED=/usr/bin/sed
UNIQ=/usr/bin/uniq
CP=/usr/bin/cp

# Make sure the revs count down so that they will be removed in the appropriate order
BADPATCHES="03 02 01"

# Check for installed badpatches
for BADPATCH in $BADPATCHES; do
  /usr/sbin/patchadd -p | $GREP "${PATCHBASE}-${BADPATCH}" > /dev/null 2>&1
  if [ $? -eq 0 ]; then
    echo "It appears that you have patch ${PATCHBASE}-${BADPATCH} on this system which has been badpatched."
    echo "Please remove patch ${PATCHBASE}-${BADPATCH} prior to installing this patch".
    exit 1;
  fi
done

# Make sure that the core product has a JES2 patch 
/usr/bin/pkginfo -q SUNWps
if [ $? -eq 0 ]; then
  patchadd -p | grep "Patch: 11673[67]" > /dev/null 2>&1
  if [ $? -eq 1 ]; then
    echo "ERROR: Patch 116736-19 for SPARC or 116737-19 for i386 or later revision missing from Portal Server Core"
    exit 1;  
  fi 
fi

# Make sure the gateway has a JES2 patch
/usr/bin/pkginfo -q SUNWpsgw
if [ $? -eq 0 ]; then
  patchadd -p | grep "Patch: 11673[89]" > /dev/null 2>&1
  if [ $? -eq 1 ]; then
    echo "ERROR: Patch 116738-19 for SPARC or 116739-16 for i386 or later revision missing from Portal Server Gateway Node"  
    exit 1;
  fi
fi

# Make sure the rewriter proxy has a JES2 patch
/usr/bin/pkginfo -q SUNWpsrwp
if [ $? -eq 0 ]; then
  patchadd -p | grep "Patch: 11674[23]" > /dev/null 2>&1
  if [ $? -eq 1 ]; then
    echo "ERROR: Patch 116742-19 for SPARC or 116743-19 for i386 or later revision missing from Portal Server Rewriter Proxy Node"
    exit 1;
  fi
fi 

# Make sure the netlet proxy has a JES2 patch
/usr/bin/pkginfo -q SUNWpsnlp
if [ $? -eq 0 ]; then
  patchadd -p | grep "Patch: 11674[01]" > /dev/null 2>&1
  if [ $? -eq 1 ]; then
    echo "ERROR: Patch 116740-19 for SPARC or 116741-19 for i386 or later revision missing from Portal Server Netlet Proxy Node"
    exit 1;
  fi
fi

# Make sure that rewriter support has a JES2 patch
if [ "$PATCHBASE" = "118195" ]; then

   # Check for SUNWpsgws for SPARC
   /usr/bin/pkginfo -q SUNWpsgws
   if [ $? -eq 0 ]; then
     patchadd -p | grep "Patch: 116749" > /dev/null 2>&1
     if [ $? -eq 1 ]; then
       echo "ERROR: Patch 116749-19 or later revision missing from Portal Server SRA Support"
       exit 1;
     fi
   fi

else

   # Check for SUNWpsgws for i386
   /usr/bin/pkginfo -q SUNWpsgws
   if [ $? -eq 0 ]; then
     patchadd -p | grep "Patch: 116750" > /dev/null 2>&1
     if [ $? -eq 1 ]; then
       echo "ERROR: Patch 116750-19 or later revision missing from Portal Server SRA Support"
       exit 1;
     fi
   fi

fi

GREP=/usr/bin/grep
SED=/usr/bin/sed
UNIQ=/usr/bin/uniq

GrabConfig() {
  local FILE=$1
  local KEY=$2
  local SEPARATOR=$3

  ANSWER=`$GREP "^$KEY$SEPARATOR" $FILE | $UNIQ | $SED -e "s/$KEY$SEPARATOR//"`
}
STATE_FILE="/etc/opt/SUNWps/PSConfig.properties"

# Permission fix for 6.2PC4 badpatch
/usr/bin/pkginfo -q SUNWps
if [ $? -eq 0 ]; then
  GrabConfig $STATE_FILE "BASEDIR" "=" 
  PS_INSTALL_DIR=$ANSWER
  chmod 700 $PS_INSTALL_DIR/SUNWps

# PST requirement to check that the services are running
GrabConfig $STATE_FILE "SERVER_PORT" "="
NETWORK_PORT=$ANSWER
GrabConfig $STATE_FILE "SERVER_HOST" "="
DEPLOY_HOST=$ANSWER
if [ -f $PS_INSTALL_DIR/SUNWps/bin/checkport ]; then
  $PS_INSTALL_DIR/SUNWps/bin/checkport $DEPLOY_HOST $NETWORK_PORT 2
  if [ $? -eq 1 ]; then
    echo "One or more of the required services are not running."
    echo "Please start them before attempting to apply the patch."
    echo "See the Special Install Instructions section of the "
    echo "README for more information."
    exit 1;
  fi
fi

fi
