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

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

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

###############################################
# 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

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

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

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

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

WSDL_URL="$PS_PROTOCOL://$PS_HOST:$PS_PORT$DEPLOY_URI/wsrp/wsdl/default"

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

AMADMIN="$IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/bin/amadmin"

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

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"
AUTHLESSANON_DN="$USER_NAMING=authlessanonymous,$PEOPLE_DN"

WCADMIN=$PS_BASEDIR/$PS_PRODUCT_DIR/lib/wcadmin
DPADMIN=$PS_BASEDIR/$PS_PRODUCT_DIR/bin/dpadmin

#
# add the PEID for the local producer
#

$ECHO "adding the sample producer entity ..."
PEID=`$WCADMIN add -u "$ADMIN_DN" -w  "$IDSAME_ADMIN_PASSWORD" -d "$ORG_DN" -r "$WSDL_URL" -n "Local Producer"`

if [ "$?" != "0" ] || [ "$PEID" == "" ]; then
  $ECHO "Error: Failed to create producer entity. $BELL_CHAR"
  exit 1
fi

$ECHO "created sample producer entity ID=$PEID"

#
# create the WSRP remote portlet channels that are referenced by the 
# wsrp samples tab
#

$ECHO "deploying sample remote portlets ..."
NAMES="JSP Bookmark Notepad Weather"
for NAME in $NAMES; do
    $ECHO "deploying sample remote portlet NAME=$NAME"
    $WCADMIN deploy -u "$ADMIN_DN" -w  "$IDSAME_ADMIN_PASSWORD" -d "$ORG_DN" --pid "$PEID" --rid "${NAME}Portlet" --chname "${NAME}RemotePortlet"
done

#
# add the wsrp samples tab
#

$ECHO "adding the WSRP sample tab ..."
FILES="$PS_BASEDIR/$PS_PRODUCT_DIR/samples/wsrpconsumer/wsrpTab.xml"
$DPADMIN modify -m -u "$ADMIN_DN" -w "$IDSAME_ADMIN_PASSWORD" -d "$ORG_DN" $FILES
$ECHO "adding the WSRP sample tab to authlessanonymous user account ..."
FILES="$PS_BASEDIR/$PS_PRODUCT_DIR/samples/wsrpconsumer/wsrpTab-anon.xml"
$DPADMIN modify -m -u "$ADMIN_DN" -w "$IDSAME_ADMIN_PASSWORD" -d "$AUTHLESSANON_DN" $FILES

exit 0
