#!/bin/ksh

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

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

BELL_CHAR='\a'

STATE_FILE="$PS_CONFIG_DIR/PSConfig.properties"
PREV_GLOBAL_DP="$PS_VAR_DIR/tmp/PrevGlobalDP.xml"

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

###############################################
# 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. $BELL_CHAR"
  exit 1
fi

GrabConfig $STATE_FILE "JDK_DIR" "="
if [ "$ANSWER" != "" ]; then
  JDK_DIR=$ANSWER
else
  $ECHO "Error: Cannot determine JDK_DIR. $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

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

FILE="$PS_BASEDIR/$PS_PRODUCT_DIR/export/rewriter_rule/GenericRuleSet.xml"
$ECHO "Loading $FILE."
$PS_BASEDIR/$PS_PRODUCT_DIR/bin/rwadmin store --runasdn "$ADMIN_DN" --password "${IDSAME_ADMIN_PASSWORD}" $FILE

FILE="$PS_BASEDIR/$PS_PRODUCT_DIR/export/rewriter_rule/DefaultRuleSet.xml"
$ECHO "Loading $FILE."
$PS_BASEDIR/$PS_PRODUCT_DIR/bin/rwadmin store --runasdn "$ADMIN_DN" --password "${IDSAME_ADMIN_PASSWORD}" $FILE

FILE="$PS_BASEDIR/$PS_PRODUCT_DIR/samples/desktop/dp-providers.xml"
$ECHO "Replacing $PS_CONFIG_DIR in $FILE."
if [ -f $FILE ]; then
  $SED -e "s#%PS_CONFIG_DIR%#$PS_CONFIG_DIR#g" $FILE > $FILE.tmp
  $MV $FILE.tmp $FILE
fi

if [ -f $PREV_GLOBAL_DP ] ; then
  $MV $PREV_GLOBAL_DP $PREV_GLOBAL_DP.old
fi
$PS_BASEDIR/$PS_PRODUCT_DIR/bin/dpadmin list -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -g > $PREV_GLOBAL_DP
if [ ! -s $PREV_GLOBAL_DP ]; then
  FILES="$PS_BASEDIR/$PS_PRODUCT_DIR/samples/desktop/dp-providers.xml $PS_BASEDIR/$PS_PRODUCT_DIR/samples/discussions/dp-providers.xml $PS_BASEDIR/$PS_PRODUCT_DIR/samples/Subscriptions/dp-providers.xml $PS_BASEDIR/$PS_PRODUCT_DIR/samples/AddressBook/addressBookProvider.xml $PS_BASEDIR/$PS_PRODUCT_DIR/samples/Calendar/calendarProvider.xml $PS_BASEDIR/$PS_PRODUCT_DIR/samples/Mail/mailProvider.xml $PS_BASEDIR/$PS_PRODUCT_DIR/samples/InstantMessaging/dp-IMProvider.xml"
  $ECHO "Loading $FILES."
  $PS_BASEDIR/$PS_PRODUCT_DIR/bin/dpadmin modify -m -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -g $FILES
fi

exit 0
