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

trap 2

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

PROC=`uname -p`
if [ "$PROC" = "sparc" ]; then
  PATCHID="118263-17"
elif [ "$PROC" = "i386" ]; then
  PATCHID="118264-17"
fi
LOG=/dev/stdout # /dev/null or /dev/stdout
VERSION="PS6.3PC17"
#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
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

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

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

BELL_CHAR='\a'
pkginfo -q SUNWps
if [ $? -eq 0 ]; then

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

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

  GrabConfig $PS_STATE_FILE "IDSAME_BASEDIR" "="
  if [ "$ANSWER" != "" ]; then
     IS_INSTALL_DIR=$ANSWER
  else
     $ECHO "Error: Cannot determine IDSAME_BASEDIR. $BELL_CHAR"
     exit 1
  fi
fi # End of checking for Portal Server node

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

  GW_STATE_FILE="/etc/opt/SUNWps/GWConfig.properties"
  if [ ! -f $GW_STATE_FILE ]; then
    $ECHO "Error: $GW_STATE_FILE does not exist. $BELL_CHAR"
    exit 1
  fi

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

  GrabConfig $GW_STATE_FILE "IDSAME_BASEDIR" "="
  if [ "$ANSWER" != "" ]; then
     IS_INSTALL_DIR=$ANSWER
  else
     $ECHO "Error: Cannot determine IDSAME_BASEDIR. $BELL_CHAR"
     exit 1
  fi
fi # End of checking for Gateway node

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

  NLP_STATE_FILE="/etc/opt/SUNWps/NLPConfig.properties"
  if [ ! -f $NLP_STATE_FILE ]; then
    $ECHO "Error: $NLP_STATE_FILE does not exist. $BELL_CHAR"
    exit 1
  fi

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

  GrabConfig $NLP_STATE_FILE "IDSAME_BASEDIR" "="
  if [ "$ANSWER" != "" ]; then
     IS_INSTALL_DIR=$ANSWER
  else
     $ECHO "Error: Cannot determine IDSAME_BASEDIR. $BELL_CHAR"
     exit 1
  fi
fi # End of checking for NetletProxy node

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

  RWP_STATE_FILE="/etc/opt/SUNWps/RWPConfig.properties"
  if [ ! -f $RWP_STATE_FILE ]; then
    $ECHO "Error: $RWP_STATE_FILE does not exist. $BELL_CHAR"
    exit 1
  fi

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

  GrabConfig $RWP_STATE_FILE "IDSAME_BASEDIR" "="
  if [ "$ANSWER" != "" ]; then
     IS_INSTALL_DIR=$ANSWER
  else
     $ECHO "Error: Cannot determine IDSAME_BASEDIR. $BELL_CHAR"
     exit 1
  fi
fi # End of checking for RewriterProxy 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 SUNWps
    if [ $? -eq 0 ]; then
      VERSION_FILE=$PS_INSTALL_DIR/SUNWps/lib/PSversion.properties
      $ECHO "`$GREP -v $PATCHID $VERSION_FILE`" > $VERSION_FILE
    fi
    pkginfo -q SUNWpsgw
    if [ $? = 0 ]; then
      VERSION_FILE=$GW_INSTALL_DIR/SUNWps/lib/SRAversion.properties
      # Backout Fix for BugID #5006702
      $SED -e 's/productname=/product=/' $VERSION_FILE > /var/tmp/SRAversion.properties
      $SED -e 's/productversion=/version=/' /var/tmp/SRAversion.properties > $VERSION_FILE     
      $RM /var/tmp/SRAversion.properties
      # End of Backout for fix for BugID #5006702
      $ECHO "`$GREP -v $PATCHID $VERSION_FILE`" > $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
}

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

rollback_platform_conf() {
    for x in /etc/opt/SUNWps/platform\.conf\.*.pre$PATCHID; do
      if [ -r $x ]; then
        y=`echo $x | sed s/\.pre$PATCHID//`
        $MV $x $y
      fi
    done
}

rollback_gw_start_script() {
  pkginfo -q SUNWpsgw
  if [ $? -eq 0 ]; then
    GWSTARTFILE=$GW_INSTALL_DIR/SUNWps/bin/gateway

    $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

    if [ -f $GWSTARTFILE.pre$PATCHID ]; then
        $RM $GWSTARTFILE.pre$PATCHID
    fi
    if [ -f /etc/init.d/gateway.pre$PATCHID ]; then
        $RM /etc/init.d/gateway.pre$PATCHID
    fi
  fi
}

rollback_nlp_start_script() {
  pkginfo -q SUNWpsnlp
  if [ $? -eq 0 ]; then
    NLP_PROPERTIES=/etc/opt/SUNWps/NLPConfig.properties
    GrabConfig $NLP_PROPERTIES "NLP_BASEDIR" "="
    BASE_DIR=$ANSWER
    NLPSTARTFILE=$BASE_DIR/SUNWps/bin/netletd

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

    if [ -f $NLPSTARTFILE.pre$PATCHID ]; then
      $RM $NLPSTARTFILE.pre$PATCHID
    fi
  fi
}

rollback_rwp_start_script() {
  pkginfo -q SUNWpsrwp
  if [ $? -eq 0 ]; then
    RWP_PROPERTIES=/etc/opt/SUNWps/RWPConfig.properties
    GrabConfig $RWP_PROPERTIES "RWP_BASEDIR" "="
    BASE_DIR=$ANSWER
    RWPSTARTFILE=$BASE_DIR/SUNWps/bin/rwproxyd

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

    if [ -f $RWPSTARTFILE.pre$PATCHID ]; then
      $RM $RWPSTARTFILE.pre$PATCHID
    fi
  fi
}

# Restarting stand-alone processes
restart_processes() {
  pkginfo -q SUNWpsrwp
  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
}

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

  ## Rev01Mod01 Bug no: 5079713###

        TEMPFILE=/var/tmp/IMProvider$PATCHID.xml
        echo "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>" > $TEMPFILE
        echo "<!DOCTYPE DisplayProfile SYSTEM \"jar://resources/psdp.dtd\">" >> $TEMPFILE
        echo "<String name=\"contentPage\" value=\"IMContent.jsp\"/>" >> $TEMPFILE

        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 $TEMPFILE ] && [ $BIND_SUCCESS = "y" ]; then
                        $ECHO "Modifying the content Page value for IMProvider..."
                        $PS_INSTALL_DIR/SUNWps/bin/dpadmin modify -p IMProvider -u $BIND_DN -w $IS_ADMIN_PASSWORD -g $TEMPFILE > /dev/null 2>&1
                        if [ $? -ne 0 ]; then
                                $ECHO "Failed to modify the IMProvider... "
                        fi
                fi

                for x in `find /etc/opt/SUNWps/desktop -name "IMContent.jsp"`; do
			$MV $x.pre$PATCHID $x
                done
        fi

   ## End of Rev01Mod01  Bug no: 5079713###
   ##

  ## Rev01Mod03 Bug no : 5069157 ##

    pkginfo -q SUNWpssso

    if [ $? -eq 0 ]; then
	# cli utilities
	AMADMIN=$IS_INSTALL_DIR/SUNWam/bin/amadmin
	DPADMIN=$PS_INSTALL_DIR/SUNWps/bin/dpadmin

	# property file
	AMCONFIG_PROPS=$IS_INSTALL_DIR/SUNWam/lib/AMConfig.properties

	# retrieve admadmin dn and default org dn from property file
	ADMIN_DN=`$GREP "com.sun.identity.authentication.super.user" "$AMCONFIG_PROPS" | cut -d "=" -f2-`
	ROOT_DN=`$GREP "com.iplanet.am.rootsuffix" "$AMCONFIG_PROPS" | cut -d "=" -f2-`
	ORG_DN=`$GREP "com.iplanet.am.defaultOrg" "$AMCONFIG_PROPS" | cut -d "=" -f2-`

	if [ "$ORG_DN" != "$ROOT_DN" ]; then
            ORG_DN="$ORG_DN,$ROOT_DN"
	fi


        #
        # SUN-COMMS-EXPRESS-ADDRESS-BOOK SSO adapter template
        #

        TEMPFILE=/var/opt/SUNWps/tmp/ssoAdapterTemplGetDefaultValues.xml
        VALUESFILE=/var/opt/SUNWps/tmp/ssoAdapterTemplDefaultValues.xml
        MODVALUESFILE=/var/opt/SUNWps/tmp/ssoAdapterTemplModValues.xml

$CAT > $TEMPFILE <<EOF
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Requests
  PUBLIC "-//Sun ONE//iDSAME 5.0 Admin CLI DTD//EN "
  "jar://com/iplanet/am/admin/cli/amAdmin.dtd"
 >
<Requests>
    <SchemaRequests serviceName="SunSSOAdapterService" SchemaType="Global">
	<GetServiceDefaultValues>
		<Attribute name="sunConfigurationTemplates"/>
	</GetServiceDefaultValues>
    </SchemaRequests>
</Requests>
EOF

        $ECHO "Retrieving SSO adapter templates"
        /usr/bin/touch /var/opt/SUNWps/tmp/.is_admin_password
        $ECHO "$IS_ADMIN_PASSWORD" > /var/opt/SUNWps/tmp/.is_admin_password
        $AMADMIN -u "$ADMIN_DN" -f /var/opt/SUNWps/tmp/.is_admin_password -t $TEMPFILE > $VALUESFILE
        $RM /var/opt/SUNWps/tmp/.is_admin_password

$CAT <<EOF > $MODVALUESFILE
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Requests
  PUBLIC "-//Sun ONE//iDSAME 5.0 Admin CLI DTD//EN "
    "jar://com/iplanet/am/admin/cli/amAdmin.dtd"
 >
<Requests>
    <SchemaRequests serviceName="SunSSOAdapterService" SchemaType="Global">
	<RemovePartialDefaultValues>
            <AttributeValuePair>
		<Attribute name="sunConfigurationTemplates"/>
EOF
i=2
words=2
while [ $words -ge 1 ];do
    config=`cut -s -f$i -d "|" $VALUESFILE `
    words=`$ECHO $config | wc -w `
    i=`expr $i + 1`
    if [ $words -ge 1 ];then
	##//if it ends with ", default" remove these
	config=`$ECHO $config | sed -e 's/,\ default$//g'`
	##//if it ends with "]" remove it
	config=`$ECHO $config | sed -e 's/]$//g'`
	##//add "default |" at the begining
	config=`$ECHO $config | sed -e 's/^/default|/g'`

	exwords=`$ECHO $config | $AWK '/SUN-COMMS-EXPRESS-ADDRESS-BOOK/' | wc -w `
	if [ $exwords -ge 1 ];then
            ##//Append </Value>
            config=`$ECHO $config | sed -e 's/^/<Value>/g' `
            ##//Add </Value> at the end
            config=`$ECHO $config | sed -e 's/$/<\/Value>/g' `
            ##replace all the & by amp;
            config=`$ECHO $config|sed -e 's/\&/\&amp;/g' `
            ##replace all the & by amp;
            config=`$ECHO $config|sed -e 's/ \& /\&amp;/g' `
            ##//append it to a file
            $ECHO $config >> $MODVALUESFILE
	fi
    fi
done
$CAT <<EOF >> $MODVALUESFILE
            </AttributeValuePair>
	</RemovePartialDefaultValues>
    </SchemaRequests>
</Requests>
EOF

        $ECHO "Removing SSO adapter template SUN-COMMS-EXPRESS-ADDRESS-BOOK"
        /usr/bin/touch /var/opt/SUNWps/tmp/.is_admin_password
        $ECHO "$IS_ADMIN_PASSWORD" > /var/opt/SUNWps/tmp/.is_admin_password
        $AMADMIN -u "$ADMIN_DN" -f /var/opt/SUNWps/tmp/.is_admin_password -t $MODVALUESFILE
        $RM /var/opt/SUNWps/tmp/.is_admin_password


        #
        # sunCommsExpressAddressBook SSO adapter configuration default values
        #

        TEMPFILE=/var/opt/SUNWps/tmp/ssoAdapterGConfigGetDefaultValues.xml
        ORIVALUESFILE=/var/opt/SUNWps/tmp/ssoAdapterGConfigDefaultValuesOri.xml
        VALUESFILE=/var/opt/SUNWps/tmp/ssoAdapterGConfigDefaultValues.xml
        MODVALUESFILE=/var/opt/SUNWps/tmp/ssoAdapterGConfigModValues.xml

$CAT > $TEMPFILE <<EOF
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Requests
  PUBLIC "-//Sun ONE//iDSAME 5.0 Admin CLI DTD//EN "
  "jar://com/iplanet/am/admin/cli/amAdmin.dtd"
 >
<Requests>
    <SchemaRequests serviceName="SunSSOAdapterService" SchemaType="Dynamic">
	<GetServiceDefaultValues>
			<Attribute name="sunSSOAdapterConfigurations"/>
	 </GetServiceDefaultValues>
    </SchemaRequests>
</Requests>
EOF

        $ECHO "Retrieving SSO adapter configurations default values"
        /usr/bin/touch /var/opt/SUNWps/tmp/.is_admin_password
        $ECHO "$IS_ADMIN_PASSWORD" > /var/opt/SUNWps/tmp/.is_admin_password
        $AMADMIN -u "$ADMIN_DN" -f /var/opt/SUNWps/tmp/.is_admin_password -t $TEMPFILE > $ORIVALUESFILE
        $RM /var/opt/SUNWps/tmp/.is_admin_password
        $CAT $ORIVALUESFILE | $GREP -i sunSSOAdapterConfigurations > $VALUESFILE

$CAT <<EOF > $MODVALUESFILE
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Requests
  PUBLIC "-//Sun ONE//iDSAME 5.0 Admin CLI DTD//EN "
    "jar://com/iplanet/am/admin/cli/amAdmin.dtd"
 >
<Requests>
    <SchemaRequests serviceName="SunSSOAdapterService" SchemaType="Dynamic">
	<RemovePartialDefaultValues>
            <AttributeValuePair>
		<Attribute name="sunSSOAdapterConfigurations"/>
EOF
i=2
words=2
while [ $words -ge 1 ];do
    config=`cut -s -f$i -d "|" $VALUESFILE `
    words=`$ECHO $config | wc -w `
	i=`expr $i + 1`
    if [ $words -ge 1 ];then
	##//if it ends with ", default" remove these
	 config=`$ECHO $config | sed -e 's/,\ default$//g'`
	 ##//if it ends with "]" remove it
	 config=`$ECHO $config | sed -e 's/]$//g'`
	 ##//add "default |" at the begining
	 config=`$ECHO $config | sed -e 's/^/default|/g'`

	exwords=`$ECHO $config | $AWK '/sunCommsExpressAddressBook/' | wc -w `
	 if [ $exwords -ge 1 ];then
            ##//Append </Value>
            config=`$ECHO $config | sed -e 's/^/<Value>/g' `
            ##//Add </Value> at the end
            config=`$ECHO $config | sed -e 's/$/<\/Value>/g' `
            ##replace all the & by amp;
            config=`$ECHO $config|sed -e 's/\&/\&amp;/g' `
            ##replace all the & by amp;
            config=`$ECHO $config|sed -e 's/ \& /\&amp;/g' `
            ##//append it to a file
            $ECHO $config >> $MODVALUESFILE
	fi
    fi
done
$CAT <<EOF >> $MODVALUESFILE
            </AttributeValuePair>
	</RemovePartialDefaultValues>
    </SchemaRequests>
</Requests>
EOF

        $ECHO "Removing default SSO adapter configuration sunCommsExpressAddressBook"
        /usr/bin/touch /var/opt/SUNWps/tmp/.is_admin_password
        $ECHO "$IS_ADMIN_PASSWORD" > /var/opt/SUNWps/tmp/.is_admin_password
        $AMADMIN -u "$ADMIN_DN" -f /var/opt/SUNWps/tmp/.is_admin_password -t $MODVALUESFILE
        $RM /var/opt/SUNWps/tmp/.is_admin_password



        #
        # sunCommsExpressAddressBook SSO adapter configuration
        #

        TEMPFILE=/var/opt/SUNWps/tmp/ssoAdapterConfigGetDefaultValues.xml
        ORIVALUESFILE=/var/opt/SUNWps/tmp/ssoAdapterConfigDefaultValuesOri.xml
        VALUESFILE=/var/opt/SUNWps/tmp/ssoAdapterConfigDefaultValues.xml
        MODVALUESFILE=/var/opt/SUNWps/tmp/ssoAdapterConfigModValues.xml

$CAT > $TEMPFILE <<EOF
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Requests
  PUBLIC "-//Sun ONE//iDSAME 5.0 Admin CLI DTD//EN "
  "jar://com/iplanet/am/admin/cli/amAdmin.dtd"
 >
<Requests>
  <OrganizationRequests DN="$ORG_DN">
    <GetServiceTemplate serviceName="SunSSOAdapterService" schemaType="Dynamic"/>
  </OrganizationRequests>
</Requests>
EOF

        $ECHO "Retrieving SSO adapter configurations for $ORG_DN"
        /usr/bin/touch /var/opt/SUNWps/tmp/.is_admin_password
        $ECHO "$IS_ADMIN_PASSWORD" > /var/opt/SUNWps/tmp/.is_admin_password
        $AMADMIN -u "$ADMIN_DN" -f /var/opt/SUNWps/tmp/.is_admin_password -t $TEMPFILE > $ORIVALUESFILE
        $RM /var/opt/SUNWps/tmp/.is_admin_password
        $CAT $ORIVALUESFILE | $GREP -i sunSSOAdapterConfigurations > $VALUESFILE

$CAT <<EOF > $MODVALUESFILE
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Requests
  PUBLIC "-//Sun ONE//iDSAME 5.0 Admin CLI DTD//EN "
    "jar://com/iplanet/am/admin/cli/amAdmin.dtd"
 >
<Requests>
  <OrganizationRequests DN="$ORG_DN">
    <RemoveServiceTemplateAttributeValues serviceName="SunSSOAdapterService" schemaType="Dynamic" level="SCOPE_SUB">
	<AttributeValuePair>
	<Attribute name="sunSSOAdapterConfigurations"/>
EOF
i=2
words=2
while [ $words -ge 1 ];do
    config=`cut -s -f$i -d "|" $VALUESFILE `
    words=`$ECHO $config | wc -w `
    i=`expr $i + 1`
    if [ $words -ge 1 ];then
	##//if it ends with ", default" remove these
	 config=`$ECHO $config | sed -e 's/,\ default$//g'`
	 ##//if it ends with "]" remove it
	 config=`$ECHO $config | sed -e 's/]$//g'`
	 ##//add "default |" at the begining
	 config=`$ECHO $config | sed -e 's/^/default|/g'`

	exwords=`$ECHO $config | $AWK '/sunCommsExpressAddressBook/' | wc -w `
	 if [ $exwords -ge 1 ];then
	##//Append </Value>
	config=`$ECHO $config | sed -e 's/^/<Value>/g' `
	##//Add </Value> at the end
	config=`$ECHO $config | sed -e 's/$/<\/Value>/g' `
	##replace all the & by amp;
	config=`$ECHO $config|sed -e 's/\&/\&amp;/g' `
	##replace all the & by amp;
	config=`$ECHO $config|sed -e 's/ \& /\&amp;/g' `
	##//append it to a file
	$ECHO $config >> $MODVALUESFILE
        fi
    fi
done
$CAT <<EOF >> $MODVALUESFILE
	</AttributeValuePair>
	 </RemoveServiceTemplateAttributeValues>
    </OrganizationRequests>
</Requests>
EOF

        $ECHO "Removing organization SSO adapter configuration sunCommsExpressAddressBook"
        /usr/bin/touch /var/opt/SUNWps/tmp/.is_admin_password
        $ECHO "$IS_ADMIN_PASSWORD" > /var/opt/SUNWps/tmp/.is_admin_password
        $AMADMIN -u "$ADMIN_DN" -f /var/opt/SUNWps/tmp/.is_admin_password -t $MODVALUESFILE
        $RM /var/opt/SUNWps/tmp/.is_admin_password

        #
        # Provider and Channel
        #

        $ECHO "Removing CommsExpressAddressBookProvider Provider"
        $DPADMIN remove -u "$ADMIN_DN" -w "$IS_ADMIN_PASSWORD" -g -n CommsExpressAddressBookProvider -t provider

        $ECHO "Removing CommsExpressAddressBook channel"
        $DPADMIN remove -u "$ADMIN_DN" -w "$IS_ADMIN_PASSWORD" -d $ORG_DN -n CommsExpressAddressBook -t channel


        #
        # mobile access comms express address book setup
        #

        pkginfo -q SUNWpsmas

        if [ $? -eq 0 ]; then

            #
            # sunCommsExpressAddressBook Mobile Access Address Book Application
            # preferences default values
            #

            TEMPFILE=/var/opt/SUNWps/tmp/maABGConfigGetDefaultValues.xml
            ORIVALUESFILE=/var/opt/SUNWps/tmp/maABGConfigDefaultValuesOri.xml
            VALUESFILE=/var/opt/SUNWps/tmp/maABGConfigDefaultValues.xml
            MODVALUESFILE=/var/opt/SUNWps/tmp/maABGConfigModValues.xml

$CAT > $TEMPFILE <<EOF
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Requests
  PUBLIC "-//Sun ONE//iDSAME 5.0 Admin CLI DTD//EN "
  "jar://com/iplanet/am/admin/cli/amAdmin.dtd"
 >
<Requests>
    <SchemaRequests serviceName="SunMobileAppABService" SchemaType="Dynamic">
	<GetServiceDefaultValues>
		<Attribute name="sunMobileAppABConfig"/>
	</GetServiceDefaultValues>
    </SchemaRequests>
</Requests>
EOF

            $ECHO "Retrieving Mobile Access Address Book Application Preferences default values"
            /usr/bin/touch /var/opt/SUNWps/tmp/.is_admin_password
            $ECHO "$IS_ADMIN_PASSWORD" > /var/opt/SUNWps/tmp/.is_admin_password
            $AMADMIN -u "$ADMIN_DN" -f /var/opt/SUNWps/tmp/.is_admin_password -t $TEMPFILE > $ORIVALUESFILE
            $RM /var/opt/SUNWps/tmp/.is_admin_password
            $CAT $ORIVALUESFILE | $GREP -i sunMobileAppABConfig > $VALUESFILE

$CAT <<EOF > $MODVALUESFILE
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Requests
  PUBLIC "-//Sun ONE//iDSAME 5.0 Admin CLI DTD//EN "
    "jar://com/iplanet/am/admin/cli/amAdmin.dtd"
 >
<Requests>
    <SchemaRequests serviceName="SunMobileAppABService" SchemaType="Dynamic">
	<RemovePartialDefaultValues>
            <AttributeValuePair>
		<Attribute name="sunMobileAppABConfig"/>
EOF
i=2
words=2
while [ $words -ge 1 ];do
    config=`cut -s -f$i -d "|" $VALUESFILE `
    words=`$ECHO $config | wc -w `
	    i=`expr $i + 1`
    if [ $words -ge 1 ];then
	    ##//if it ends with ", default" remove these
	config=`$ECHO $config | sed -e 's/,\ default$//g'`
	##//if it ends with "]" remove it
	config=`$ECHO $config | sed -e 's/]$//g'`
	##//add "default |" at the begining
	config=`$ECHO $config | sed -e 's/^/default|/g'`

	exwords=`$ECHO $config | $AWK '/sunCommsExpressAddressBook/' | wc -w `
	 if [ $exwords -ge 1 ];then
            ##//Append </Value>
            config=`$ECHO $config | sed -e 's/^/<Value>/g' `
            ##//Add </Value> at the end
            config=`$ECHO $config | sed -e 's/$/<\/Value>/g' `
            ##replace all the & by amp;
            config=`$ECHO $config|sed -e 's/\&/\&amp;/g' `
            ##replace all the & by amp;
            config=`$ECHO $config|sed -e 's/ \& /\&amp;/g' `
            ##//append it to a file
            $ECHO $config >> $MODVALUESFILE
    	fi
    fi
done
$CAT <<EOF >> $MODVALUESFILE
            </AttributeValuePair>
	</RemovePartialDefaultValues>
    </SchemaRequests>
</Requests>
EOF

           $ECHO "Removing default Mobile Access Address Book Application Preferences for sunCommsExpressAddressBook"
           /usr/bin/touch /var/opt/SUNWps/tmp/.is_admin_password
           $ECHO "$IS_ADMIN_PASSWORD" > /var/opt/SUNWps/tmp/.is_admin_password
           $AMADMIN -u "$ADMIN_DN" -f /var/opt/SUNWps/tmp/.is_admin_password -t $MODVALUESFILE
           $RM /var/opt/SUNWps/tmp/.is_admin_password



           #
           # sunCommsExpressAddressBook Mobile Access Address Book
           # Application Preferences organization values
           #

           TEMPFILE=/var/opt/SUNWps/tmp/maABConfigGetDefaultValues.xml
           ORIVALUESFILE=/var/opt/SUNWps/tmp/maABConfigDefaultValuesOri.xml
           VALUESFILE=/var/opt/SUNWps/tmp/maABConfigDefaultValues.xml
           MODVALUESFILE=/var/opt/SUNWps/tmp/maABConfigModValues.xml

$CAT > $TEMPFILE <<EOF
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Requests
  PUBLIC "-//Sun ONE//iDSAME 5.0 Admin CLI DTD//EN "
  "jar://com/iplanet/am/admin/cli/amAdmin.dtd"
 >
<Requests>
  <OrganizationRequests DN="$ORG_DN">
    <GetServiceTemplate serviceName="SunMobileAppABService" schemaType="Dynamic"/>
  </OrganizationRequests>
</Requests>
EOF

          $ECHO "Retrieving Mobile Access Address Book Application Preferences for $ORG_DN"
          /usr/bin/touch /var/opt/SUNWps/tmp/.is_admin_password
          $ECHO "$IS_ADMIN_PASSWORD" > /var/opt/SUNWps/tmp/.is_admin_password
          $AMADMIN -u "$ADMIN_DN" -f /var/opt/SUNWps/tmp/.is_admin_password -t $TEMPFILE > $ORIVALUESFILE
          $RM /var/opt/SUNWps/tmp/.is_admin_password
          $CAT $ORIVALUESFILE | $GREP -i sunMobileAppABConfig > $VALUESFILE

$CAT <<EOF > $MODVALUESFILE
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Requests
  PUBLIC "-//Sun ONE//iDSAME 5.0 Admin CLI DTD//EN "
    "jar://com/iplanet/am/admin/cli/amAdmin.dtd"
 >
<Requests>
  <OrganizationRequests DN="$ORG_DN">
    <RemoveServiceTemplateAttributeValues serviceName="SunMobileAppABService" schemaType="Dynamic" level="SCOPE_SUB">
	<AttributeValuePair>
	<Attribute name="sunMobileAppABConfig"/>
EOF
i=2
words=2
while [ $words -ge 1 ];do
    config=`cut -s -f$i -d "|" $VALUESFILE `
    words=`$ECHO $config | wc -w `
    i=`expr $i + 1`
    if [ $words -ge 1 ];then
	##//if it ends with ", default" remove these
	 config=`$ECHO $config | sed -e 's/,\ default$//g'`
	 ##//if it ends with "]" remove it
	 config=`$ECHO $config | sed -e 's/]$//g'`
	 ##//add "default |" at the begining
	 config=`$ECHO $config | sed -e 's/^/default|/g'`

	exwords=`$ECHO $config | $AWK '/sunCommsExpressAddressBook/' | wc -w `
	 if [ $exwords -ge 1 ];then
	##//Append </Value>
	config=`$ECHO $config | sed -e 's/^/<Value>/g' `
	##//Add </Value> at the end
	config=`$ECHO $config | sed -e 's/$/<\/Value>/g' `
	##replace all the & by amp;
	config=`$ECHO $config|sed -e 's/\&/\&amp;/g' `
	##replace all the & by amp;
	config=`$ECHO $config|sed -e 's/ \& /\&amp;/g' `
	##//append it to a file
	$ECHO $config >> $MODVALUESFILE
        fi
    fi
done
$CAT <<EOF >> $MODVALUESFILE
	</AttributeValuePair>
	 </RemoveServiceTemplateAttributeValues>
    </OrganizationRequests>
</Requests>
EOF

          $ECHO "Removing organization Mobile Access Address Book Application Preferences for sunCommsExpressAddressBook"
          /usr/bin/touch /var/opt/SUNWps/tmp/.is_admin_password
          $ECHO "$IS_ADMIN_PASSWORD" > /var/opt/SUNWps/tmp/.is_admin_password
          $AMADMIN -u "$ADMIN_DN" -f /var/opt/SUNWps/tmp/.is_admin_password -t $MODVALUESFILE
          $RM /var/opt/SUNWps/tmp/.is_admin_password

        fi
    fi


  ## End of Rev01Mod03 Bug no : 5069157 ##




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

  ## Rev02Mod01 bug no. 5083578 ##

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

      SSO_ADMIN_DIR=$PS_INSTALL_DIR/SUNWps/web-src/ps/ssoadapteradmin

      if [ -f $SSO_ADMIN_DIR/AddProperty.jsp.pre$PATCHID ]; then
        $MV $SSO_ADMIN_DIR/AddProperty.jsp.pre$PATCHID $SSO_ADMIN_DIR/AddProperty.jsp 
        $CHMOD 644 $SSO_ADMIN_DIR/AddProperty.jsp
      fi

      if [ -f $SSO_ADMIN_DIR/EditTemplateProperties.jsp.pre$PATCHID ]; then
        $MV $SSO_ADMIN_DIR/EditTemplateProperties.jsp.pre$PATCHID $SSO_ADMIN_DIR/EditTemplateProperties.jsp 
        $CHMOD 644 $SSO_ADMIN_DIR/EditTemplateProperties.jsp 
      fi

      if [ -f $SSO_ADMIN_DIR/PropertyPickType.jsp.pre$PATCHID ]; then
        $MV $SSO_ADMIN_DIR/PropertyPickType.jsp.pre$PATCHID $SSO_ADMIN_DIR/PropertyPickType.jsp 
        $CHMOD 644 $SSO_ADMIN_DIR/PropertyPickType.jsp 
      fi

    fi
  ## End of Rev02Mod01 bug no. 5083578 ##

  #########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##############
   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
          $MV $files.pre$PATCHID $files
          $CHMOD 644 $files
        fi
      done

    fi

  #########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 == 6 ]]
  then
  #########Make changes for Rev06 here###########
  echo "Backing out changes for $PATCHBASE-06..."

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

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

  #########End of changes for Rev08##############
  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##############
  elif [[ $rev == 11 ]]
  then
  #########Make changes for Rev11 here###########
  echo "Backing out changes for $PATCHBASE-11..."

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

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

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

  #########End of changes for Rev14##############
  elif [[ $rev == 15 ]]
  then
  #########Make changes for Rev15 here###########
  echo "Backing out changes for $PATCHBASE-15..."

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

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

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

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

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

#######################################################################################
#
# 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
rollback_platform_conf
rollback_gw_start_script
rollback_rwp_start_script
rollback_nlp_start_script
restart_processes

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

trap ''
