#!/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 "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

AMADMIN="$IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/bin/amadmin"
DPADMIN="$PS_BASEDIR/$PS_PRODUCT_DIR/bin/dpadmin"

if [ -s $PREV_GLOBAL_DP ]; then
  $ECHO "Existing display profile detected at global node."
else
  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

  OUTFILE="/tmp/out.tmp"
  INFILE="/tmp/query.xml"
  $CAT > $INFILE << EOF
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Requests PUBLIC "-//iPlanet//iDSAME 5.0 Admin CLI DTD//EN" "file:$IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/dtd/amAdmin.dtd">
<Requests>
  <OrganizationRequests DN="$ORG_DN">
    <GetServiceTemplate serviceName="iPlanetAMAuthService" schemaType="Organization"/>
  </OrganizationRequests>
</Requests>
EOF
  $AMADMIN --runasdn "$ADMIN_DN" --password "${IDSAME_ADMIN_PASSWORD}" --data $INFILE > $OUTFILE 2>&1
  USER_NAMING=`$GREP "iplanet-am-auth-user-naming-attr =" $OUTFILE | $NAWK '{print $3}' | $SED -e "s/\[//" -e "s/\]//"`
  $RM -f $INFILE $OUTFILE

  PEOPLE_DN="ou=People,$ORG_DN"
  AUTHLESSANONYMOUS_DN="$USER_NAMING=authlessanonymous,$PEOPLE_DN"

  FILE="$PS_BASEDIR/$PS_PRODUCT_DIR/samples/portlet/portletTab.xml"
  if [ -f $FILE ]; then
    $ECHO "Loading $FILE."
    $DPADMIN modify -m -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -d "$ORG_DN" $FILE
  fi

  FILE="$PS_BASEDIR/$PS_PRODUCT_DIR/samples/portlet/sampleChannels.xml"
  if [ -f $FILE ]; then
    $ECHO "Loading $FILE."
    $DPADMIN modify -m -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -d "$ORG_DN" $FILE
  fi

  FILE="$PS_BASEDIR/$PS_PRODUCT_DIR/samples/portlet/portletTab-anon.xml"
  if [ -f $FILE ]; then
    $ECHO "Loading $FILE to $AUTHLESSANONYMOUS_DN."
    $DPADMIN modify -m -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -d "$AUTHLESSANONYMOUS_DN" $FILE
  fi

  FILE="$PS_BASEDIR/$PS_PRODUCT_DIR/samples/portlet/sampleChannels-anon.xml"
  if [ -f $FILE ]; then
    $ECHO "Loading $FILE to $AUTHLESSANONYMOUS_DN."
    $DPADMIN modify -m -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -d "$AUTHLESSANONYMOUS_DN" $FILE
  fi
fi

exit 0
