#!/bin/ksh 

###############################################
# Sourcing macros
DIRNAME=/usr/bin/dirname
. ../lib/psmacros
###############################################

OMIT_CHAR='\c'
BELL_CHAR='\a'

STATE_FILE="$PS_CONFIG_DIR/PSConfig.properties"
JAX_AVAILABLE_FLAG="false"


echo " State file is  >>>>>>>>>>>>>>> ${STATE_FILE} >>>>>>>>>>>>>>>> "

###############################################
# Get configuration from file
###############################################
GrabConfig() {
  local FILE=$1
  local KEY=$2
  local SEPARATOR=$3

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

###############################################
# Get Property values from file
###############################################
GrabConfigWithSpaces() {
  GRABCONFIG_KEY=$1
  GRABCONFIG_FILE=$2
  GRABCONFIG_SEPARATOR=$3
  ANSWER_CONFIG=`$GREP "^$GRABCONFIG_KEY$GRABCONFIG_SEPARATOR" $GRABCONFIG_FILE | $UNIQ | $SED -e "s/$GRABCONFIG_KEY$GRABCONFIG_SEPARATOR//" | $SED -e "s/^ //"`
}

###############################################
# Question
###############################################
Question() {
  local PROMPT=$1
  local DEFAULT=$2
  local VALID_CHARS=$3
  local DONE=""
  local CHECK=""

  DONE="n"
  while [ "$DONE" = "n" ]; do
    if [ "$DEFAULT" != "" ]; then
      $ECHO "$PROMPT [$DEFAULT] $OMIT_CHAR"
    else
      $ECHO "$PROMPT $OMIT_CHAR"
    fi
    read ANSWER
    if [ "$ANSWER" = "" ]; then
      ANSWER=$DEFAULT
      DONE="y"
    else
      CHECK=`$ECHO $ANSWER | $GREP "[^$VALID_CHARS]"`
      if [ "$CHECK" = "" ]; then
        DONE="y"
      else
        $ECHO "Invalid answer! $BELL_CHAR"
      fi
    fi
  done
}

###############################################
# Pause
###############################################
Pause() {
  local ANSWER=""

  $ECHO "Press any key when ready to continue... $OMIT_CHAR"
  read ANSWER
}

###############################################
# Get password
###############################################
GetPassword() {
  local PROMPT=$1
  local DONE=""

  DONE="n"
  while [ "$DONE" = "n" ]; do
    $ECHO "$PROMPT $OMIT_CHAR"
    $STTY -echo
    read ANSWER
    $STTY echo
    $ECHO ""
    if [ "$ANSWER" != "" ]; then
      DONE="y"
    fi
  done
}

###############################################
# Append line in file
###############################################
AppendLine() {
  local FILE=$1
  local MATCH=$2
  local TEXT=$3

  $CP $FILE $FILE-tmp
$SED -e "/$MATCH/ {
a\\
$TEXT
}" $FILE-tmp > $FILE
  $RM $FILE-tmp
}

###############################################
# Delete line in file
###############################################
DeleteLine() {
  local FILE=$1
  local MATCH=$2

  $CP -p $FILE $FILE-tmp
  $SED -e "/$MATCH/d" $FILE-tmp > $FILE
  $RM $FILE-tmp
}

CheckJAX(){
$ECHO ""
$ECHO "Checking whether JAX shared components are installed ..."

if [ "$OSTYPE" = "SunOS" ]; then
	# Do solaris specific search for JAX shared component
	if [[ ("$(pkginfo | grep SUNWjaxp)" = "") && ("$(pkginfo | grep SUNWjaxb)" = "") && ("$(pkginfo | grep SUNWxrgrtb)" = "") && ("$(pkginfo | grep SUNWxrpcrt)" = "") && ("$(pkginfo | grep SUNWxsrt)" = "")]]
			then $ECHO JAX components is NOT installed on host $(hostname)
		else
                    	$ECHO JAX components is installed on host $(hostname)
			JAX_AVAILABLE_FLAG="true"
	fi
fi

if [ "$OSTYPE" = "Linux" ]; then
	# Do Linux specific search for JAX
	if [[ ("$(rpm -qa | grep "jaxp")" = "")&&("$(rpm -qa | grep "xrpcrt")" = "" )&&("$(rpm -qa | grep "xsrt")" = "" ) ]]
			then $ECHO JAX components is NOT installed on host $(hostname)
		else
                    	$ECHO JAX components is installed on host $(hostname)
			JAX_AVAILABLE_FLAG="true"
	fi
fi
}

#############################################################################
# This procedure deletes the lines in the deploy.import file,
# that are specific to copying the JAX jars.
#
#############################################################################
RemoveJAXComponentsCopy(){

DEPLOY_IMPORT_FILE="${PS_BASEDIR}/${PS_PRODUCT_DIR}/export/deploy.import"
DeleteLine $DEPLOY_IMPORT_FILE jax-qname.jar
DeleteLine $DEPLOY_IMPORT_FILE namespace.jar 
DeleteLine $DEPLOY_IMPORT_FILE relaxngDatatype.jar 
DeleteLine $DEPLOY_IMPORT_FILE xsdlib.jar 
DeleteLine $DEPLOY_IMPORT_FILE jaxb-api.jar 
DeleteLine $DEPLOY_IMPORT_FILE jaxb-impl.jar 
DeleteLine $DEPLOY_IMPORT_FILE jaxb-libs.jar 
DeleteLine $DEPLOY_IMPORT_FILE jaxb-xjc.jar 
DeleteLine $DEPLOY_IMPORT_FILE xercesImpl.jar 
DeleteLine $DEPLOY_IMPORT_FILE xalan.jar 
DeleteLine $DEPLOY_IMPORT_FILE dom.jar 
DeleteLine $DEPLOY_IMPORT_FILE sax.jar 
DeleteLine $DEPLOY_IMPORT_FILE jaxrpc-impl.jar 
DeleteLine $DEPLOY_IMPORT_FILE jaxrpc-spi.jar 
DeleteLine $DEPLOY_IMPORT_FILE saaj-api.jar 
DeleteLine $DEPLOY_IMPORT_FILE saaj-impl.jar  

}

#############################################################################
# This procedure adds the lines in the deploy.import file,
# that are specific to copying the JAX jars.
# First it checks the existence of the shared JAX components installation.
# Check is also made if the deploy.import already has these copy instruction.
############################################################################# 
AddJAXComponentsCopy(){
CheckJAX
SHARE_LIB_DIR="/usr/share/lib"
if [ "$OSTYPE" = "SunOS" ] 
    then SHARE_LIB_DIR="/usr/share/lib"
fi
if [ "$OSTYPE" = "Linux" ] 
    then SHARE_LIB_DIR="/usr/share/lib"
fi

if [ "${JAX_AVAILABLE_FLAG}" = "true" ]; then
      # Copy JAX shared library jars
      DEPLOY_IMPORT_FILE="${PS_BASEDIR}/${PS_PRODUCT_DIR}/export/deploy.import"
       
      # Make sure we don't duplicate
      # Write to deploy.import only if the relevant jax jar's are not already there.
      
      $ECHO Inserting JAX shared components copy instructions 
      JAX_ALREADY_BEING_COPIED="$(grep jax-qname.jar $DEPLOY_IMPORT_FILE)"

      if [ -z "$JAX_ALREADY_BEING_COPIED" ]; then
      $ECHO ${SHARE_LIB_DIR}/jax-qname.jar %WEB_SRC_DIR%/WEB-INF/lib/jax-qname.jar >> $DEPLOY_IMPORT_FILE
      fi
      
      JAX_ALREADY_BEING_COPIED="$(grep namespace.jar $DEPLOY_IMPORT_FILE)"
      if [ -z "$JAX_ALREADY_BEING_COPIED" ]; then
      $ECHO ${SHARE_LIB_DIR}/namespace.jar %WEB_SRC_DIR%/WEB-INF/lib/namespace.jar >> $DEPLOY_IMPORT_FILE
      fi
      
      JAX_ALREADY_BEING_COPIED="$(grep relaxngDatatype.jar  $DEPLOY_IMPORT_FILE)"
      if [ -z "${JAX_ALREADY_BEING_COPIED}" ]; then
      $ECHO ${SHARE_LIB_DIR}/relaxngDatatype.jar %WEB_SRC_DIR%/WEB-INF/lib/relaxngDatatype.jar >> $DEPLOY_IMPORT_FILE
      fi
      
      JAX_ALREADY_BEING_COPIED="$(grep xsdlib.jar  $DEPLOY_IMPORT_FILE)"
      if [ -z "${JAX_ALREADY_BEING_COPIED}" ]; then
      $ECHO ${SHARE_LIB_DIR}/xsdlib.jar %WEB_SRC_DIR%/WEB-INF/lib/xsdlib.jar >> $DEPLOY_IMPORT_FILE
      fi
      
      JAX_ALREADY_BEING_COPIED="$(grep jaxb-api.jar  $DEPLOY_IMPORT_FILE)"
      if [ -z "${JAX_ALREADY_BEING_COPIED}" ]; then
      $ECHO ${SHARE_LIB_DIR}/jaxb-api.jar %WEB_SRC_DIR%/WEB-INF/lib/jaxb-api.jar >> $DEPLOY_IMPORT_FILE
      fi
      
      JAX_ALREADY_BEING_COPIED="$(grep jaxb-impl.jar  $DEPLOY_IMPORT_FILE)"
      if [ -z "${JAX_ALREADY_BEING_COPIED}" ]; then   
      $ECHO ${SHARE_LIB_DIR}/jaxb-impl.jar %WEB_SRC_DIR%/WEB-INF/lib/jaxb-impl.jar >> $DEPLOY_IMPORT_FILE
      fi
      
      JAX_ALREADY_BEING_COPIED="$(grep jaxb-libs.jar  $DEPLOY_IMPORT_FILE)"
      if [ -z "${JAX_ALREADY_BEING_COPIED}" ]; then 
      $ECHO ${SHARE_LIB_DIR}/jaxb-libs.jar %WEB_SRC_DIR%/WEB-INF/lib/jaxb-libs.jar >> $DEPLOY_IMPORT_FILE
      fi
      JAX_ALREADY_BEING_COPIED="$(grep jaxb-xjc.jar  $DEPLOY_IMPORT_FILE)"
      if [ -z "${JAX_ALREADY_BEING_COPIED}" ]; then
      $ECHO ${SHARE_LIB_DIR}/jaxb-xjc.jar %WEB_SRC_DIR%/WEB-INF/lib/jaxb-xjc.jar >> $DEPLOY_IMPORT_FILE
      fi
      
      JAX_ALREADY_BEING_COPIED="$(grep xercesImpl.jar  $DEPLOY_IMPORT_FILE)"
      if [ -z "${JAX_ALREADY_BEING_COPIED}" ]; then
      $ECHO ${SHARE_LIB_DIR}/xercesImpl.jar %WEB_SRC_DIR%/WEB-INF/lib/xercesImpl.jar >> $DEPLOY_IMPORT_FILE
      fi
      
      JAX_ALREADY_BEING_COPIED="$(grep xalan.jar  $DEPLOY_IMPORT_FILE)"
      if [ -z "${JAX_ALREADY_BEING_COPIED}" ]; then
      $ECHO ${SHARE_LIB_DIR}/xalan.jar.jar %WEB_SRC_DIR%/WEB-INF/lib/xalan.jar >> $DEPLOY_IMPORT_FILE
      fi
      
      JAX_ALREADY_BEING_COPIED="$(grep dom.jar  $DEPLOY_IMPORT_FILE)"
      if [ -z "${JAX_ALREADY_BEING_COPIED}" ]; then
      $ECHO ${SHARE_LIB_DIR}/dom.jar.jar %WEB_SRC_DIR%/WEB-INF/lib/dom.jar >> $DEPLOY_IMPORT_FILE
      fi
      
      JAX_ALREADY_BEING_COPIED="$(grep sax.jar  $DEPLOY_IMPORT_FILE)"
      if [ -z "${JAX_ALREADY_BEING_COPIED}" ]; then
      $ECHO ${SHARE_LIB_DIR}/sax.jar .jar %WEB_SRC_DIR%/WEB-INF/lib/sax.jar >> $DEPLOY_IMPORT_FILE
      fi
      
      JAX_ALREADY_BEING_COPIED="$(grep jaxp-api.jar  $DEPLOY_IMPORT_FILE)"
      if [ -z "${JAX_ALREADY_BEING_COPIED}" ]; then
      $ECHO ${SHARE_LIB_DIR}/jaxp-api.jar %WEB_SRC_DIR%/WEB-INF/lib/jaxp-api.jar >> $DEPLOY_IMPORT_FILE
      fi
      
      JAX_ALREADY_BEING_COPIED="$(grep jaxrpc-impl.jar  $DEPLOY_IMPORT_FILE)"
      if [ -z "${JAX_ALREADY_BEING_COPIED}" ]; then
      $ECHO ${SHARE_LIB_DIR}/jaxrpc-impl.jar %WEB_SRC_DIR%/WEB-INF/lib/jaxrpc-impl.jar >> $DEPLOY_IMPORT_FILE
      fi
      
      JAX_ALREADY_BEING_COPIED="$(grep jaxrpc-api.jar  $DEPLOY_IMPORT_FILE)"
      if [ -z "${JAX_ALREADY_BEING_COPIED}" ]; then
      $ECHO ${SHARE_LIB_DIR}/jaxrpc-api.jar %WEB_SRC_DIR%/WEB-INF/lib/jaxrpc-api.jar >> $DEPLOY_IMPORT_FILE
      fi
      
      JAX_ALREADY_BEING_COPIED="$(grep jaxrpc-spi.jar  $DEPLOY_IMPORT_FILE)"
      if [ -z "${JAX_ALREADY_BEING_COPIED}" ]; then
      $ECHO ${SHARE_LIB_DIR}/jaxrpc-spi.jar %WEB_SRC_DIR%/WEB-INF/lib/jaxrpc-spi.jar >> $DEPLOY_IMPORT_FILE
      fi
      
      JAX_ALREADY_BEING_COPIED="$(grep saaj-api.jar  $DEPLOY_IMPORT_FILE)"
      if [ -z "${JAX_ALREADY_BEING_COPIED}" ]; then
      $ECHO ${SHARE_LIB_DIR}/saaj-api.jar %WEB_SRC_DIR%/WEB-INF/lib/saaj-api.jar >> $DEPLOY_IMPORT_FILE
      fi
      
      JAX_ALREADY_BEING_COPIED="$(grep saaj-impl.jar  $DEPLOY_IMPORT_FILE)"
      if [ -z "${JAX_ALREADY_BEING_COPIED}" ]; then
      $ECHO ${SHARE_LIB_DIR}/saaj-impl.jar %WEB_SRC_DIR%/WEB-INF/lib/saaj-impl.jar >> $DEPLOY_IMPORT_FILE
      fi
      
      else
          $ECHO Install JAX shared components and then upgrade
          exit 1 
fi
}


###########################################################
# Main
###########################################################

if [ `$ID | $NAWK '{print $1}'` != "uid=0(root)" ]; then
  $ECHO "You must be root user. $BELL_CHAR"
  exit 1
fi

if [ ! -f $STATE_FILE ]; then
  $ECHO "Error: $STATE_FILE does not exist."
  exit 1
fi

GrabConfig $STATE_FILE "JDK_DIR" "="
if [ "$ANSWER" != "" ]; then
  JDK_DIR=$ANSWER
else
  $ECHO "Error: Cannot determine JDK_DIR."
  exit 1
fi
GrabConfig $STATE_FILE "DEPLOY_TYPE" "="
if [ "$ANSWER" != "" ]; then
  DEPLOY_TYPE=$ANSWER
else
  $ECHO "Error: Cannot determine DEPLOY_TYPE."
  exit 1
fi
GrabConfig $STATE_FILE "DEPLOY_URI" "="
if [ "$ANSWER" != "" ]; then
  DEPLOY_URI=$ANSWER
else
  $ECHO "Error: Cannot determine DEPLOY_URI."
  exit 1
fi
GrabConfig $STATE_FILE "DS_HOST" "="
if [ "$ANSWER" != "" ]; then
  DS_HOST=$ANSWER
else
  $ECHO "Error: Cannot determine DS_HOST. $BELL_CHAR"
  exit 1
fi
GrabConfig $STATE_FILE "DS_PORT" "="
if [ "$ANSWER" != "" ]; then
  DS_PORT=$ANSWER
else
  $ECHO "Error: Cannot determine DS_PORT. $BELL_CHAR"
  exit 1
fi
GrabConfig $STATE_FILE "DS_DIRMGR_DN" "="
if [ "$ANSWER" != "" ]; then
  DS_DIRMGR_DN=$ANSWER
else
  $ECHO "Error: Cannot determine DS_DIRMGR_DN. $BELL_CHAR"
  exit 1
fi
GrabConfig $STATE_FILE "IDSAME_BASEDIR" "="
if [ "$ANSWER" != "" ]; then
  IDSAME_BASEDIR=$ANSWER
else
  $ECHO "Error: Cannot determine IDSAME_BASEDIR."
  exit 1
fi
GrabConfig $STATE_FILE "BASEDIR" "="
if [ "$ANSWER" != "" ]; then
  PS_BASEDIR=$ANSWER
else
  $ECHO "Error: Cannot determine BASEDIR."
  exit 1
fi
GrabConfig $STATE_FILE "SERVER_PROTOCOL" "="
if [ "$ANSWER" != "" ]; then
  SERVER_PROTOCOL=$ANSWER
else
  $ECHO "Error: Cannot determine SERVER_PROTOCOL."
  exit 1
fi
GrabConfig $STATE_FILE "SERVER_HOST" "="
if [ "$ANSWER" != "" ]; then
  SERVER_HOST=$ANSWER
else
  $ECHO "Error: Cannot determine SERVER_HOST."
  exit 1
fi
GrabConfig $STATE_FILE "SERVER_PORT" "="
if [ "$ANSWER" != "" ]; then
  SERVER_PORT=$ANSWER
else
  $ECHO "Error: Cannot determine SERVER_PORT."
  exit 1
fi

FILE="$IDSAME_CONFIG_DIR/config/AMConfig.properties"
GrabConfigWithSpaces "com.iplanet.am.version" $FILE "="
AM_VERSION=$ANSWER_CONFIG

$ECHO AM Version on host $(hostname) is $AM_VERSION
if [ ! "$AM_VERSION" = "6.3" ]; then
       # Do insertion of JAX shared jars copying instructions in deploy.import
       AddJAXComponentsCopy
       else
       # No insertion of JAX shared jars copying instructions is neccessary
       # However if they copy instructions already exist remove them
       RemoveJAXComponentsCopy
       $ECHO ""
fi

$ECHO "Portal Server 2004Q2 to 2005Q1 Upgrade"
$ECHO ""

GetPassword "What is the Directory Manager password?"
DS_DIRMGR_PASSWORD="$ANSWER"
export DS_DIRMGR_PASSWORD

GetPassword "What is the Identity Server administration password?"
IDSAME_ADMIN_PASSWORD="$ANSWER"
export IDSAME_ADMIN_PASSWORD


DEPLOY_ADMIN_PASSWORD="not_needed"
if [ "$DEPLOY_TYPE" = "SUNONE" ] || [ "$DEPLOY_TYPE" = "IWS" ] || [ "$DEPLOY_TYPE" = "WEBLOGIC" ]; then
  GetPassword "What is the web container administration password?"
  DEPLOY_ADMIN_PASSWORD="$ANSWER"
fi
export DEPLOY_ADMIN_PASSWORD

IDSAME_PATH="$IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/ldaplib/ldapsdk:/usr/lib/mps/secv1"
if [ -z "${LD_LIBRARY_PATH}" ]; then
  LD_LIBRARY_PATH=$IDSAME_PATH
else
  LD_LIBRARY_PATH="$IDSAME_PATH:${LD_LIBRARY_PATH}"
fi
export LD_LIBRARY_PATH


FILE="$IDSAME_CONFIG_DIR/config/AMConfig.properties"
ADMIN_DN=`$GREP "^com.sun.identity.authentication.super.user=" $FILE | $SED -e "s/com.sun.identity.authentication.super.user=//"`
ROOT_DN=`$GREP "^com.iplanet.am.rootsuffix=" $FILE | $SED -e "s/com.iplanet.am.rootsuffix=//"`
ORG_DN=`$GREP "^com.iplanet.am.defaultOrg=" $FILE | $SED -e "s/com.iplanet.am.defaultOrg=//"`
if [ "$ORG_DN" != "$ROOT_DN" ]; then
  ORG_DN="$ORG_DN,$ROOT_DN"
fi
SCHEMA_DN="cn=schema"
PEOPLE_DN="ou=People,$ORG_DN"

LDAPSEARCH="$IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/bin/ldapsearch"
LDAPMODIFY="$IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/bin/ldapmodify"
AMADMIN="$IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/bin/amadmin"
DPADMIN="$PS_BASEDIR/$PS_PRODUCT_DIR/bin/dpadmin"


##################################################
# 1-01,2-01 Upgrade SSO Adapter
##################################################

$ECHO ""
$ECHO "Updating SSO Adapter Service..."

FILE="/tmp/sunSSOAdapterService.xml"

$CAT > $FILE <<EOF
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Requests
  PUBLIC "-//Sun ONE//iDSAME 5.0 Admin CLI DTD//EN "
  "file:$IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/dtd/amAdmin.dtd">
<Requests>
    <SchemaRequests serviceName="SunSSOAdapterService" SchemaType="Global">
        <AddDefaultValues>
            <AttributeValuePair>
                <Attribute name="sunConfigurationTemplates"/>
                <Value>default|http:///?configName=SUN-UWC-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>
                <Value>default|http:///?configName=SUN-UWC-CALENDAR&amp;encoded=password&amp;default=protocol&amp;default=clientProtocol&amp;default=type&amp;default=subType&amp;default=enableProxyAuth&amp;default=proxyAdminUid&amp;default=proxyAdminPassword&amp;default=ssoClassName&amp;merge=host&amp;merge=port&amp;merge=uid&amp;merge=password&amp;clientProtocol=http&amp;enableProxyAuth=false&amp;proxyAdminUid=[PROXY-ADMIN-UID]&amp;proxyAdminPassword=[PROXY-ADMIN_PASSWORD]&amp;type=CALENDAR-TYPE&amp;subType=sun-one&amp;ssoClassName=com.sun.ssoadapter.impl.JCAPISSOAdapter&amp;default=enablePerRequestConnection&amp;enablePerRequestConnection=true&amp;default=userAttribute&amp;userAttribute=uid&amp;default=domain&amp;default=serverSSOEnabled&amp;serverSSOEnabled=true&amp;merge=clientHost&amp;merge=clientPort&amp;default=uwcContext&amp;uwcContext=uwc</Value>
                <Value>default|imap:///?configName=SUN-UWC-MAIL&amp;encoded=password&amp;default=protocol&amp;default=clientProtocol&amp;default=type&amp;default=subType&amp;default=enableProxyAuth&amp;default=proxyAdminUid&amp;default=proxyAdminPassword&amp;default=ssoClassName&amp;merge=host&amp;merge=port&amp;merge=uid&amp;merge=password&amp;merge=smtpServer&amp;merge=clientPort&amp;merge=smtpPort&amp;clientProtocol=http&amp;enableProxyAuth=false&amp;proxyAdminUid=[PROXY-ADMIN-UID]&amp;proxyAdminPassword=[PROXY-ADMIN_PASSWORD]&amp;type=MAIL-TYPE&amp;subType=sun-one&amp;ssoClassName=com.sun.ssoadapter.impl.JavaMailSSOAdapter&amp;default=enablePerRequestConnection&amp;enablePerRequestConnection=true&amp;default=userAttribute&amp;userAttribute=uid&amp;merge=domain&amp;default=serverSSOEnabled&amp;serverSSOEnabled=true</Value>
            </AttributeValuePair>
        </AddDefaultValues>
    </SchemaRequests>
    <SchemaRequests serviceName="SunSSOAdapterService" SchemaType="Dynamic">
        <AddDefaultValues>
            <AttributeValuePair>
                <Attribute name="sunSSOAdapterConfigurations"/>
                <Value>default|undef:///?configName=sunUWCAddressBook&amp;configDesc=SUN-UWC-ADDRESS-BOOK</Value>
                <Value>default|http:///?configName=sunUWCCalendar&amp;configDesc=SUN-UWC-CALENDAR</Value>
                <Value>default|imap:///?configName=sunUWCMail&amp;configDesc=SUN-UWC-MAIL&amp;port=143&amp;smtpPort=25</Value>
            </AttributeValuePair>
        </AddDefaultValues>
    </SchemaRequests>
    <OrganizationRequests DN="$ORG_DN">
        <AddServiceTemplateAttributeValues serviceName="SunSSOAdapterService" schemaType="Dynamic">
            <AttributeValuePair>
                <Attribute name="sunSSOAdapterConfigurations"/>
                    <Value>default|undef:///?configName=sunUWCAddressBook&amp;configDesc=SUN-UWC-ADDRESS-BOOK</Value>
                    <Value>default|http:///?configName=sunUWCCalendar&amp;configDesc=SUN-UWC-CALENDAR</Value>
                    <Value>default|imap:///?configName=sunUWCMail&amp;configDesc=SUN-UWC-MAIL&amp;port=143&amp;smtpPort=25</Value>
            </AttributeValuePair>
        </AddServiceTemplateAttributeValues>
    </OrganizationRequests>
</Requests>
EOF
$AMADMIN -u "$ADMIN_DN" -w "$IDSAME_ADMIN_PASSWORD" -v -t $FILE
$RM -f $FILE

############################################################################################
# 1-02 Create new Address Book Provider Display Profile definition
# 3-02 Create Sample IFrameProvider Display Profile definition
############################################################################################

$ECHO ""
$ECHO "Creating new Address Book Provider Display Profile definition..."

FILE="/tmp/addressBookProvider.xml"
$CAT > $FILE <<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="UWCAddressBookProvider" 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="sunUWCAddressBook" />
        <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>
    <Provider name="IFrameProvider" class="com.sun.portal.providers.jsp.JSPProvider">
        <Properties>
                <String name="title" value="*** IFrame Provider ***"/>
                <String name="description" value="*** This Provider uses IFrames ***"/>
                <String name="refreshTime" value="0" advanced="true"/>
                <ConditionalProperties condition="client" value="HTML">
                  <ConditionalProperties condition="locale" value="en">
                    <String name="helpURL" value="en/desktop/iframechann.htm" advanced="true"/>
                  </ConditionalProperties>
                </ConditionalProperties>
                <String name="fontFace1" value="Sans-serif"/>
                <String name="productName" value="Sun JavaTM System Portal Server"/>
                <String name="width" value="thick"/>
                <String name="srcURL" value="http://www.sun.com"/>
                <String name="fWidth" value="100%"/>
                <String name="fHeight" value="400"/>
                <String name="scrolling" value="yes"/>
                <String name="fBorder" value="0"/>
                <String name="contentPage" value="iframe.jsp"/>
                <Boolean name="showExceptions" value="false"/>
                <String name="editType" value="edit_subset" advanced="true"/>
                <Boolean name="isEditable" value="false" advanced="true"/>
       </Properties>
   </Provider>
  </Providers>
</DisplayProfile>
EOF
$DPADMIN modify -m -u "$ADMIN_DN" -w "$IDSAME_ADMIN_PASSWORD" -g $FILE
$RM -f $FILE

############################################################################################
# 1-03,2-02 Create new Address Book , Mail and Calendar Channel Display Profile definition
############################################################################################

$ECHO ""
$ECHO "Creating new Address Book, Mail and Calendar Channel Display Profile definition..."

FILE="/tmp/addressBookMailCalendarChannels.xml"
$CAT > $FILE <<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="UWCAddressBook" provider="UWCAddressBookProvider">
      <Properties/>
    </Channel>
    <Channel name="UWCCalendar" provider="CalendarProvider">
      <Properties>
        <String name="title" value="Communications Express Calendar" />
        <String name="description" value="Sun Java System Communications Express Calendar" />
        <String name="ssoAdapter"  value="sunUWCCalendar" />
        <ConditionalProperties condition="client" value="HTML">
            <Collection name="applicationHelperEdit">
                <String value="com.sun.portal.providers.calendar.UWCCalendarHelper" />
            </Collection>
            <String name="applicationHelperURL"
                    value="com.sun.portal.providers.calendar.UWCCalendarHelper" />
        </ConditionalProperties>
        <Collection name="ssoEditAttributes">
            <String name="host" value="string|Server Name:"/>
            <String name="port" value="string|Server Port:"/>
            <String name="uid" value="string|User Name:"/>
            <String name="password" value="password|User Password:"/>
            <String name="clientHost" value="string|Client Server Name:"/>
            <String name="clientPort" value="string|Client Port:"/>
        </Collection>
      </Properties>
    </Channel>
    <Channel name="UWCMail" provider="MailProvider">
      <Properties>
        <String name="title" value="Communications Express Mail" />
        <String name="description" value="Sun Java System Communications Express Mail" />
        <String name="ssoAdapter"  value="sunUWCMail" />
        <ConditionalProperties condition="client" value="HTML">
           <Collection name="applicationHelperEdit">
             <String value="com.sun.portal.providers.mail.UWCMailHelper" />
           </Collection>
           <String name="applicationHelperURL" value="com.sun.portal.providers.mail.UWCMailHelper" />
        </ConditionalProperties>
      </Properties>
    </Channel>
    <Channel name="SampleIFrame" provider="IFrameProvider">
         <Properties>
              <String name="title" value="IFrame Channel"/>
             <String name="description" value="This is the sample for the IFrame Provider" />
             <ConditionalProperties condition="locale" value="en">
                 <ConditionalProperties condition="locale" value="US">
                      <String name="title" value="IFrame Channel"/>
                      <String name="description" value="This is the sample forthe IFrame Provider" />
                </ConditionalProperties>
             </ConditionalProperties>
         </Properties>
      </Channel>
    <Container name="MyFrontPageTabPanelContainer" provider="PredefinedFrontPageTabPanelContainerProvider">
      <Properties/>
      <Available>
          <Reference value="UWCAddressBook"/>
      </Available>
      <Selected/>
      <Channels/>
    </Container>
  </Channels>
  <Providers/>
</DisplayProfile>
EOF
$DPADMIN modify -m -u "$ADMIN_DN" -w "$IDSAME_ADMIN_PASSWORD" -d "$ORG_DN" $FILE
$RM -f $FILE

#########################################################
#  Mobile Access related updates 
#  First check whether MA is installed
#########################################################

$ECHO ""
$ECHO "Checking whether Mobile Access is Installed ..."

UNAME=/bin/uname
OSTYPE=`$UNAME -s`
MA_AVAILABLE_FLAG="false"

if [ "$OSTYPE" = "SunOS" ]; then
	# Do solaris specific search for MA
	if [[ "$(pkginfo | grep SUNWpsma)" = "" ]]
			then echo Mobile Access is NOT installed on host $(hostname)
		else
			echo Mobile Access is installed on host $(hostname)
			MA_AVAILABLE_FLAG="true"	
	fi
fi	

if [ "$OSTYPE" = "Linux" ]; then
	# Do Linux specific search for MA
	if [[ "$(rpm -qa | grep "mobileaccess")" = "" ]]
			then echo Mobile Access is NOT installed on host $(hostname)
		else
			echo Mobile Access is installed on host $(hostname)
			MA_AVAILABLE_FLAG="true"	
	fi
fi

###############################################################################################
#  1-04 Mobile Access Address Book Provider Display Profile definition 
###############################################################################################

if [[ "${MA_AVAILABLE_FLAG}" = "true" ]]; then
	# Continue with the usual updates

$ECHO ""
$ECHO "Creating Mobile Access Address Book Provider Display Profile definition..."

FILE="/tmp/mapAddressBookProvider.xml"
$CAT > $FILE <<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
$DPADMIN modify -m -u "$ADMIN_DN" -w "$IDSAME_ADMIN_PASSWORD" -p UWCAddressBookProvider -g $FILE
$RM -f $FILE
fi

#########################################################################
# 1-05 Mobile Access Address Book Container Display Profile definition
#########################################################################

if [[ "${MA_AVAILABLE_FLAG}" = "true" ]]; then
	# Continue with the usual updates

$ECHO ""
$ECHO "Creating Mobile Access Address Book Container Display Profile definition ..."

FILE="/tmp/mapAddressBookContainer.xml"
$CAT > $FILE <<EOF
<!DOCTYPE DisplayProfile SYSTEM "jar://resources/psdp.dtd">
<DisplayProfile version="1.0" priority="10">
  <Properties/>
  <Channels>
    <Container name="JSPNativeContainer" provider="WirelessJSPDesktopProvider"> 
      <Properties/>
        <Available>
          <Reference value="UWCAddressBook"/>
        </Available>
    <Selected/>
    <Channels/>
    </Container>
    <Container name="TemplateNativeContainer" provider="WirelessTemplateDesktopProvider"> 
      <Properties/>
        <Available>
          <Reference value="UWCAddressBook"/>
        </Available>
    <Selected/>
    <Channels/>
    </Container>
    <Container name="JSPRenderingContainer" provider="JSPRenderingContainerProvider"> 
      <Properties/>
        <Available>
          <Reference value="UWCAddressBook"/>
        </Available>
    <Selected/>
    <Channels/>
    </Container>
  </Channels>
  <Providers/>
</DisplayProfile>
EOF
$DPADMIN modify -m -u "$ADMIN_DN" -w "$IDSAME_ADMIN_PASSWORD" -d "$ORG_DN" $FILE
$RM -f $FILE
fi


#########################################################################
# 1-06 Create new Mobile Address Book Configuration
#########################################################################

if [[ "${MA_AVAILABLE_FLAG}" = "true" ]]; then
	# Creating new Mobile Address Book Configuration

$ECHO ""
$ECHO "Creating Mobile Access Address Book Container Display Profile definition ..."

FILE="/tmp/mapAddressBookConfiguration.xml"
$CAT > $FILE <<EOF	
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Requests
  PUBLIC "-//Sun ONE//iDSAME 5.0 Admin CLI DTD//EN "
  "file:$IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/dtd/amAdmin.dtd">
<Requests>
    <SchemaRequests serviceName="SunMobileAppABService" SchemaType="Dynamic">
        <AddDefaultValues>
            <AttributeValuePair>
                <Attribute name="sunMobileAppABConfig"/>
                    <Value>default|undef:///?configName=sunUWCAddressBook&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=sunUWCAddressBook&amp;configDesc=MA-AB-APP</Value>
            </AttributeValuePair>
        </AddServiceTemplateAttributeValues>
    </OrganizationRequests>
</Requests>
EOF
$AMADMIN -u "$ADMIN_DN" -w "$IDSAME_ADMIN_PASSWORD" -v -t $FILE
#$RM -f $FILE
fi



# To replace desktopConfig.properties path
FILE=$PS_CONFIG_DIR/desktop/desktopconfig.properties
if [ "$OSTYPE" = "SunOS" ]; then
  $INSTALLF "SUNWpsdt" $FILE
fi
$CP $FILE $FILE-tmp
$SED -e "s#%PS_BASEDIR%#$PS_BASEDIR#g" \
	 -e "s#%PS_PRODUCT_DIR%#$PS_PRODUCT_DIR#g" \
	 -e "s#%PS_CONFIG_DIR%#$PS_CONFIG_DIR#g" $FILE-tmp > $FILE
$RM -f $FILE-tmp
if [ "$OSTYPE" = "SunOS" ]; then
  $INSTALLF -f "SUNWpsdt"
fi

#Deployment

if [ "$DEPLOY_TYPE" = "WEBLOGIC" ] || [ "$DEPLOY_TYPE" = "WEBSPHERE" ]; then
  $ECHO ""
  ${PS_BASEDIR}/${PS_PRODUCT_DIR}/bin/deploy deploy -uri $DEPLOY_URI -deploy_admin_password "$DEPLOY_ADMIN_PASSWORD"
else
  $ECHO ""
  ${PS_BASEDIR}/${PS_PRODUCT_DIR}/bin/deploy redeploy -deploy_admin_password "$DEPLOY_ADMIN_PASSWORD"
fi

###########################
# Warn user to restart

$ECHO ""
$ECHO "Please restart the web container and directory server to complete the upgrade."

exit 0
