#!/bin/ksh 
# Copyright 10/15/2002 Sun Microsystems, Inc. All Rights Reserved.

trap 2

###############################################
# ENV vars which need to be modified
###############################################
PATCHID="116411-02"

###############################################
# Static ENV vars 
###############################################
ECHO=/usr/bin/echo
GREP=/usr/bin/grep
PKGINFO=/usr/bin/pkginfo
SED=/usr/bin/sed
CP=/usr/bin/cp
RM=/usr/bin/rm
MV=/usr/bin/mv
LS=/usr/bin/ls
CAT=/usr/bin/cat
CHOWN=/usr/bin/chown
CHMOD=/usr/bin/chmod
AWK=/usr/bin/awk
NAWK=/usr/bin/nawk
FIND=/usr/bin/find
pkginfo -q SUNWps
if [ $? -eq 0 ]; then
  INSTALL_DIR=`$GREP "BASEDIR=" /var/sadm/pkg/SUNWps/pkginfo | $GREP -v DEPLOY | $GREP -v IDSAME | $AWK ' BEGIN { FS="=" } { print $2 }`
  DEPLOY_URI=`$GREP "DEPLOY_URI=" /var/sadm/pkg/SUNWps/pkginfo | $AWK ' BEGIN { FS="=" } { print $2 }`
  DEPLOY_DOMAIN=`$GREP "DEPLOY_DOMAIN=" /var/sadm/pkg/SUNWps/pkginfo | $AWK ' BEGIN { FS="=" } { print $2 }`
  DEPLOY_INSTANCE=`$GREP "DEPLOY_INSTANCE=" /var/sadm/pkg/SUNWps/pkginfo | $AWK ' BEGIN { FS="=" } { print $2 }`
  DEPLOY_TYPE=`$GREP "DEPLOY_TYPE=" /var/sadm/pkg/SUNWps/pkginfo | $AWK ' BEGIN { FS="=" } { print $2 }`
fi
pkginfo -q SUNWpsgw
if [ $? -eq 0 ]; then
  INSTALL_DIR=`$GREP "SRAP_BASEDIR=" /var/sadm/pkg/SUNWpsgw/pkginfo | $AWK ' BEGIN { FS="=" } { print $2 }`
fi
PATCHREV=`$ECHO ${PATCHID} | $AWK ' BEGIN { FS="-" } {print $2}`
PATCHBASE=`$ECHO ${PATCHID} | $AWK ' BEGIN { FS="-" } {print $1}`


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

restoreVersionString() {
  VERSION_FILE=$INSTALL_DIR/SUNWps/lib/PSversion.properties
  if [ -f $VERSION_FILE ]; then
    $ECHO "`$GREP -v $PATCHID $VERSION_FILE`" > $VERSION_FILE 
  fi
  VERSION_FILE2=$INSTALL_DIR/SUNWps/lib/SRAversion.properties
  if [ -f $VERSION_FILE2 ]; then
    $ECHO "`$GREP -v $PATCHID $VERSION_FILE2`" > $VERSION_FILE2
  fi
}

redeploy() {
$ECHO "Restoring Portal web service with original files..."
if [ $DEPLOY_TYPE = "IWS" ]; then
  for INSTANCE in `$LS $INSTALL_DIR/SUNWps/web-apps | $GREP -v pre$PATCHID`
  do
    if [ -d $INSTALL_DIR/SUNWps/web-apps/$INSTANCE.pre$PATCHID ]; then
      $RM -rf $INSTALL_DIR/SUNWps/web-apps/$INSTANCE
      $MV $INSTALL_DIR/SUNWps/web-apps/$INSTANCE.pre$PATCHID $INSTALL_DIR/SUNWps/web-apps/$INSTANCE
    fi
  done
elif [ $DEPLOY_TYPE = "SUNONE" ]; then
  $RM -rf $DEPLOY_DOMAIN
  $MV $DEPLOY_DOMAIN.pre$PATCHID $DEPLOY_DOMAIN
else
  $ECHO "Unsupported or unknown DEPLOY TYPE: $DEPLOY_TYPE..."
fi

}


###############################################
# 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 $*
}

# 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 ins tall script.

rev_mods() {
  typeset -i rev=$1

  if [[ $rev == 1 ]]
  then
    $ECHO "Backing out changes for $PATCHBASE-01..."
    #==============================================
    #
    # REV01
    # Backout changes to version handling
    #
    #==============================================
    $ECHO "Restoring gateway start file..."
    GWSTARTFILE=/etc/init.d/gateway
    if [ -f $GWSTARTFILE.pre$PATCHID ]; then
      $MV $GWSTARTFILE.pre$PATCHID $GWSTARTFILE
      if [ -f $INSTALL_DIR/SUNWps/bin/gateway.pre$PATCHID ]; then
        $MV $INSTALL_DIR/SUNWps/bin/gateway.pre$PATCHID $INSTALL_DIR/SUNWps/bin/gateway
      fi
    fi
    VERSIONSCRIPT=$INSTALL_DIR/SUNWps/bin/version
    if [ -f $VERSIONSCRIPT.pre$PATCHID ]; then
	$ECHO "Restoring version script..."
      $MV $VERSIONSCRIPT.pre$PATCHID $VERSIONSCRIPT
    fi
    #==============================================
    #
    # REV01
    # Backout platform.conf entries added for BugIDs 
    # #4841043, and #4880860
    #
    #==============================================
    PLATFORMFILE=/etc/opt/SUNWps/platform.conf.default
    if [ -f $PLATFORMFILE ]; then
      $ECHO "Backing out changes made to platform.conf files..."
      for files in `ls /etc/opt/SUNWps/platform.conf* | grep -v platform.conf-orig`
      do
        if [ -f $files.pre$PATCHID ]; then
          /usr/bin/mv $files.pre$PATCHID $files
          /usr/bin/chmod 644 $files
        fi
      done
    fi
    #==============================================
    #
    # REV01
    # Backout srapGateway.properties entry for 
    # BugID #4880860
    #
    #==============================================
    PROPFILE=$INSTALL_DIR/SUNWps/locale/srapGateway.properties
    if [ -f $PROPFILE.pre$PATCHID ]; then
      /usr/bin/mv $PROPFILE.pre$PATCHID $PROPFILE
      /usr/bin/chmod 644 $PROPFILE
    fi
 

    
  elif [[ $rev == 2 ]]
  then
    $ECHO "Backing out changes for $PATCHBASE-02..."
  elif [[ $rev == 3 ]]
  then
    $ECHO "Backing out changes for $PATCHBASE-03..."
  else
    $ECHO "$rev: Unrecognized version number for patch base - $PATCHBASE"
  fi
}

# 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
# for backout, the order is reversed

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

  while (($orig_rev < $patch_rev))
  do
    rev_mods $patch_rev;
    ((patch_rev=$patch_rev - 1))
  done
}

# 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 a nother '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() {

  $ECHO "Checking for previous patch revisions..."

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

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

  # Check case where there may be no patch revisions installed
  if [[ $NUM_REVS > 0 ]]
  then
    # Now sort it and get the highest rev currently installed
    sort_arr ${CURRENT_REVS[*]}

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

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


###############################################
# Main processing
###############################################

check_patch_revs
restoreVersionString
pkginfo -q SUNWps
if [ $? -eq 0 ]; then
  redeploy
fi

###############################################
# Restart the Server
###############################################

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

  if [ $DEPLOY_TYPE = "IWS" ]; then
    $ECHO "Restarting SunONE Portal Server w/ original settings."
    /etc/init.d/amserver stop
    /etc/init.d/amserver start
  elif [ $DEPLOY_TYPE = "SUNONE" ]; then
    $ECHO "Restarting SunONE Portal Server on SunONE App Server instance $DEPLOY_INSTANCE w/ original settings."
    for INSTANCE in $INSTANCES
    do
      $ECHO
      $ECHO "Instance --- $INSTANCE"
      $DEPLOY_DOMAIN/$INSTANCE/bin/stopserv
      $DEPLOY_DOMAIN/$INSTANCE/bin/startserv
    done
  elif [ $DEPLOY_TYPE = "SUNONE" ]; then
    $ECHO "Do not know how to start yet!"
  else
    $ECHO "Do not know how to start yet!"
  fi

  $ECHO "Server restarted.  Please wait a moment before connecting to it."
fi


###############################################
# Restart the Gateway
###############################################

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


###############################################
# Done
###############################################
$ECHO ""
$ECHO "Postbackout processing complete."

trap ''
