#!/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" = "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
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
HEAD=/usr/bin/head
TAIL=/usr/bin/tail

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

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
}

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
      if [ ! -f $FILE.pre$PATCHID ]; then
        $CP $FILE $FILE.pre$PATCHID
      fi
      $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
}

rewrite_dsame_agent_key() {
    if [ $DSAME_REWRITTEN != "Y" ]; then
      for CONFFILE in `ls /etc/opt/SUNWps/platform\.conf\.* | grep -v pre`; do
        if [ -r $CONFFILE ]; then
          GDA=`grep gateway.dsame.agent $CONFFILE | cut -d= -f2 | sed 's#\\\\##g'`
          $HEAD -1 $CONFFILE > $CONFFILE.$PATCHID.tmp
          $ECHO "gateway.dsame.agent.0=$GDA" >> $CONFFILE.$PATCHID.tmp
          $TAIL +2 $CONFFILE | $GREP -v 'gateway.dsame.agent' >> $CONFFILE.$PATCHID.tmp
          if [ ! -f $CONFFILE.pre$PATCHID ]; then
            $CP $CONFFILE $CONFFILE.pre$PATCHID
          fi
          $MV $CONFFILE.$PATCHID.tmp $CONFFILE
        fi
      done
      DSAME_REWRITTEN="Y"
    fi
}

add_gateway_identity_server() {
    if [ $IS_ADDED != "Y" ]; then
      IS_STATE_FILE="$IS_INSTALL_DIR/SUNWam/lib/AMConfig.properties"

      GrabConfig $IS_STATE_FILE "com.iplanet.am.server.protocol" "="
      IS_PROTOCOL=$ANSWER
      GrabConfig $IS_STATE_FILE "com.iplanet.am.server.host" "="
      IS_HOST=$ANSWER
      GrabConfig $IS_STATE_FILE "com.iplanet.am.server.port" "="
      IS_PORT=$ANSWER

      IS_URL="$IS_PROTOCOL://$IS_HOST:$IS_PORT/"

      for CONFFILE in `ls /etc/opt/SUNWps/platform\.conf\.* | grep -v pre`; do
        if [ -r $CONFFILE ]; then
          if [ ! -f $CONFFILE.pre$PATCHID ]; then
            $CP $CONFFILE $CONFFILE.pre$PATCHID
          fi
          $ECHO "gateway.identity.server.0=$IS_URL" >> $CONFFILE
        fi
      done
      IS_ADDED="Y"
    fi
}

replace_gw_install_vars() {
  GW_PROPERTIES=/etc/opt/SUNWps/GWConfig.properties

  PROD_DIR="SUNWps"
  GrabConfig $GW_PROPERTIES "GW_BASEDIR" "="
  BASE_DIR=$ANSWER
  GrabConfig $GW_PROPERTIES "IDSAME_BASEDIR" "="
  IDSB_DIR=$ANSWER
  GWSTARTFILE=$BASE_DIR/SUNWps/bin/gateway

  $SED -e "s#\${BASE_DIR}#$BASE_DIR#g" \
       -e "s#\${PRODUCT_DIR}#$PROD_DIR#g" \
       -e "s#\${IDSAME_BASE_DIR}#$IDSB_DIR#g" $GWSTARTFILE > $GWSTARTFILE.$PATCHID.tmp

  $MV $GWSTARTFILE.$PATCHID.tmp $GWSTARTFILE
  $CHMOD 744 $GWSTARTFILE
  $CP $GWSTARTFILE /etc/init.d/gateway
  $CHMOD 755 $GWSTARTFILE

  $CP /tmp/gateway.prepatch $GWSTARTFILE.pre$PATCHID
  $CP /tmp/gateway.prepatch /etc/init.d/gateway.pre$PATCHID
  $ECHO 
  $ECHO "WARNING: A new gateway start script has been"
  $ECHO "installed in $BASE_DIR/SUNWps/bin"
  $ECHO "You will need to reapply any changes or"
  $ECHO "tuning you previously made to this script."
  $ECHO "A copy of your old start script has been retained"
  $ECHO "for your reference as $GWSTARTFILE.pre$PATCHID"
  $ECHO 
}

replace_rwp_install_vars() {
  RWP_PROPERTIES=/etc/opt/SUNWps/RWPConfig.properties

  PROD_DIR="SUNWps"
  GrabConfig $RWP_PROPERTIES "RWP_BASEDIR" "="
  BASE_DIR=$ANSWER
  GrabConfig $RWP_PROPERTIES "IDSAME_BASEDIR" "="
  IDSB_DIR=$ANSWER
  RWPSTARTFILE=$BASE_DIR/SUNWps/bin/rwproxyd

  $SED -e "s#\${BASE_DIR}#$BASE_DIR#g" \
       -e "s#\${PRODUCT_DIR}#$PROD_DIR#g" \
       -e "s#\${IDSAME_BASE_DIR}#$IDSB_DIR#g" $RWPSTARTFILE > $RWPSTARTFILE.$PATCHID.tmp

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

  $CP /tmp/rwproxyd.prepatch $RWPSTARTFILE.pre$PATCHID
  $ECHO
  $ECHO "WARNING: A new rwproxyd start script has been"
  $ECHO "installed in $BASE_DIR/SUNWps/bin"
  $ECHO "You will need to reapply any changes or"
  $ECHO "tuning you previously made to this script."
  $ECHO "A copy of your old start script has been retained"
  $ECHO "for your reference as $RWPSTARTFILE.pre$PATCHID"
  $ECHO
}

replace_nlp_install_vars() {
  NLP_PROPERTIES=/etc/opt/SUNWps/NLPConfig.properties

  PROD_DIR="SUNWps"
  GrabConfig $NLP_PROPERTIES "NLP_BASEDIR" "="
  BASE_DIR=$ANSWER
  GrabConfig $NLP_PROPERTIES "IDSAME_BASEDIR" "="
  IDSB_DIR=$ANSWER
  NLPSTARTFILE=$BASE_DIR/SUNWps/bin/netletd

  $SED -e "s#\${BASE_DIR}#$BASE_DIR#g" \
       -e "s#\${PRODUCT_DIR}#$PROD_DIR#g" \
       -e "s#\${IDSAME_BASE_DIR}#$IDSB_DIR#g" $NLPSTARTFILE > $NLPSTARTFILE.$PATCHID.tmp

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

  $CP /tmp/netletd.prepatch $NLPSTARTFILE.pre$PATCHID
  $ECHO
  $ECHO "WARNING: A new netletd start script has been"
  $ECHO "installed in $BASE_DIR/SUNWps/bin"
  $ECHO "You will need to reapply any changes or"
  $ECHO "tuning you previously made to this script."
  $ECHO "A copy of your old start script has been retained"
  $ECHO "for your reference as $NLPSTARTFILE.pre$PATCHID"
  $ECHO
}

remove_tmp_scripts() {
  if [ -f /tmp/gateway.prepatch ]; then
      $RM -f /tmp/gateway.prepatch
  fi
  if [ -f /tmp/rwproxyd.prepatch ]; then
      $RM -f /tmp/rwproxyd.prepatch
  fi
  if [ -f /tmp/netletd.prepatch ]; then
      $RM -f /tmp/netletd.prepatch
  fi
}

replace_script_install_vars() {
  pkginfo -q SUNWpsgw
  if [ $? -eq 0 ]; then
    replace_gw_install_vars
  fi

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

  pkginfo -q SUNWpsnlp
  if [ $? -eq 0 ]; then
    replace_nlp_install_vars
  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

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

  ## 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
                	$CP $x $x.pre$PATCHID
                	$SED "s/request.getParameter(\"src_url\")/<%= request.getParameter(\"src_url\") %>/g" $x > $x.tmp
                	$MV $x.tmp $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

        # 
        # comms express sso adapter template and configuration
        #

        TEMPFILE="/var/tmp/sunSSOAdapterService.xml"
$CAT > $TEMPFILE <<EOF
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Requests
  PUBLIC "-//Sun ONE//iDSAME 5.0 Admin CLI DTD//EN "
  "file:$IS_INSTALL_DIR/SUNWam/dtd/amAdmin.dtd">
<Requests>
    <SchemaRequests serviceName="SunSSOAdapterService" SchemaType="Global">
        <AddDefaultValues>
            <AttributeValuePair>
                <Attribute name="sunConfigurationTemplates"/>
                <Value>default|http:///?configName=SUN-COMMS-EXPRESS-ADDRESS-BOOK&amp;default=enablePerRequestConnection&amp;default=clientProtocol&amp;default=ssoClassName&amp;default=subType&amp;default=type&amp;default=uwcContext&amp;merge=host&amp;merge=clientPort&amp;merge=uid&amp;merge=password&amp;merge=domain&amp;encoded=password&amp;clientProtocol=http&amp;enablePerRequestConnection=true&amp;ssoClassName=com.sun.ssoadapter.impl.WabpSSOAdapter&amp;subType=sun-one&amp;type=AB-TYPE&amp;uwcContext=uwc</Value>
            </AttributeValuePair>
        </AddDefaultValues>
    </SchemaRequests>
    <SchemaRequests serviceName="SunSSOAdapterService" SchemaType="Dynamic">
        <AddDefaultValues>
            <AttributeValuePair>
                <Attribute name="sunSSOAdapterConfigurations"/>
                <Value>default|undef:///?configName=sunCommsExpressAddressBook&amp;configDesc=SUN-COMMS-EXPRESS-ADDRESS-BOOK</Value>
            </AttributeValuePair>
        </AddDefaultValues>
    </SchemaRequests>
    <OrganizationRequests DN="$ORG_DN">
        <AddServiceTemplateAttributeValues serviceName="SunSSOAdapterService" schemaType="Dynamic">
            <AttributeValuePair>
                <Attribute name="sunSSOAdapterConfigurations"/>
                    <Value>default|undef:///?configName=sunCommsExpressAddressBook&amp;configDesc=SUN-COMMS-EXPRESS-ADDRESS-BOOK</Value>
            </AttributeValuePair>
        </AddServiceTemplateAttributeValues>
    </OrganizationRequests>
</Requests>
EOF

        $ECHO "Creating sso adapter template for comms express address book"
	/usr/bin/touch /var/opt/SUNWps/tmp/.is_admin_password
	$CHMOD 400 /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
	$RM /var/opt/SUNWps/tmp/.is_admin_password
        #$AMADMIN -u "$ADMIN_DN" -w "$IS_ADMIN_PASSWORD" -t $TEMPFILE

        # 
        # comms express address book provider
        #

        TEMPFILE="/var/tmp/commsExpressAddressBookProvider.xml"
$CAT > $TEMPFILE <<EOF
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE DisplayProfile SYSTEM "jar://resources/psdp.dtd">
<DisplayProfile version="1.0" priority="0">
<Properties/>
  <Channels/>
  <Providers>
    <Provider name="CommsExpressAddressBookProvider" class="com.sun.portal.providers.ab.AddressBookProvider" >
      <Properties>
        <String name="title"       value="Communications Express Address Book" />
        <String name="description" value="Sun Java System Communications Express Address Book" />
        <String name="width"       value="thick" />
        <String name="refreshTime" value="0" />
        <String name="productName" value="Sun JavaTM System Portal Server 6 2004Q2" />
        <String name="fontFace1"   value="Sanes-serif" />
        <String name="editType"    value="edit_subset" />
        <String name="sunPortalABSortBy" value="none" />
        <String name="sunPortalABSortOrder" value="none"/>
        <String name="ssoAdapter"  value="sunCommsExpressAddressBook" />
        <Integer name="maxEntries" value="30" />
        <Integer name="numEntries" value="5" />
        <Boolean name="isEditable" value="true" />
        <Boolean name="displayEntries" value="true" />
        <ConditionalProperties condition="client" value="HTML">
            <ConditionalProperties condition="locale" value="en">
                <String name="helpURL" value="en/desktop/addressbook.htm" />
            </ConditionalProperties>
            <String name="helpURL" value="en/desktop/addressbook.htm" />
            <Collection name="applicationHelperEdit">
              <String value="com.sun.portal.providers.ab.UWCABApplicationHelper" />
            </Collection>
            <String name="applicationHelperURL" value="com.sun.portal.providers.ab.UWCABApplicationHelper" />
        </ConditionalProperties>
        <Collection name="ssoEditAttributes">
          <String name="host" value="string|Host Name:"/>
          <String name="clientPort" value="string|Client Port:"/>
          <String name="uid" value="string|User Name:"/>
          <String name="password" value="password|User Password:"/>
          <String name="domain" value="string|User Domain:"/>
        </Collection>
        <Collection name="dpEditAttributes">
          <String name="displayEntries"       value="check|Display Entries: "/>
          <String name="numEntries"           value="int|Number of Entries:"/>
          <String name="sunPortalABSortBy"    value="select|Sort By:"/>
          <String name="sunPortalABSortOrder" value="select|Sort Order:"/>
        </Collection>
        <Collection name="sunPortalABSortBySelectOptions">
            <String name="NoSort"    value="None"/>
            <String name="Commonname"  value="Full name"/>
        </Collection>
        <Collection name="sunPortalABSortOrderSelectOptions">
          <String name="NoSort" value="None"/>
            <String name="Up"   value="Ascending"/>
            <String name="Down" value="Descending"/>
        </Collection>
      </Properties>
    </Provider>
  </Providers>
</DisplayProfile>
EOF

        $ECHO "Creating provider for comms express address book"
        $DPADMIN modify -m -u "$ADMIN_DN" -w "$IS_ADMIN_PASSWORD" -g $TEMPFILE

	#
        # comms express address book channel
	#

        TEMPFILE="/var/tmp/commsExpressAddressBookChannel.xml"
$CAT > $TEMPFILE <<EOF
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE DisplayProfile SYSTEM "jar://resources/psdp.dtd">
<DisplayProfile version="1.0" priority="10">
  <Properties/>
  <Channels>
    <Channel name="CommsExpressAddressBook" provider="CommsExpressAddressBookProvider">
      <Properties/>
    </Channel>
  </Channels>
  <Providers/>
</DisplayProfile>
EOF

   	$ECHO "Creating channel for comms express address book"
	$DPADMIN modify -m -u "$ADMIN_DN" -w "$IS_ADMIN_PASSWORD" -d "$ORG_DN" $TEMPFILE

        # 
        # mobile access comms express address book setup
        #

        pkginfo -q SUNWpsmas

        if [ $? -eq 0 ]; then

            # 
            # comms express sso adapter template and configuration
            #

            TEMPFILE="/var/tmp/commsMapAddressBookConfiguration.xml"
$CAT > $TEMPFILE <<EOF
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Requests
  PUBLIC "-//Sun ONE//iDSAME 5.0 Admin CLI DTD//EN "
  "file:$IS_INSTALL_DIR/SUNWam/dtd/amAdmin.dtd">
<Requests>
    <SchemaRequests serviceName="SunMobileAppABService" SchemaType="Dynamic">
        <AddDefaultValues>
            <AttributeValuePair>
                <Attribute name="sunMobileAppABConfig"/>
                <Value>default|undef:///?configName=sunCommsExpressAddressBook&amp;configDesc=MA-AB-APP</Value>
            </AttributeValuePair>
        </AddDefaultValues>
    </SchemaRequests>

    <OrganizationRequests DN="$ORG_DN">
        <AddServiceTemplateAttributeValues serviceName="SunMobileAppABService" schemaType="Dynamic">
            <AttributeValuePair>
                <Attribute name="sunMobileAppABConfig"/>
                <Value>default|undef:///?configName=sunCommsExpressAddressBook&amp;configDesc=MA-AB-APP</Value>
            </AttributeValuePair>
        </AddServiceTemplateAttributeValues>
    </OrganizationRequests>
</Requests>
EOF
            $ECHO "Creating mobile configuration for comms express address book"
            /usr/bin/touch /var/opt/SUNWps/tmp/.is_admin_password
            $CHMOD 400 /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
            $RM /var/opt/SUNWps/tmp/.is_admin_password
            #$AMADMIN -u "$ADMIN_DN" -w "$IS_ADMIN_PASSWORD" -t $TEMPFILE

            # 
            # map comms express address book provider
            #

            TEMPFILE="/var/tmp/mapCommsExpressAddressBookProvider.xml"
$CAT > $TEMPFILE <<EOF
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE DisplayProfile SYSTEM "jar://resources/psdp.dtd">
<Properties>
    <ConditionalProperties condition="client" value="HTML">
        <Collection name="applicationHelperEdit">
        <String value="com.sun.portal.wireless.providers.ab.MAJspABAppHelper" />
    </Collection>
    </ConditionalProperties>
    <ConditionalProperties condition="client" value="WML">
        <String name="applicationHelperURL" value="com.sun.portal.wireless.providers.ab.MAJspABAppHelper" />
        <Boolean name="isEditable" value="false" advanced="true"/>
    </ConditionalProperties>
    <ConditionalProperties condition="client" value="HDML">
        <String name="applicationHelperURL" value="com.sun.portal.wireless.providers.ab.MAJspABAppHelper" />
        <Boolean name="isEditable" value="false" advanced="true"/>
    </ConditionalProperties>
    <ConditionalProperties condition="client" value="JHTML">
        <String name="applicationHelperURL" value="com.sun.portal.wireless.providers.ab.MAJspABAppHelper" />
        <Boolean name="isEditable" value="false" advanced="true"/>
    </ConditionalProperties>
    <ConditionalProperties condition="client" value="XHTML">
        <String name="applicationHelperURL" value="com.sun.portal.wireless.providers.ab.MAJspABAppHelper" />
        <Boolean name="isEditable" value="false" advanced="true"/>
    </ConditionalProperties>
    <ConditionalProperties condition="client" value="cHTML">
        <String name="applicationHelperURL" value="com.sun.portal.wireless.providers.ab.MAJspABAppHelper" />
        <Boolean name="isEditable" value="false" advanced="true"/>
    </ConditionalProperties>
    <ConditionalProperties condition="client" value="iHTML">
        <String name="applicationHelperURL" value="com.sun.portal.wireless.providers.ab.MAJspABAppHelper" />
        <Boolean name="isEditable" value="false" advanced="true"/>
    </ConditionalProperties>
</Properties>
EOF
            $ECHO "Updating comms express address book provider for mobile access"
            $DPADMIN modify -m -u "$ADMIN_DN" -w "$IS_ADMIN_PASSWORD" -p CommsExpressAddressBookProvider -g $TEMPFILE

        fi
        # End mobile access comms express address book setup

    fi

  ## End of Rev01Mod03 Bug no : 5069157 ##


  #########End of changes for Rev01##############
  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..."
   ######### Bug# 5085363
    PLATFORMFILE=/etc/opt/SUNWps/platform.conf.default
    if [ -f $PLATFORMFILE ]; then
      $ECHO "Adding ignoreServerList to platform.conf..."
      for files in `ls /etc/opt/SUNWps/platform.conf* | grep -v orig | grep -v pre`
      do
        if [ ! -f $files.pre$PATCHID ]; then
          $CP $files $files.pre$PATCHID
        fi
        insert_line $PLATFORMFILE "gateway.debug.dir" "gateway.ignoreServerList=false"
        $SED '/^$/d' $files > $files.no_blank_lines
        $MV $files.no_blank_lines $files
        $CHMOD 644 $files $files.pre$PATCHID
      done
   fi
  ###############################################
## Begin of webdav section #5082270

    pkginfo -q SUNWpssso

    if [ $? -eq 0 ]; then
      		if [ $HAS_IDENTITY_SUPER_ADMIN_PASSWORD = "n" ]; then
         		GetIdentitySuperAdminPassword
      		fi
        # 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

        # 
        #Get the template 
        #



        TEMPFILE="/var/tmp/getDefaultValues.xml"
        VALUESFILE="/var/tmp/defaultValues"
        MODVALUESFILE="/var/tmp/modValues.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 "Getting the SunSSOAdapterService's sunConfigurationTemplates using AMADMIN"
        $AMADMIN -u "$ADMIN_DN" -w "$IS_ADMIN_PASSWORD" -t $TEMPFILE > $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="Global">
        <RemoveDefaultValues>
                <Attribute name="sunConfigurationTemplates"/>
        </RemoveDefaultValues>
    </SchemaRequests>
    <SchemaRequests serviceName="SunSSOAdapterService" SchemaType="Global">
        <AddDefaultValues>
            <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'`
            ##//if it has EXCHANGE-CALENDAR add "Somestuff here" at the end
            exwords=`$ECHO $config | $AWK '/EXCHANGE-CALENDAR/' | wc -w `
            if [ $exwords -ge 1 ];then
                #Don't add if it already has it 
                pimwords=`$ECHO $config | $AWK '/pimInterfaceType/' | wc -w `
                if [ $pimwords -lt 1 ];then
                config=`$ECHO $config | $SED  -e 's/$/\&default=pimInterfaceType\&pimInterfaceType=[INTERFACE-TYPE]\&default=usersDn\&usersDn=CN=Users\&default=hostDn\&hostDn=DC=[HOST]\&default=rootSuffix\&default=webdavProtocol\&webdavProtocol=http\&default=webdavPort\&webdavPort=80\&default=webdavCustomTaskFilter\&webdavCustomTaskFilter=true\&default=activeDirectoryLdapPort\&activeDirectoryLdapPort=389/'  `
                fi
            fi
            exwords=`$ECHO $config | $AWK '/EXCHANGE-ADDRESS-BOOK/' | wc -w `
            if [ $exwords -ge 1 ];then
                pimwords=`$ECHO $config | $AWK '/usersBaseDn/' | wc -w `
                if [ $pimwords -lt 1 ];then
                config=`$ECHO $config | $SED  -e 's/$/\&default=usersBaseDn\&usersBaseDn=[USERS-BASE-DN]/' `
                fi
            fi
            ##//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>
        </AddDefaultValues>
    </SchemaRequests>
</Requests>
EOF

        $ECHO "Removing and then adding new default values to  SunSSOAdapterService's sunConfigurationTemplates using AMADMIN"
        $AMADMIN -u "$ADMIN_DN" -w "$IS_ADMIN_PASSWORD" -t  $MODVALUESFILE

  fi

  ##End of webdav section #5082270


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

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

  # rewrite gateway.dsame.agent parameter in platform.conf
  DSAME_REWRITTEN="N"

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

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

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

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

  # add gateway.identity.server entry to platform.conf
  IS_ADDED="N"

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

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

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

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

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

  #########End of changes for Rev10##############
  elif [[ $rev == 11 ]]
  then
  #########Make changes for Rev11 here###########
  echo "Making changes for $PATCHBASE-11..."

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

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

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

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

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

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

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

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

  #########End of changes for Rev19##############

  fi
  #########End of rev_mods huge if block#########
} # 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
replace_script_install_vars
remove_tmp_scripts
restart_processes

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

trap ''
