#!/bin/ksh

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

###############################################
# Defines
###############################################

BELL_CHAR='\a'

STATE_FILE="$PS_CONFIG_DIR/PSConfig.properties"




###############################################
# 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//"`
}




###############################################
# Replace string in file
###############################################
ReplaceString() {
  local FILE=$1
  local MATCH=$2
  local TEXT=$3

  $CP $FILE $FILE-tmp || exit 1
  $SED -e "s#$MATCH#$TEXT#" $FILE-tmp >$FILE
  $RM $FILE-tmp
}




###############################################
# Extract desktopType and defaultChannelName from DP...
###############################################
ExtractDesktopInfo() {

  TMP_FILE="/tmp/ma-dp-request-file.xml"
  DESKTOP_SCHEMA_FILE="/tmp/DesktopSchemaFile"

  trap 1 2 15

cat >"${TMP_FILE}"<<EOF
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Requests PUBLIC "-//iPlanet//Sun ONE Identity Server 6.0 Admin CLI DTD//EN" "jar://com/iplanet/am/admin/cli/amAdmin.dtd" >
<Requests>
  <OrganizationRequests DN="$ORG_DN">
    <GetServiceTemplate serviceName="SunPortalDesktopService" schemaType="Dynamic"/>
  </OrganizationRequests>
</Requests>
EOF

  trap ''

  #
  # Get the desktop service template
  #
  if [ -s $TMP_FILE ]
  then
    $ECHO "Getting the desktop service template.."  			
    $AMADMIN -u $ADMIN_DN -w $IDSAME_ADMIN_PASSWORD -t $TMP_FILE > $DESKTOP_SCHEMA_FILE  2>/dev/null || exit 1
  fi
  #
  DISPATCHER_NAME=`$NAWK -F: '/sunportaldesktopdefaultchannelname/ {print $NF}' $DESKTOP_SCHEMA_FILE \
					  | $CUT -d'=' -f2 \
					  | $SED -e "s/\[//g" \
					  | $SED -e "s/\]//g" \
                                          | $SED -e "s/ //g"`
  DESKTOP_TYPE=`$NAWK -F: '/sunportaldesktoptype/ {print $NF}' $DESKTOP_SCHEMA_FILE \
					  | $CUT -d'=' -f2 \
					  | $SED -e "s/\[//g" \
					  | $SED -e "s/\]//g" \
                                          | $SED -e "s/ //g"`

  $RM -f $TMP_FILE $DESKTOP_SCHEMA_FILE
}



###############################################
# Update default channel name...
###############################################
UpdateDefaultChannelName() {
  DESKTOP_FILE="/tmp/desktop.xml"
  $ECHO "Updating default channel name..."
cat >"${DESKTOP_FILE}"<<EOF
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Requests PUBLIC "-//iPlanet//Sun ONE Identity Server 6.0 Admin CLI DTD//EN" "jar://com/iplanet/am/admin/cli/amAdmin.dtd" >
<Requests>
  <SchemaRequests serviceName="SunPortalDesktopService" SchemaType="Dynamic">
    <ModifyDefaultValues>
      <AttributeValuePair>
        <Attribute name="sunPortalDesktopDefaultChannelName"/>
        <Value>$1</Value>
      </AttributeValuePair>
    </ModifyDefaultValues>
  </SchemaRequests>

  <OrganizationRequests DN="$ORG_DN">
    <ModifyServiceTemplate serviceName="SunPortalDesktopService" schemaType="Dynamic">
      <AttributeValuePair>
        <Attribute name="sunPortalDesktopDefaultChannelName"/>
        <Value>$1</Value>
      </AttributeValuePair>
    </ModifyServiceTemplate>
  </OrganizationRequests>
</Requests>
EOF

  $AMADMIN -u $ADMIN_DN -w $IDSAME_ADMIN_PASSWORD -t $DESKTOP_FILE || exit 1
  $RM -f $DESKTOP_FILE
}




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

if [ `$ID | $AWK '{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. $BELL_CHAR"
  exit 1
fi

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

#***
# Define a few more things...
#***
DPADMIN="$PS_BASEDIR/$PS_PRODUCT_DIR/bin/dpadmin"
AMADMIN="$IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/bin/amadmin"

# IS 6.2 new location
# FILE="$IDSAME_CONFIG_DIR/config/AMConfig.properties"
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
PEOPLE_DN="ou=People,$ORG_DN"
AUTHLESSANON_DN="uid=authlessanonymous,$PEOPLE_DN"

ORG_FILE="$PS_BASEDIR/$PS_PRODUCT_DIR/samples/MAP/dp-org-sample.xml"
ANON_FILE="$PS_BASEDIR/$PS_PRODUCT_DIR/samples/MAP/dp-anon-sample.xml"


#***
# Extract desktopType and defaultChannelName from DP...
#***
ExtractDesktopInfo

#***
# Determine if this is an MA upgrade...
#***
IS_MA_UPGRADE=""
$ECHO $DISPATCHER_NAME | $EGREP -s WirelessDesktopDispatcher && IS_MA_UPGRADE="yes"

#***
# Determine if sampleportal is installed...
#***
SAMPLEPORTAL_PRESENT=""
$ECHO $DESKTOP_TYPE | $EGREP -s sampleportal && SAMPLEPORTAL_PRESENT="yes"

#***
#  Add core MA global DP definitions...
#***
if [ -z "$IS_MA_UPGRADE" ]
then
    FILE="$PS_BASEDIR/$PS_PRODUCT_DIR//MAP/xml/dp-global-core.xml"
    $ECHO "Loading $FILE."
    $DPADMIN modify -m -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -g $FILE || exit 1
fi

#***
# Update default channel name...
#***
if [ -z "$IS_MA_UPGRADE" ]
then
    UpdateDefaultChannelName WirelessDesktopDispatcher
fi

#***
# If sampleportal is present, enable MA desktop...
#***
if [ -n "$SAMPLEPORTAL_PRESENT" -a -z "$IS_MA_UPGRADE" ]
then 
    if [ "$OSTYPE" = "SunOS" ]; then
      $INSTALLF "SUNWpsma" $ORG_FILE
    fi
    ReplaceString $ORG_FILE "\[SUNPORTALDESKTOPDEFAULTCHANNEL\]" "$DISPATCHER_NAME"
    ReplaceString $ORG_FILE "\[PS_CONFIG_DIR\]" "$PS_CONFIG_DIR"
    if [ "$OSTYPE" = "SunOS" ]; then
      $INSTALLF -f "SUNWpsma"
    fi

    $ECHO "Adding MA DP entries."
    $ECHO "Loading $ORG_FILE."
    $DPADMIN modify -m -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -d "$ORG_DN" $ORG_FILE || exit 1
fi

#***
# If sampleportal is present, anon MA desktop...
#***
if [ -n "$SAMPLEPORTAL_PRESENT" -a -z "$IS_MA_UPGRADE" ]
then 
    if [ "$OSTYPE" = "SunOS" ]; then
      $INSTALLF "SUNWpsma" $ANON_FILE
    fi
    ReplaceString $ANON_FILE "\[SUNPORTALDESKTOPDEFAULTCHANNEL\]" "$DISPATCHER_NAME"
    if [ "$OSTYPE" = "SunOS" ]; then
      $INSTALLF -f "SUNWpsma"
    fi

    $ECHO "Adding MA anon DP entries."
    $ECHO "Loading $ANON_FILE to $AUTHLESSANON_DN "
    $DPADMIN modify -m -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -d "$AUTHLESSANON_DN" $ANON_FILE || exit 1

fi

exit 0
