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


#set -x

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

# Make sure the revs count down so that they will be removed in the appropriate order
BADPATCHES="18 17 16"

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

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"

# Check for installed 6.2 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

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

  # PST requirement to check that the services are running
  GrabConfig $STATE_FILE "SERVER_PORT" "="
  NETWORK_PORT=$ANSWER
  GrabConfig $STATE_FILE "DEPLOY_ADMIN_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 patch release notes for more information."
      exit 1;
    fi
  fi
fi

# Create a temporary copy of the current start scripts 
# in case one of the postpatch rev_mods blocks wants to retain it
pkginfo -q SUNWpsgw
if [ $? -eq 0 ]; then
  GW_INSTALL_DIR=`$GREP "^BASEDIR=" /var/sadm/pkg/SUNWpsgw/pkginfo | cut -d= -f2`
  $CP $GW_INSTALL_DIR/SUNWps/bin/gateway /tmp/gateway.prepatch
fi

pkginfo -q SUNWpsrwp
if [ $? -eq 0 ]; then
  RWP_INSTALL_DIR=`$GREP "^BASEDIR=" /var/sadm/pkg/SUNWpsrwp/pkginfo | cut -d= -f2`
  $CP $RWP_INSTALL_DIR/SUNWps/bin/rwproxyd /tmp/rwproxyd.prepatch
fi

pkginfo -q SUNWpsnlp
if [ $? -eq 0 ]; then
  NLP_INSTALL_DIR=`$GREP "^BASEDIR=" /var/sadm/pkg/SUNWpsnlp/pkginfo | cut -d= -f2`
  $CP $NLP_INSTALL_DIR/SUNWps/bin/netletd /tmp/netletd.prepatch
fi

