#!/bin/ksh

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

CAT=/usr/bin/cat
CP=/usr/bin/cp
ECHO=/usr/bin/echo
GREP=/usr/bin/grep
ID=/usr/bin/id
MV=/usr/bin/mv
NAWK=/usr/bin/nawk
RM=/usr/bin/rm
SED=/usr/bin/sed
UNIQ=/usr/bin/uniq

OSTYPE=`/bin/uname -s`
if [ "$OSTYPE" = "Linux" ]; then
  CAT=/bin/cat
  CP=/bin/cp
  ECHO=/bin/echo
  GREP=/bin/grep
  MV=/bin/mv
  NAWK=/bin/gawk
  RM=/bin/rm
  SED=/bin/sed
fi

BELL_CHAR='\a'

STATE_FILE="/etc/opt/SUNWps/PSConfig.properties"
PREV_GLOBAL_DP="/var/opt/SUNWps/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="/etc/opt/SUNWam/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/SUNWps/export/rewriter_rule/GenericRuleSet.xml"
$ECHO "Loading $FILE."
$PS_BASEDIR/SUNWps/bin/rwadmin store --runasdn "$ADMIN_DN" --password "${IDSAME_ADMIN_PASSWORD}" $FILE

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

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

exit 0
