#!/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 relocatability through the use of a client_root_path specified with the -R flag"
  exit 1;
fi

AWK=/usr/bin/awk
GREP=/usr/bin/grep
SED=/usr/bin/sed
UNIQ=/usr/bin/uniq
ECHO=/usr/bin/echo
CHMOD=/usr/bin/chmod
CUT=/usr/bin/cut

REQUIREDVERSION="6.3.1"
BELL_CHAR='\a'

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

  ANSWER=`$GREP "^$KEY$SEPARATOR" $FILE | $UNIQ | $SED -e "s/$KEY$SEPARATOR//"`
}

# Confirm this is a 6.3.1 system - replaces broken patch dependency resolution
CheckVersion() {
  local VERSIONFILE=$1
  local VERSIONKEY=$2

  if [ ! -f $VERSIONFILE ]; then
    $ECHO "Error: Can not determine installed Portal version - $VERSIONFILE does not exist. $BELL_CHAR"
    exit 1
  fi

  local INSTALLEDVERSION=`$GREP "^$VERSIONKEY" $VERSIONFILE | $CUT -d= -f2`
  if [ "$INSTALLEDVERSION" == "" ]; then
    $ECHO "Error: Can not determine installed Portal version. $BELL_CHAR"
    exit 1
  fi

  if [ "$INSTALLEDVERSION" != "$REQUIREDVERSION" ]; then
    $ECHO "Error: This patch can only be applied to Portal Server $REQUIREDVERSION. $BELL_CHAR"
    exit 1
  fi
}


# Version checking
pkginfo -q SUNWps
if [ $? -eq 0 ]; then

  PS_INSTALL_DIR=`pkginfo -l SUNWps | $GREP BASEDIR | $AWK '{ print $2 }'`

  CheckVersion $PS_INSTALL_DIR/SUNWps/lib/PSversion.properties productversion

fi # End of checking for Portal Server node

pkginfo -q SUNWpsgw
if [ $? -eq 0 ]; then

  GW_INSTALL_DIR=`pkginfo -l SUNWpsgw | $GREP BASEDIR | $AWK '{ print $2 }'`

  CheckVersion $GW_INSTALL_DIR/SUNWps/lib/SRAversion.properties version

fi # End of checking for Gateway node

pkginfo -q SUNWpsnlp
if [ $? -eq 0 ]; then

  NLP_INSTALL_DIR=`pkginfo -l SUNWpsnlp | $GREP BASEDIR | $AWK '{ print $2 }'`

  CheckVersion $NLP_INSTALL_DIR/SUNWps/lib/SRAversion.properties version

fi # End of checking for NetletProxy node

pkginfo -q SUNWpsrwp
if [ $? -eq 0 ]; then

  RWP_INSTALL_DIR=`pkginfo -l SUNWpsrwp | $GREP BASEDIR | $AWK '{ print $2 }'`

  CheckVersion $RWP_INSTALL_DIR/SUNWps/lib/SRAversion.properties version

fi # End of checking for RewriterProxy node

pkginfo -q SUNWps
if [ $? -eq 0 ]; then

  PS_STATE_FILE="/etc/opt/SUNWps/PSConfig.properties"
  if [ -f "$PS_STATE_FILE" ]
  then
    GrabConfig $PS_STATE_FILE "DEPLOY_TYPE" "="
    if [ "$ANSWER" != "" ]; then
      DEPLOY_TYPE=$ANSWER
    else
      $ECHO "Warning: Cannot determine DEPLOY_TYPE. $BELL_CHAR"
    fi
  fi

  # Permission fix for SUNWps
  if [ "$PS_INSTALL_DIR" != "" ]; then
    $CHMOD 755 $PS_INSTALL_DIR/SUNWps
  fi

  DEPLOY=$PS_INSTALL_DIR/SUNWps/bin/deploy

  if [ "$DEPLOY_TYPE" ]
  then
    $ECHO ""
    $ECHO "To complete the patch install, you must deploy"
    $ECHO "the updated web application using:"
    $ECHO "# $DEPLOY redeploy"
    $ECHO "and then restart your web container instances."
    $ECHO ""
  fi

  if [ "$DEPLOY_TYPE" = "IWS" ]; then

      GrabConfig $PS_STATE_FILE "DEPLOY_INSTANCE" "="
      if [ "$ANSWER" != "" ]; then
        DEPLOY_INSTANCE=$ANSWER
      else
        $ECHO "Warning: Cannot determine DEPLOY_INSTANCE. $BELL_CHAR"
        DEPLOY_INSTANCE="<deploy-instance>"
      fi

      GrabConfig $PS_STATE_FILE "DEPLOY_URI" "="
      if [ "$ANSWER" != "" ]; then
        DEPLOY_URI=$ANSWER
      else
        $ECHO "Warning: Cannot determine DEPLOY_URI. $BELL_CHAR"
        DEPLOY_URI="<deploy-uri>"
      fi

      $ECHO "WARNING: The deploy command redeploys content from $PS_INSTALL_DIR/SUNWps/web-src"
      $ECHO "to /var/opt/SUNWps/https-$DEPLOY_INSTANCE$DEPLOY_URI/web-apps"
      $ECHO "Any customisations to the Portal web application should always be made"
      $ECHO "to web-src and then deployed to web-apps. Ensure that any changes you"
      $ECHO "have made under web-apps have been replicated in web-src before"
      $ECHO "running deploy, or they will be lost."
      $ECHO ""
  fi
fi
