#!/bin/ksh
# Copyright 07/25/01 Sun Microsystems, Inc. All Rights Reserved.
# @(#)postbackout       1.16 01/07/25 Sun Microsystems

trap 2

PATCHID="120725-01"
INSTALL_DIR=`/usr/bin/grep ips.basedir /etc/opt/SUNWips/platform.conf | cut -d= -f2`
PATCHREV=`/usr/bin/echo ${PATCHID} | cut -d- -f2`
PATCHBASE=`/usr/bin/echo ${PATCHID} | cut -d- -f1`

restoreVersionString() {
    VERSION_FILE=/etc/opt/SUNWips/.version
    if [ -f $VERSION_FILE-pre$PATCHID ]; then
        /usr/bin/mv $VERSION_FILE-pre$PATCHID $VERSION_FILE
    fi
}

rollback_jce() {
    TARGET=$INSTALL_DIR/SUNWips/bin/$1
    if [ -f $TARGET ]; then
        cp $TARGET $TARGET.$PATCHID.tmp
        sed s/jce1_2_2/jce1_2_1/ $TARGET.$PATCHID.tmp > $TARGET
        rm -f $TARGET.pre$PATCHID
        rm -f $TARGET.$PATCHID.tmp
    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..."

# Rollback change from jce1_2_1 to jce1_2_2

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

    SERVER_DIR=$INSTALL_DIR/netscape/server4
    cd $SERVER_DIR
    for server in `ls | grep "https-*" | cut -c7-`; do
        if [ "$server" != "admserv" ]; then
            JVM12="$SERVER_DIR/https-$server/config/jvm12.conf"
            cp $JVM12 $JVM12.$PATCHID.tmp
            sed s/jce1_2_2/jce1_2_1/ $JVM12.$PATCHID.tmp > $JVM12
            rm -f $JVM12.pre$PATCHID
            rm -f $JVM12.$PATCHID.tmp
        fi
    done
fi           
             
rollback_jce ipsadmin
rollback_jce ipshttpd
rollback_jce ipsnetletd
rollback_jce ipsgateway
rollback_jce ldapUpdate

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

else
  echo "$rev: Unrecognized version number for patch base - $PATCHBASE"
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


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

pkginfo -q SUNWwtsvd
if [ $? -eq 0 ]; then
  /usr/bin/echo ""
  /usr/bin/echo "Restarting iPS Server w/ original settings."
  /etc/init.d/ipsserver startall
  /usr/bin/echo "Server restarted.  Please wait a moment before connecting to it."
fi

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

pkginfo -q SUNWwtgwd
if [ $? -eq 0 ]; then
  /usr/bin/echo ""
  /usr/bin/echo "Restarting iPS Gateway w/ original settings."
  /etc/init.d/ipsgateway start
  /usr/bin/echo "Gateway restarted.  Please wait a moment before connecting to it."
fi

#######################################################################################
#
# Done
#

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

trap ''
