#!/bin/ksh
# Copyright 07/24/01 Sun Microsystems, Inc. All Rights Reserved.
# @(#)postpatch	1.11 07/24/01 Sun Microsystems

trap 2

#######################################################################################
#
# ENV vars which need to be modified
#

PROC=`uname -p`
if [ "$PROC" = "i386" ]; then
  PATCHID="117757-29"
else
  PATCHID="116856-29"
fi
LOG=/dev/stdout # /dev/null or /dev/stdout
VERSION="PS6.2PC29"
#set -x


#######################################################################################
#
# Static ENV vars 
#
ECHO=/usr/bin/echo
UMASK=/usr/bin/umask
CP=/usr/bin/cp
SED=/usr/bin/sed
UNIQ=/usr/bin/uniq
GREP=/usr/bin/grep
LS=/usr/bin/ls
BASENAME=/usr/bin/basename
CAT=/usr/bin/cat
CHMOD=/usr/bin/chmod
CHOWN=/usr/bin/chown
CP=/usr/bin/cp
CUT=/usr/bin/cut
DATE=/usr/bin/date
DIRNAME=/usr/bin/dirname
AWK=/usr/bin/awk
ENV=/usr/bin/env
GREP=/usr/bin/grep
ID=/usr/bin/id
LN=/usr/bin/ln
LS=/usr/bin/ls
MKDIR=/usr/bin/mkdir
MV=/usr/bin/mv
NAWK=/usr/bin/nawk
PKGINFO=/usr/bin/pkginfo
RM=/usr/bin/rm
STTY=/usr/bin/stty
TOUCH=/usr/bin/touch
GETFACL=/usr/bin/getfacl

/usr/bin/echo $PATH | /usr/bin/grep "/usr/java/bin[^/]" > /dev/null 2>&1
if [ $? -ne 0 ] ; then
  PATH="$PATH:/usr/java/bin"
  export PATH
fi
PATCHREV=`/usr/bin/echo ${PATCHID} | cut -d- -f2`
PATCHBASE=`/usr/bin/echo ${PATCHID} | cut -d- -f1`
HAS_IDENTITY_SUPER_ADMIN_PASSWORD="n"
HAS_DEPLOY_ADMIN_PASSWORD="n"

# Get value of a key in a flatfile and assign it to ANSWER

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

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

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

BELL_CHAR='\a'
STATE_FILE="/etc/opt/SUNWps/PSConfig.properties"
if [ ! -f $STATE_FILE ]; then
  $ECHO "Error: $STATE_FILE does not exist. $BELL_CHAR"
  exit 1
fi

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

if [ "$DEPLOY_TYPE" = "SUNONE" ]; then
  GrabConfig $STATE_FILE "DEPLOY_DIR" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_DIR=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_DIR. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $STATE_FILE "DEPLOY_DOMAIN" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_DOMAIN=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_DOMAIN. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $STATE_FILE "DEPLOY_INSTANCE" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_INSTANCE=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_INSTANCE. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $STATE_FILE "DEPLOY_ADMIN" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_ADMIN=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_ADMIN. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $STATE_FILE "DEPLOY_ADMIN_PORT" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_ADMIN_PORT=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_ADMIN_PORT. $BELL_CHAR"
    exit 1
  fi
else
  GrabConfig $STATE_FILE "DEPLOY_DIR" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_DIR=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_DIR. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $STATE_FILE "DEPLOY_INSTANCE" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_INSTANCE=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_INSTANCE. $BELL_CHAR"
    exit 1
  fi
fi

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

  GrabConfig $STATE_FILE "IDSAME_BASEDIR" "="
  if [ "$ANSWER" != "" ]; then
     IS_INSTALL_DIR=$ANSWER
  else
     $ECHO "Error: Cannot determine IDSAME_BASEDIR. $BELL_CHAR"
     exit 1
  fi

DEPLOY=$PS_INSTALL_DIR/SUNWps/bin/deploy
ASADMIN="$DEPLOY_DIR/bin/asadmin"

fi # End of checking for Portal Server node

pkginfo -q SUNWpsgw
if [ $? -eq 0 ]; then
  #May need to change this if DSAME and GW are installed on the same machine in different dirs
  GW_INSTALL_DIR=`$GREP "^BASEDIR=" /var/sadm/pkg/SUNWpsgw/pkginfo | cut -d= -f2`

fi

###############################################
# Set DEPLOY_INSTANCES
###############################################
SetDeployInstanceList() {
  local INSTANCE=""
  local INSTANCES=""
  local ADMIN_INSTANCE=""
  local TMP_INSTANCES=""

  if [ "$DEPLOY_INSTANCE" != "" ]; then
    DEPLOY_INSTANCES="$DEPLOY_INSTANCE"
  else
    if [ "$DEPLOY_TYPE" = "SUNONE" ]; then
      ADMIN_INSTANCE="admin-server"

      INSTANCES=""
      TMP_INSTANCES=`$LS -d $DEPLOY_DOMAIN/* 2>&1`
      if [ $? -eq 0 ]; then
        for INSTANCE in $TMP_INSTANCES; do
          INSTANCES="$INSTANCE $INSTANCES"
        done
      fi
    elif [ "$DEPLOY_TYPE" = "WEBLOGIC" ]; then
      ADMIN_INSTANCE=""

      INSTANCES=""
      if [ -f $STATE_FILE ]; then
        GrabConfig $STATE_FILE "DEPLOY_INSTANCE" "="
        INSTANCES=$ANSWER
      fi
    elif [ "$DEPLOY_TYPE" = "WEBSPHERE" ]; then
      ADMIN_INSTANCE=""

      INSTANCES=""
      if [ -f $STATE_FILE ]; then
        GrabConfig $STATE_FILE "DEPLOY_INSTANCE" "="
        INSTANCES=$ANSWER
      fi
    elif [ "$DEPLOY_TYPE" = "IWS" ]; then
      ADMIN_INSTANCE="admserv"

      INSTANCES=""
      TMP_INSTANCES=`$LS -d $DEPLOY_DIR/https-* 2>&1`
      if [ $? -eq 0 ]; then
        for INSTANCE in $TMP_INSTANCES; do
          INSTANCES="$INSTANCE $INSTANCES"
        done
      fi
    fi

    DEPLOY_INSTANCES=""
    for INSTANCE in $INSTANCES; do
      INSTANCE=`$BASENAME $INSTANCE | $SED -e "s/https-//"`
      if [ "$INSTANCE" != "$ADMIN_INSTANCE" ]; then
        DEPLOY_INSTANCES="$INSTANCE $DEPLOY_INSTANCES"
      fi
    done
  fi
}

##############################################################
#
# Util functions
#

insert_line() {
  file=$1
  match=$2
  new=$3

  #$CP $file $file-orig-$$
  $SED -e "
/$match/ {
i\\
$new
}" $file > $file-tmp
mv $file-tmp $file
}

replace_line() {
  file=$1
  match=$2
  new=$3

  #$CP $file $file-orig-$$
  $SED -e "
/$match/ {
c\\
$new
}" $file > $file-tmp
mv $file-tmp $file
}

updateVersionString() {
    VERSION="$VERSION,`/usr/bin/date`"
    pkginfo -q SUNWps
    if [ $? = 0 ]; then
      VERSION_FILE=$PS_INSTALL_DIR/SUNWps/lib/PSversion.properties
      $ECHO "patch=$PATCHID,$VERSION" >> $VERSION_FILE
    fi
    pkginfo -q SUNWpsgw
    if [ $? = 0 ]; then
      VERSION_FILE=$GW_INSTALL_DIR/SUNWps/lib/SRAversion.properties
      # Fix for BugID #5006702
        $SED -e 's/product=/productname=/' $VERSION_FILE > /var/tmp/SRAversion.properties
        $SED -e 's/version=/productversion=/' /var/tmp/SRAversion.properties > $VERSION_FILE     
        $RM /var/tmp/SRAversion.properties
      # End of fix for BugID #5006702
      $ECHO "patch=$PATCHID,$VERSION" >> $VERSION_FILE
    fi
}

# function test_bind checks to make sure that the password matches for the super
# admin bind DN set in the Identity config file.  Sets BIND_SUCCESS to y if the
# password given is correct

test_bind() {
  local PASSWD=$1
  GrabConfig "/etc/opt/SUNWps/PSConfig.properties" "IDSAME_BASEDIR" "="
  local IS_INST_DIR=$ANSWER
  GrabConfig "/etc/opt/SUNWps/PSConfig.properties" "BASEDIR" "="
  local PS_INST_DIR=$ANSWER
  local BIND_DN=`$GREP "com.sun.identity.authentication.super.user" "$IS_INST_DIR/SUNWam/lib/AMConfig.properties" | cut -d "=" -f2-`
  `$PS_INST_DIR/SUNWps/bin/rwadmin list --runasdn "$BIND_DN" --password "$PASSWD" | $GREP -i "Authorization Failed" > /dev/null 2>&1`
  if [ $? -eq 0 ]; then
    $ECHO "Authentication failed for user $BIND_DN..."
    BIND_SUCCESS=n
  else
    BIND_SUCCESS=y
  fi
}

GetDeployAdminPassword() {
DONE="n"
while [ "$DONE" = "n" ]; do
  $ECHO
  $ECHO "Please enter Application Server Administration Password: "
  $STTY -echo
  read PASSWORD
  $STTY echo
  if [ "$PASSWORD" != "" ]; then
    print "Again? $OMIT_CHAR"
    $STTY -echo
    read PASSWORD_REPEAT
    $STTY echo
    print ""
    if [ "$PASSWORD" != "$PASSWORD_REPEAT" ]; then
      print "Passwords do not match! $BELL_CHAR"
    else
      DONE="y"
    fi
  fi
done
HAS_DEPLOY_ADMIN_PASSWORD="y"
DEPLOY_ADMIN_PASSWORD=$PASSWORD
}

GetIdentitySuperAdminPassword() {
BIND_SUCCESS="n"
typeset -i fail_count=0
while (( $fail_count < 3 )) && [ $BIND_SUCCESS = "n" ]
do
  DONE="n"
  while [ "$DONE" = "n" ]; do
    $ECHO
    $ECHO "Please Enter Identity Server Super Administrator Password: "
    $STTY -echo
    read PASSWORD
    $STTY echo
    if [ "$PASSWORD" != "" ]; then
      print "Again? $OMIT_CHAR"
      $STTY -echo
      read PASSWORD_REPEAT
      $STTY echo
      print ""
      if [ "$PASSWORD" != "$PASSWORD_REPEAT" ]; then
        print "Passwords do not match! $BELL_CHAR"
      else
        DONE="y"
      fi
    fi
  done
  test_bind $PASSWORD
  ((fail_count=$fail_count + 1))
  if (( $fail_count == 3 )); then
    $ECHO "Failed to authenticate 3 times..."
  fi
done
IS_ADMIN_PASSWORD=$PASSWORD
HAS_IDENTITY_SUPER_ADMIN_PASSWORD="y"
}

rewrite_dsame_agent_key() {
    local REV=$1
    for x in `ls /etc/opt/SUNWps/platform\.conf\.* | grep -v pre`; do
      if [ -r $x ]; then
        $CP $x $x.pre$PATCHBASE-$REV
        # Fix for CR #6177963
        $ECHO "\n" >> $x
        $SED "s/gateway.dsame.agent=/gateway.dsame.agent.0=/g" $x > /$x.tmp
        $MV $x.tmp $x
      fi
    done
}

replace_gw_install_vars() {
  GWSTARTFILE=$GW_INSTALL_DIR/SUNWps/bin/gateway
  GW_PKG_INFO_FILE=/var/sadm/pkg/SUNWpsgw/pkginfo

  BASE_DIR=`grep "^BASEDIR"        $GW_PKG_INFO_FILE | cut -d= -f2 | $SED "s/\//\\\\\\\\\//g"`
  PROD_DIR=`grep "^PRODUCT_DIR"    $GW_PKG_INFO_FILE | cut -d= -f2 | $SED "s/\//\\\\\\\\\//g"`
  # Fix for nondefault IDSAME_BASEDIR value 
  if [ -f /etc/opt/SUNWps/GWConfig.properties ]; then
    GrabConfig /etc/opt/SUNWps/GWConfig.properties "IDSAME_BASEDIR" "="
    if [ "$ANSWER" != "" ]; then
      IDSB_DIR=$ANSWER
    else
      $ECHO "Error: Cannot determine IDSAME_BASEDIR. $BELL_CHAR"
    fi
  else
    IDSB_DIR=`grep "^IDSAME_BASEDIR" $GW_PKG_INFO_FILE | cut -d= -f2 | $SED "s/\//\\\\\\\\\//g"`
  fi

  $SED -e "s#\${BASE_DIR}#$BASE_DIR#g" \
       -e "s#\${PRODUCT_DIR}#$PROD_DIR#g" \
       -e "s#\${IDSAME_BASE_DIR}#$IDSB_DIR#g" $GWSTARTFILE > $GWSTARTFILE.$PATCHID.tmp

  $MV $GWSTARTFILE.$PATCHID.tmp $GWSTARTFILE
  $CHMOD 744 $GWSTARTFILE
  $CP $GWSTARTFILE /etc/init.d/gateway
  $CP $GWSTARTFILE /etc/rc0.d/K41gateway
  $CP $GWSTARTFILE /etc/rc1.d/K41gateway
  $CP $GWSTARTFILE /etc/rc2.d/K41gateway
  $CP $GWSTARTFILE /etc/rc3.d/S43gateway
  $CP $GWSTARTFILE /etc/rcS.d/K41gateway
  $CHMOD 755 $GWSTARTFILE

  $CP /tmp/gateway.prepatch $GWSTARTFILE.pre$PATCHID
  $CP /tmp/gateway.prepatch /etc/init.d/gateway.pre$PATCHID
  $ECHO 
  $ECHO "WARNING: A new gateway start script has been"
  $ECHO "installed in $GW_INSTALL_DIR/SUNWps/bin"
  $ECHO "You will need to reapply any changes or"
  $ECHO "tuning you previously made to this script."
  $ECHO "A copy of your old start script has been retained"
  $ECHO "for your reference as $GWSTARTFILE.pre$PATCHID"
  $ECHO 
}

replace_rwp_install_vars() {
  RWP_PKG_INFO_FILE=/var/sadm/pkg/SUNWpsrwp/pkginfo
  RWP_INSTALL_DIR=`$GREP "^BASEDIR=" $RWP_PKG_INFO_FILE | cut -d= -f2`
  RWPSTARTFILE=$RWP_INSTALL_DIR/SUNWps/bin/rwproxyd

  BASE_DIR=`grep "^BASEDIR"        $RWP_PKG_INFO_FILE | cut -d= -f2 | $SED "s/\//\\\\\\\\\//g"`
  PROD_DIR=`grep "^PRODUCT_DIR"    $RWP_PKG_INFO_FILE | cut -d= -f2 | $SED "s/\//\\\\\\\\\//g"`
  # Fix for nondefault IDSAME_BASEDIR value
  if [ -f /etc/opt/SUNWps/GWConfig.properties ]; then
    GrabConfig /etc/opt/SUNWps/GWConfig.properties "IDSAME_BASEDIR" "="
    if [ "$ANSWER" != "" ]; then
      IDSB_DIR=$ANSWER
    else
      $ECHO "Error: Cannot determine IDSAME_BASEDIR. $BELL_CHAR"
    fi
  else
    IDSB_DIR=`grep "^IDSAME_BASEDIR" $RWP_PKG_INFO_FILE | cut -d= -f2 | $SED "s/\//\\\\\\\\\//g"`
  fi

  $SED -e "s#\${BASE_DIR}#$BASE_DIR#g" \
       -e "s#\${PRODUCT_DIR}#$PROD_DIR#g" \
       -e "s#\${IDSAME_BASE_DIR}#$IDSB_DIR#g" $RWPSTARTFILE > $RWPSTARTFILE.$PATCHID.tmp

  $MV $RWPSTARTFILE.$PATCHID.tmp $RWPSTARTFILE
  $CP $RWPSTARTFILE /etc/init.d/rwproxyd
  $CHMOD 744 /etc/init.d/rwproxyd
  $CHMOD 755 $RWPSTARTFILE

  $CP /tmp/rwproxyd.prepatch $RWPSTARTFILE.pre$PATCHID
  $ECHO
  $ECHO "WARNING: A new rwproxyd start script has been"
  $ECHO "installed in $RWP_INSTALL_DIR/SUNWps/bin"
  $ECHO "You will need to reapply any changes or"
  $ECHO "tuning you previously made to this script."
  $ECHO "A copy of your old start script has been retained"
  $ECHO "for your reference as $RWPSTARTFILE.pre$PATCHID"
  $ECHO
}

replace_nlp_install_vars() {
  NLP_PKG_INFO_FILE=/var/sadm/pkg/SUNWpsnlp/pkginfo
  NLP_INSTALL_DIR=`$GREP "^BASEDIR=" $NLP_PKG_INFO_FILE | cut -d= -f2`
  NLPSTARTFILE=$NLP_INSTALL_DIR/SUNWps/bin/netletd

  BASE_DIR=`grep "^BASEDIR"        $NLP_PKG_INFO_FILE | cut -d= -f2 | $SED "s/\//\\\\\\\\\//g"`
  PROD_DIR=`grep "^PRODUCT_DIR"    $NLP_PKG_INFO_FILE | cut -d= -f2 | $SED "s/\//\\\\\\\\\//g"`
  # Fix for nondefault IDSAME_BASEDIR value
  if [ -f /etc/opt/SUNWps/GWConfig.properties ]; then
    GrabConfig /etc/opt/SUNWps/GWConfig.properties "IDSAME_BASEDIR" "="
    if [ "$ANSWER" != "" ]; then
      IDSB_DIR=$ANSWER
    else
      $ECHO "Error: Cannot determine IDSAME_BASEDIR. $BELL_CHAR"
    fi
  else
    IDSB_DIR=`grep "^IDSAME_BASEDIR" $NLP_PKG_INFO_FILE | cut -d= -f2 | $SED "s/\//\\\\\\\\\//g"`
  fi

  $SED -e "s#\${BASE_DIR}#$BASE_DIR#g" \
       -e "s#\${PRODUCT_DIR}#$PROD_DIR#g" \
       -e "s#\${IDSAME_BASE_DIR}#$IDSB_DIR#g" $NLPSTARTFILE > $NLPSTARTFILE.$PATCHID.tmp

  $MV $NLPSTARTFILE.$PATCHID.tmp $NLPSTARTFILE
  $CP $NLPSTARTFILE /etc/init.d/netletd
  $CHMOD 744 /etc/init.d/netletd
  $CHMOD 755 $NLPSTARTFILE

  $CP /tmp/netletd.prepatch $NLPSTARTFILE.pre$PATCHID
  $ECHO
  $ECHO "WARNING: A new netletd start script has been"
  $ECHO "installed in $NLP_INSTALL_DIR/SUNWps/bin"
  $ECHO "You will need to reapply any changes or"
  $ECHO "tuning you previously made to this script."
  $ECHO "A copy of your old start script has been retained"
  $ECHO "for your reference as $NLPSTARTFILE.pre$PATCHID"
  $ECHO
}

#######################################################################################
#
# Helper functions specific to handling previous patch revisions 
#

# function sort_arr sorts an array lexicographically and writes a global val NEW_ARR 
#   with the results

sort_arr() {
  set -s
  set -A NEW_ARR $*
}

# function call_rev_mods() calls rev_mods for every revision between the current 
#   revision on the system, and the revision of the patch to be installed

call_rev_mods() {
  typeset -i patch_rev=$1
  typeset -i cur_rev=$2

  while (($cur_rev < $patch_rev))
  do
    ((cur_rev=$cur_rev + 1))
    rev_mods $cur_rev;
  done
}

######################################################################################
#
# rev_mods handles all profile updates file manipulation and anything else which may
#   be required by the patch itself.  rev_mods is the workhorse of the patch install
#   script. 
#

rev_mods() {
  typeset -i rev=$1

  if [[ $rev == 1 ]]
  then
  ######## Make changes for Rev01 here ##########
  echo "Making changes for $PATCHBASE-01..."

  ######## End of changes for Rev01 #############
  elif [[ $rev == 2 ]]
  then
  ######## Make changes for Rev02 here ##########
  echo "Making changes for $PATCHBASE-02..."

  ######## End of changes for Rev02 #############
  elif [[ $rev == 3 ]]
  then
  ######## Make changes for Rev03 here ##########
  echo "Making changes for $PATCHBASE-03..."

  ######## End of changes for Rev03 #############
  elif [[ $rev == 4 ]]
  then
  ######## Make changes for Rev04 here ##########
  echo "Making changes for $PATCHBASE-04..."
  
  #==============================================
  #
  # REV04
  # Ensure that start scripts skip over
  # platform.conf files that have been backed
  # up by patch installs
  #
  #==============================================

  # No longer need to handle the start scripts
  # as they are replaced entirely in revs 17 and 18

  #==============================================
  #
  # REV04
  # Change file perms for new ruleset to match
  # portal user permissions. 
  #
  #==============================================

  DEFAULTRULESET=$PS_INSTALL_DIR/SUNWps/export/rewriter_rule/DefaultRuleSet.xml
  OWA2003RULESET=$PS_INSTALL_DIR/SUNWps/export/rewriter_rule/exchange_2003_owa_ruleset.xml
  if [ -f $DEFAULTRULESET ]; then
    PSUSER=`$GETFACL $DEFAULTRULESET | $GREP owner | $SED -n -e 's/ //gp' | $CUT -d ':' -f2`
    PSGROUP=`$GETFACL $DEFAULTRULESET | $GREP group | $SED -n -e 's/ //gp' | $CUT -d ':' -f2`
    if [ -f $OWA2003RULESET ]; then
      $CHOWN $PSUSER:$PSGROUP $OWA2003RULESET
    fi
  fi

  #==============================================
  #
  # REV04
  # Add the new OWA rewriter ruleset to Identity
  # using rwadmin CLI 
  #
  #==============================================

  pkginfo -q SUNWps
  if [ $? -eq 0 ]; then
    if [ $HAS_IDENTITY_SUPER_ADMIN_PASSWORD = "n" ]; then
      GetIdentitySuperAdminPassword
    fi
    BIND_DN=`$GREP "com.sun.identity.authentication.super.user" "$IS_INSTALL_DIR/SUNWam/lib/AMConfig.properties" | cut -d "=" -f2-`
    if [ -f $OWA2003RULESET ] && [ $BIND_SUCCESS = "y" ]; then
      $ECHO "Adding new RuleSet for Exchange 2003 OWA..."
      $PS_INSTALL_DIR/SUNWps/bin/rwadmin store --runasdn "$BIND_DN" --password "$IS_ADMIN_PASSWORD" $OWA2003RULESET > /dev/null 2>&1
      if [ $? -ne 0 ]; then
        $ECHO "Failed to upload new ruleset to the Identity Server: $OWA2003RULESET"
      fi
    fi 

  fi 


  ######## End of changes for Rev04 #############
  elif [[ $rev == 5 ]]
  then
  ######## Make changes for Rev05 here ##########
  echo "Making changes for $PATCHBASE-05..."

  ######## End of changes for Rev05 #############
  elif [[ $rev == 6 ]]
  then
  ######## Make changes for Rev06 here ##########
  echo "Making changes for $PATCHBASE-06..."

  ######## End of changes for Rev06 #############
  elif [[ $rev == 7 ]]
  then
  ######## Make changes for Rev07 here ##########
  echo "Making changes for $PATCHBASE-07..."

  ######## End of changes for Rev07 #############
  elif [[ $rev == 8 ]]
  then
  ######## Make changes for Rev08 here ##########
  echo "Making changes for $PATCHBASE-08..."

  #==============================================
  #
  # REV08
  # Change file perms for new ruleset to match
  # portal user permissions.
  #
  #==============================================

  DEFAULTRULESET=$PS_INSTALL_DIR/SUNWps/export/rewriter_rule/DefaultRuleSet.xml
  IDARULESET=$PS_INSTALL_DIR/SUNWps/export/rewriter_rule/iDARuleSet.xml
  if [ -f $DEFAULTRULESET ]; then
    PSUSER=`$GETFACL $DEFAULTRULESET | $GREP owner | $SED -n -e 's/ //gp' | $CUT -d ':' -f2`
    PSGROUP=`$GETFACL $DEFAULTRULESET | $GREP group | $SED -n -e 's/ //gp' | $CUT -d ':' -f2`
    if [ -f $IDARULESET ]; then
      $CHOWN $PSUSER:$PSGROUP $IDARULESET
    fi
  fi

  #==============================================
  #
  # REV08
  # Add the new iDA rewriter ruleset to Identity
  # using rwadmin CLI
  #
  #==============================================

  pkginfo -q SUNWps
  if [ $? -eq 0 ]; then
    if [ $HAS_IDENTITY_SUPER_ADMIN_PASSWORD = "n" ]; then
      GetIdentitySuperAdminPassword
    fi
    BIND_DN=`$GREP "com.sun.identity.authentication.super.user" "$IS_INSTALL_DIR/SUNWam/lib/AMConfig.properties" | cut -d "=" -f2-`
    if [ -f $IDARULESET ] && [ $BIND_SUCCESS = "y" ]; then
      $ECHO "Adding new RuleSet for Delegated Administrator..."
      $PS_INSTALL_DIR/SUNWps/bin/rwadmin store --runasdn "$BIND_DN" --password "$IS_ADMIN_PASSWORD" $IDARULESET > /dev/null 2>&1
      if [ $? -ne 0 ]; then
        $ECHO "Failed to upload new ruleset to the Identity Server: $IDARULESET"
      fi
    fi

  fi

  ######## End of changes for Rev08 #############
  elif [[ $rev == 9 ]]
  then
  ######## Make changes for Rev09 here ##########
  echo "Making changes for $PATCHBASE-09..."

  ######## End of changes for Rev09 #############
  elif [[ $rev == 10 ]]
  then
  ######## Make changes for Rev10 here ##########
  echo "Making changes for $PATCHBASE-10..."

  ######## End of changes for Rev10 #############
  elif [[ $rev == 11 ]]
  then
  ######## Make changes for Rev11 here ##########
  echo "Making changes for $PATCHBASE-11..."

  ######## End of changes for Rev11 #############
  elif [[ $rev == 12 ]]
  then
  ######## Make changes for Rev12 here ##########
  echo "Making changes for $PATCHBASE-12..."

  ######## End of changes for Rev12 #############
  elif [[ $rev == 13 ]]
  then
  ######## Make changes for Rev13 here ##########
  echo "Making changes for $PATCHBASE-13..."

  ######## End of changes for Rev13 #############
  elif [[ $rev == 14 ]]
  then
  ######## Make changes for Rev14 here ##########
  echo "Making changes for $PATCHBASE-14..."

  #==============================================
  #
  # REV14
  # Change file perms for new jar file
  #
  #==============================================
                 
  DEFAULTJAR=$PS_INSTALL_DIR/SUNWps/web-src/WEB-INF/lib/ssoadapter.jar
  HTTPCLIENTJAR=$PS_INSTALL_DIR/SUNWps/web-src/WEB-INF/lib/http_client.jar
  if [ -f $DEFAULTJAR ]; then
    PSUSER=`$GETFACL $DEFAULTJAR | $GREP owner | $SED -n -e 's/ //gp' | $CUT -d ':' -f2`
    PSGROUP=`$GETFACL $DEFAULTJAR | $GREP group | $SED -n -e 's/ //gp' | $CUT -d ':' -f2`
    if [ -f $HTTPCLIENTJAR ]; then
      $CHOWN $PSUSER:$PSGROUP $HTTPCLIENTJAR
    fi
  fi

  elif [[ $rev == 15 ]]
  then
  ######## Make changes for Rev15 here ##########
  echo "Making changes for $PATCHBASE-15..."

  ######## End of changes for Rev15 #############
  elif [[ $rev == 16 ]]
  then
  ######## Make changes for Rev16 here ##########
  echo "Making changes for $PATCHBASE-16..."

  ######## End of changes for Rev16 #############
  elif [[ $rev == 17 ]]
  then
  ######## Make changes for Rev17 here ##########
  echo "Making changes for $PATCHBASE-17..."

  #==============================================
  #
  # REV17
  # Rewrite gateway.dsame.agent in platform.conf
  #
  #==============================================

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

  ######## End of changes for Rev17 #############
  elif [[ $rev == 18 ]]
  then
  ######## Make changes for Rev18 here ##########
  echo "Making changes for $PATCHBASE-18..."

  ######## End of changes for Rev18 #############
  elif [[ $rev == 19 ]]
  then
  ######## Make changes for Rev19 here ##########
  echo "Making changes for $PATCHBASE-19..."

  ######## End of changes for Rev19 #############
  elif [[ $rev == 20 ]]
  then
  ######## Make changes for Rev20 here ##########
  echo "Making changes for $PATCHBASE-20..."

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

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

  ######## End of changes for Rev20 #############
  elif [[ $rev == 21 ]]
  then
  ######## Make changes for Rev21 here ##########
  echo "Making changes for $PATCHBASE-21..."
  

  ## Changes for bug# 6181556  in close-window.jsp
   FILE=$PS_INSTALL_DIR/SUNWps/web-src/close-windows.jsp
   if [ -f $FILE ]; then
   	$GREP "<%@ page session="false" %>" $FILE
  	if [ $? -ne 0 ]; then
              $CP $FILE $FILE.pre$PATCHID
  		insert_line $FILE  '<%@ page import="javax.servlet.ServletContext" %>' '<%@ page session="false" %>'
  	fi
   fi
  ## changes for bug# 6181556 ended.


  ######## End of changes for Rev21 #############

  elif [[ $rev == 22 ]]
  then
  ######## Make changes for Rev 22here ##########
  echo "Making changes for $PATCHBASE-22..."

  ######## End of changes for Rev22 #############
  elif [[ $rev == 23 ]]
  then
  ######## Make changes for Rev23 here##########
  echo "Making changes for $PATCHBASE-23..."

  ######## End of changes for Rev23 #############
  elif [[ $rev == 24 ]]
  then
  ######## Make changes for Rev24 here##########
  echo "Making changes for $PATCHBASE-24..."

  ######## End of changes for Rev24 #############

  elif [[ $rev == 25 ]]
  then
  ######## Make changes for Rev25 here##########
  echo "Making changes for $PATCHBASE-25..."

  ######## End of changes for Rev25 #############

  elif [[ $rev == 26 ]]
  then
  ######## Make changes for Rev26 here##########
  echo "Making changes for $PATCHBASE-26..."

  DPPROPS=/etc/opt/SUNWps/desktop/desktopconfig.properties
  $CP $DPPROPS $DPPROPS.pre$PATCHBASE-26

  $CAT <<EOF >> $DPPROPS
#
# Specify whether to rewrite the names of cookies retrieved by
# URLScraper channels. Cookies with matching names from
# different channels may be lost if this is set to false
rewriteURLScraperCookieNames=true

EOF
  ######## End of changes for Rev26 #############

  elif [[ $rev == 27 ]]
  then
  ######## Make changes for Rev27 here##########
  echo "Making changes for $PATCHBASE-27..."

  ######## End of changes for Rev27 #############

  elif [[ $rev == 28 ]]
  then
  ######## Make changes for Rev28 here##########
  echo "Making changes for $PATCHBASE-28..."

  PLATFORMFILE=/etc/opt/SUNWps/platform.conf.default
  if [ -f $PLATFORMFILE ]; then
    echo "Adding gateway.rewritebackslash to platform.conf..."
    for file in `ls /etc/opt/SUNWps/platform.conf* | grep -v orig | grep -v pre` 
    do
      if [ ! -f $file.pre$PATCHID ]; then
        $CP $file $file.pre$PATCHID
      fi
      $CAT <<EOF >> $file
#
# Specify whether to rewrite backslash URLs
gateway.rewritebackslash=true

EOF
    done
  fi

  ######## End of changes for Rev28 #############

  elif [[ $rev == 29 ]]
  then
  ######## Make changes for Rev29 here##########
  echo "Making changes for $PATCHBASE-29..."

  ######## End of changes for Rev29 #############

  ######## End of rev_mods huge if block#########
  fi
} # End of rev_mods

#######################################################################################
#
# Check for previous patch revisions.  If there are not any, then continue with profile
#    updates and flatfile manipulation.  For more than one patch revision, use another
#    'if' block so that the statements are not executed multiple times.  Check for the
#    highest rev first, and then only make changes from it to the current rev.
#    EX:  PATCHREV=04
#         if 03, then only apply changes for 04
#         if 02, then apply changes for 03, and 04
#         if 01, then apply changes for 02, 03, and 04
#         else, just apply changes for 04
#    
#    Note: These steps are necessary for the cummulative patch process to work correctly
#

# Function check_patch_revs checks for the existence of previous patch revisions for 
#    the current patch being installed.

check_patch_revs() {

  /usr/bin/echo "Checking for previous patch revisions..."

  # First get highest rev for the patch
  set -A CURRENT_REVS `showrev -p | /usr/bin/nawk ' { print substr($0, match($0, "Patch:")+7)} ' | \
  $SED 's/ Obsoletes:.*//g' | grep ${PATCHBASE} | /usr/bin/awk ' BEGIN { FS="-" } {print $2} '`

  typeset -i NUM_REVS=${#CURRENT_REVS[*]}

  # Check case where there may be no patch revisions installed

  if [[ $NUM_REVS > 1 ]]
  then 

    # Now sort it and get the highest rev currently installed
    sort_arr ${CURRENT_REVS[*]}

    HIGHEST_REV=${NEW_ARR[${NUM_REVS}-2]} 

    call_rev_mods ${PATCHREV} ${HIGHEST_REV}
  else
    call_rev_mods ${PATCHREV} '00'
  fi

} 

# Remove the temporary copy of the old gateway start script that prepatch creates
remove_tmp_scripts() {
    if [ -f /tmp/gateway.prepatch ]; then
          $RM -f /tmp/gateway.prepatch
    fi
    if [ -f /tmp/rwproxyd.prepatch ]; then
          $RM -f /tmp/rwproxyd.prepatch
    fi
    if [ -f /tmp/netletd.prepatch ]; then
          $RM -f /tmp/netletd.prepatch
    fi
}

#######################################################################################
#
# Main processing
#
check_patch_revs
updateVersionString

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

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

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

remove_tmp_scripts

#######################################################################################
#
# Restart the Server
#
# Restarting stand-alone processes

restart_processes() {
  pkginfo -q SUNWpsgw
  if [ $? -eq 0 ]; then
    $GW_INSTALL_DIR/SUNWps/bin/gateway start
  fi

  pkginfo -q SUNWpsnlp
  if [ $? -eq 0 ]; then
    $NLP_INSTALL_DIR/SUNWps/bin/netletd start
  fi

  pkginfo -q SUNWpsrwp
  if [ $? -eq 0 ]; then
    $RWP_INSTALL_DIR/SUNWps/bin/rwproxyd start
  fi
}

restart_processes

#######################################################################################
#
# Done
#
  $ECHO ""
  $ECHO "NOTE: To complete the patch install, you must deploy"
  $ECHO "the updated web application using:"
  $ECHO "# $DEPLOY redeploy -deploy_admin_password <password>"
  $ECHO "and then restart your web container instances."
  $ECHO ""


/usr/bin/echo ""
/usr/bin/echo "Postpatch processing complete."

trap ''
