#!/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="118196-07"
else
  PATCHID="118195-07"
fi
LOG=/dev/stdout # /dev/null or /dev/stdout
VERSION="PS6.3SynchUp"
#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
SETFACL=/usr/bin/setfacl

/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} | /usr/bin/awk ' BEGIN { FS="-" } {print $2 }`
PATCHBASE=`/usr/bin/echo ${PATCHID} | /usr/bin/awk ' BEGIN { FS="-" } {print $1 }`
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
  GrabConfig "/etc/opt/SUNWps/GWConfig.properties" "GW_BASEDIR" "="
  GW_INSTALL_DIR=$ANSWER
  if [ .$IS_INSTALL_DIR = "." ]; then
    GrabConfig "/etc/opt/SUNWps/GWConfig.properties" "IDSAME_BASEDIR" "="
    IS_INSTALL_DIR=$ANSWER
  fi
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"
}

ReplaceMacrosTokens() {
  local FILES=$*
  for FILE in $FILES; do
    if [ -f $FILE ]; then
      $CP $FILE $FILE.pre$PATCHID
      $SED -e "s#%PS_BASEDIR%#$PS_INSTALL_DIR#g" \
           -e "s#%PS_PRODUCT_DIR%#$PS_PRODUCT_DIR#g" \
           -e "s#GATEWAY_IPADDR#$GW_IP#g" \
           -e "s#%PS_CONFIG_DIR%#$PS_CONFIG_DIR#g" \
           -e "s#SUNPORTALDESKTOPDEFAULTCHANNEL#JSPTabContainer#g" \
           -e "s#\[PS_CONFIG_DIR\]#$PS_CONFIG_DIR#g" \
           -e "s#%PS_VAR_DIR%#$PS_VAR_DIR#g" \
           -e "s#\${BASE_DIR}#$PS_INSTALL_DIR#g" \
           -e "s#\${IDSAME_BASE_DIR}#$IS_INSTALL_DIR#g" \
           -e "s#\${PRODUCT_DIR}#SUNWps#g" \
           -e "s#%IDSAME_VAR_DIR%#$IDSAME_VAR_DIR#g" $FILE > $FILE.tmp
      $MV $FILE.tmp $FILE
    fi
  done
}

#######################################################################################
#
# 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..."

  #==============================================
  #
  # REV01
  # Give new files appropriate default
  # ownership 
  #
  #============================================== 
  if [ "$PS_INSTALL_DIR" = "" ]; then
    PS_INSTALL_DIR=$GW_INSTALL_DIR
  fi
  NEWFILE=$PS_INSTALL_DIR/SUNWps/lib/psmacros
  COMPFILE=$PS_INSTALL_DIR/SUNWps/lib/config.jar
  if [ -f $NEWFILE ]; then
    $GETFACL $COMPFILE | $SETFACL -f - $NEWFILE
  fi
  NEWFILE=/etc/opt/SUNWps/desktop/desktopconfig.properties
  if [ -f $NEWFILE ]; then
    $GETFACL $COMPFILE | $SETFACL -f - $NEWFILE
  fi
  NEWFILE=$PS_INSTALL_DIR/SUNWps/bin/certadmin
  if [ -f $NEWFILE ]; then
    $GETFACL $COMPFILE | $SETFACL -f - $NEWFILE
    $CHMOD 755 $NEWFILE
  fi
  NEWFILE=$PS_INSTALL_DIR/SUNWps/bin/checkgw
  if [ -f $NEWFILE ]; then
    $GETFACL $COMPFILE | $SETFACL -f - $NEWFILE
    $CHMOD 755 $NEWFILE
  fi
  NEWFILE=$PS_INSTALL_DIR/SUNWps/bin/checkrp
  if [ -f $NEWFILE ]; then
    $GETFACL $COMPFILE | $SETFACL -f - $NEWFILE
    $CHMOD 755 $NEWFILE
  fi
  NEWFILE=$PS_INSTALL_DIR/SUNWps/bin/gateway
  if [ -f $NEWFILE ]; then
    $GETFACL $COMPFILE | $SETFACL -f - $NEWFILE
    $CHMOD 755 $NEWFILE
  fi
  NEWFILE=$PS_INSTALL_DIR/SUNWps/samples/MAP/dp-org-sample.xml
  if [ -f $NEWFILE ]; then
    $GETFACL $COMPFILE | $SETFACL -f - $NEWFILE
  fi
  NEWFILE=$PS_INSTALL_DIR/SUNWps/bin/checknetletd
  if [ -f $NEWFILE ]; then
    $GETFACL $COMPFILE | $SETFACL -f - $NEWFILE
    $CHMOD 755 $NEWFILE
  fi
  NEWFILE=$PS_INSTALL_DIR/SUNWps/bin/netletd
  if [ -f $NEWFILE ]; then
    $GETFACL $COMPFILE | $SETFACL -f - $NEWFILE
    $CHMOD 755 $NEWFILE
  fi
  NEWFILE=$PS_INSTALL_DIR/SUNWps/bin/rwproxyd
  if [ -f $NEWFILE ]; then
    $GETFACL $COMPFILE | $SETFACL -f - $NEWFILE
    $CHMOD 755 $NEWFILE
  fi
  NEWFILE=$GW_INSTALL_DIR/SUNWps/bin/changepasswd
  if [ -f $NEWFILE ]; then
    $GETFACL $COMPFILE | $SETFACL -f - $NEWFILE
    $CHMOD 755 $NEWFILE
  fi

  # Now that we have the Macros, we can source it to get access to the necessary
  # values we need to swap for other postpatch activities
  DIRNAME=/usr/bin/dirname
  . $PS_INSTALL_DIR/SUNWps/lib/psmacros

  #==============================================
  #
  # REV01
  # Replace Install Tokens w/ those found in 
  # psmacros
  #============================================== 
  MODFILE1=$PS_CONFIG_DIR/desktop/desktopconfig.properties
  MODFILE2=$GW_INSTALL_DIR/$PS_PRODUCT_DIR/web-src/WEB-INF/xml/gwsagent.xml
  MODFILE3=$PS_INSTALL_DIR/$PS_PRODUCT_DIR/web-src/WEB-INF/xml/rendering_apps.xml
  MODFILE4=$PS_INSTALL_DIR/$PS_PRODUCT_DIR/web-src/WEB-INF/xml/response_buffer_servlet.xml
  MODFILE5=$PS_INSTALL_DIR/$PS_PRODUCT_DIR/web-src/WEB-INF/xml/wireless_apps.xml
  MODFILE6=$PS_INSTALL_DIR/$PS_PRODUCT_DIR/web-src/WEB-INF/xml/wireless_help.xml
  MODFILE7=$PS_INSTALL_DIR/$PS_PRODUCT_DIR/web-src/WEB-INF/xml/portletadmin.xml
  MODFILE8=$PS_CONFIG_DIR/portlet/PDConfig.properties
  MODFILE9=$PS_INSTALL_DIR/$PS_PRODUCT_DIR/web-src/WEB-INF/xml/searchadmin.xml
  MODFILE10=$GW_INSTALL_DIR/$PS_PRODUCT_DIR/bin/checkgw
  MODFILE11=$GW_INSTALL_DIR/$PS_PRODUCT_DIR/bin/checkrp
  MODFILE12=$GW_INSTALL_DIR/$PS_PRODUCT_DIR/bin/gateway
  MODFILE13=$PS_INSTALL_DIR/$PS_PRODUCT_DIR/samples/MAP/dp-org-sample.xml
  MODFILE14=$PS_INSTALL_DIR/SUNWps/bin/netletd
  MODFILE15=$PS_INSTALL_DIR/SUNWps/bin/rwproxyd
  ReplaceMacrosTokens $MODFILE1 $MODFILE2 $MODFILE3 $MODFILE4 $MODFILE5 $MODFILE6 
  ReplaceMacrosTokens $MODFILE7 $MODFILE8 $MODFILE9 $MODFILE10 $MODFILE11 $MODFILE12
  ReplaceMacrosTokens $MODFILE13 $MODFILE14 $MODFILE15

  $CHMOD 755 $MODFILE10 $MODFILE11 $MODFILE12 $MODFILE14 $MODFILE15 > /dev/null 2>&1

  #==============================================
  #
  # REV01
  # CP new files to init.d startup dirs
  # 
  #============================================== 
  if [ -f $GW_INSTALL_DIR/$PS_PRODUCT_DIR/bin/gateway ]; then
    $CP $GW_INSTALL_DIR/$PS_PRODUCT_DIR/bin/gateway /etc/init.d/gateway
    $CHMOD 755 /etc/init.d/gateway
  fi
  if [ -f $PS_INSTALL_DIR/$PS_PRODUCT_DIR/bin/rwproxyd /etc/init.d/rwproxyd ]; then
    $CP $PS_INSTALL_DIR/$PS_PRODUCT_DIR/bin/rwproxyd /etc/init.d/rwproxyd
    $CHMOD 755 /etc/init.d/rwproxyd
  fi
  if [ -f $PS_INSTALL_DIR/$PS_PRODUCT_DIR/bin/netletd ]; then
    $CP $PS_INSTALL_DIR/$PS_PRODUCT_DIR/bin/netletd /etc/init.d/netletd
    $CHMOD 755 /etc/init.d/netletd  $PS_INSTALL_DIR/$PS_PRODUCT_DIR/bin/netletd
  fi 

  #==============================================
  #
  # REV01
  # Modify platform.conf entries
  # 
  #==============================================
  for confile in `ls $PS_CONFIG_DIR | grep platform.conf | grep -v pre | grep -v orig`;
  do
    $CP $PS_CONFIG_DIR/$confile $PS_CONFIG_DIR/$confile.pre$PATCHBASE-01
    echo "gateway.data.dir=$PS_VAR_DIR" >> $PS_CONFIG_DIR/$confile
    replace_line $PS_CONFIG_DIR/$confile "gateway.debug.dir" "gateway.debug.dir=$PS_VAR_DIR/debug"
  done

  #########End of changes for Rev02##############
  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..."
  ###############################################
  #########End of changes for Rev05##############
  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..."
  ###############################################
  #########End of changes for Rev08##############





  #########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)} ' | \
  /usr/bin/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

} 

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

#DEBUG
if [ 1 -ne 1 ]; then
#######################################################################################
#
# Restart the Server
#

pkginfo -q SUNWps
if [ $? -eq 0 ]; then
  /usr/bin/echo ""
  # redeploy
  $ECHO ""
  $ECHO "Redeploying Portal Web Services...."
  $DEPLOY redeploy > /dev/null 2>&1

  ## restart the instances of webserver and appserver
  SetDeployInstanceList
  if [ $DEPLOY_TYPE="IWS" ]; then
      $ECHO "Restarting webserver instances ..."
      if [ -f /etc/init.d/amserver ]; then
        /etc/init.d/amserver startall
      fi
      ########Commented out because portal web module is not deployed
      ########propertly when started in this manner from the script
      #for SERVER_INSTANCE in $DEPLOY_INSTANCES
      #  do
      #     $ECHO
      #     $ECHO "Instance --- $SERVER_INSTANCE"
      #     $DEPLOY_DIR/https-$SERVER_INSTANCE/stop
      #     $ECHO "DEBUG: Calling $DEPLOY_DIR/https-$SERVER_INSTANCE/start..."
      #     $DEPLOY_DIR/https-$SERVER_INSTANCE/start
      #  done
  elif [ $DEPLOY_TYPE="SUNONE" ]; then
       $ECHO "Restarting Appserver instances ..."
       for SERVER_INSTANCE in $DEPLOY_INSTANCES
        do
           $ECHO
           $ECHO "Instance --- $SERVER_INSTANCE"
           $DEPLOY_DOMAIN/$SERVER_INSTANCE/bin/stopserv
           $DEPLOY_DOMAIN/$SERVER_INSTANCE/bin/startserv
      done
  fi
fi

pkginfo -q SUNWpsgw
if [ $? -eq 0 ]; then
  $ECHO ""
  $ECHO "Restarting Portal Gateway..."
  /etc/init.d/gateway stop
  /etc/init.d/gateway start
  $ECHO "Gateway restarted.  Please wait a moment before connecting to it."
fi

fi # end debug

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

trap ''
