#!/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

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_STATE_FILE="/etc/opt/SUNWps/PSConfig.properties"
  if [ ! -f $PS_STATE_FILE ]; then
    $ECHO "Error: $PS_STATE_FILE does not exist. $BELL_CHAR"
    exit 1
  fi

  GrabConfig $PS_STATE_FILE "BASEDIR" "="
  if [ "$ANSWER" != "" ]; then
     PS_INSTALL_DIR=$ANSWER
  else
     $ECHO "Error: Cannot determine BASEDIR. $BELL_CHAR"
     exit 1
  fi

  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_STATE_FILE="/etc/opt/SUNWps/GWConfig.properties"
  if [ ! -f $GW_STATE_FILE ]; then
    $ECHO "Error: $GW_STATE_FILE does not exist. $BELL_CHAR"
    exit 1
  fi

  GrabConfig $GW_STATE_FILE "GW_BASEDIR" "="
  if [ "$ANSWER" != "" ]; then
     GW_INSTALL_DIR=$ANSWER
  else
     $ECHO "Error: Cannot determine GW_BASEDIR. $BELL_CHAR"
     exit 1
  fi

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

fi # End of checking for Gateway node

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

  NLP_STATE_FILE="/etc/opt/SUNWps/NLPConfig.properties"
  if [ ! -f $NLP_STATE_FILE ]; then
    $ECHO "Error: $NLP_STATE_FILE does not exist. $BELL_CHAR"
    exit 1
  fi

  GrabConfig $NLP_STATE_FILE "NLP_BASEDIR" "="
  if [ "$ANSWER" != "" ]; then
     NLP_INSTALL_DIR=$ANSWER
  else
     $ECHO "Error: Cannot determine NLP_BASEDIR. $BELL_CHAR"
     exit 1
  fi

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

fi # End of checking for NetletProxy node

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

  RWP_STATE_FILE="/etc/opt/SUNWps/RWPConfig.properties"
  if [ ! -f $RWP_STATE_FILE ]; then
    $ECHO "Error: $RWP_STATE_FILE does not exist. $BELL_CHAR"
    exit 1
  fi

  GrabConfig $RWP_STATE_FILE "RWP_BASEDIR" "="
  if [ "$ANSWER" != "" ]; then
     RWP_INSTALL_DIR=$ANSWER
  else
     $ECHO "Error: Cannot determine RWP_BASEDIR. $BELL_CHAR"
     exit 1
  fi

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

fi # End of checking for RewriterProxy node

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

  GrabConfig $PS_STATE_FILE "DEPLOY_TYPE" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_TYPE=$ANSWER
  else
    $ECHO "Warning: Cannot determine DEPLOY_TYPE. $BELL_CHAR"
  fi

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

  DEPLOY=$PS_INSTALL_DIR/SUNWps/bin/deploy

  $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 ""

  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
