#!/bin/ksh
# @(#)postbackout       1.16 01/07/25 Sun Microsystems

trap 2

#######################################################################################
#
# Patch unique env vars...
#

PATCHID="121914-01"
VERSION="PS7.0p1"

#######################################################################################

LOG=/dev/stdout # /dev/null or /dev/stdout

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

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

BELL_CHAR='\a'
pkginfo -q SUNWportal-admin
if [ $? -eq 0 ]; then
  PS_INSTALL_DIR=`pkginfo -r SUNWportal-admin`
  IS_INSTALL_DIR=`pkginfo -r SUNWamsdk`
fi # end of checking for Portal Server node

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

restoreVersionString() {
    pkginfo -q SUNWportal-admin
    if [ $? -eq 0 ]; then
      VERSION_FILE=$PS_INSTALL_DIR/SUNWportal/lib/PSversion.properties
      $ECHO "`$GREP -v $PATCHID $VERSION_FILE`" > $VERSION_FILE
    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 $*
}

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

######################################################################################
#
# 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 "Backing out changes for $PATCHBASE-01..."

    $MV $IS_INSTALL_DIR/SUNWam/lib/AMConfig.properties.pre$PATCHID $IS_INSTALL_DIR/SUNWam/lib/AMConfig.properties
	
  #########End of changes for Rev01##############
  elif [[ $rev == 2 ]]
  then
  #########Make changes for Rev02 here###########
  echo "Backing out changes for $PATCHBASE-02..."

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

  #########End of changes for Rev03##############
  elif [[ $rev == 4 ]]
  then
  #########Make changes for Rev04 here###########
  echo "Backing out changes for $PATCHBASE-04..."

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

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

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

  #########End of changes for Rev10##############
  #########End of rev_mods huge if block#########
  fi
}

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

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

trap ''
