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

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

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

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

BELL_CHAR='\a'

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

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

  PS_STATE_FILE="/etc/opt/SUNWps/PSConfig.properties"
  DEPLOY_TYPE=""
  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 backout, you must deploy"
    $ECHO "the restored 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

