#!/bin/ksh

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

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

BELL_CHAR='\a'

LOG_DIR="$IDSAME_VAR_DIR/debug"
LOG_FILE="$LOG_DIR/isconfig.log"

SERVICE_DIR="$PS_VAR_DIR/tmp/service"
REQUEST_DIR="$PS_VAR_DIR/tmp/request"
RESBUNDLE_DIR="$PS_VAR_DIR/tmp/locale"

PS_STATE_FILE="$PS_CONFIG_DIR/PSConfig.properties"
SRA_STATE_FILE="$PS_CONFIG_DIR/SRAConfig.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//"`
}

###############################################
# Load schema
###############################################
LoadSchema() {
  local FILE=$1
  local NAME=""

  NAME=`$GREP -i "^objectclasses:" $FILE | $SED -e "s/^..*NAME '\([^'][^']*\)'..*$/\1/"`
  if [ "$NAME" != "" ]; then
    $LDAPSEARCH -b "$SCHEMA_DN" -h $DS_HOST -p $DS_PORT -D "$DS_DIRMGR_DN" -w "${DS_DIRMGR_PASSWORD}" "(objectclass=*)" | $GREP "$NAME" > /dev/null 2>&1
    if [ $? -ne 0 ]; then
      $ECHO "Importing $FILE..."
      $ECHO "Importing $FILE..." >> $LOG_FILE
      $LDAPMODIFY -a -h $DS_HOST -p $DS_PORT -D "$DS_DIRMGR_DN" -w "${DS_DIRMGR_PASSWORD}" -f $FILE >> $LOG_FILE 2>&1
      if [ $? -ne 0 ]; then
	$ECHO "Error: Error importing $FILE. $BELL_CHAR"
	exit 1
      fi
    else
      $ECHO "$FILE already imported."
      $ECHO "$FILE already imported." >> $LOG_FILE
    fi
  else
    $ECHO "Error: $FILE is not valid ldif file. $BELL_CHAR"
    exit 1
  fi
}

###############################################
# Load service
###############################################
LoadService() {
  local FILE=$1

  if [ -f $SERVICE_DIR/$FILE ]; then
    $ECHO "$FILE already loaded."
    $ECHO "$FILE already loaded." >> $LOG_FILE
  else
    $SED -e "s#%DEPLOY_URI%#$DEPLOY_URI#g" \
	 -e "s#%DYNAMIC_URI%#$DYNAMIC_URI#g" \
	 -e "s#%PS_BASEDIR%#$PS_BASEDIR#g" \
	 -e "s#%PS_PRODUCT_DIR%#$PS_PRODUCT_DIR#g" \
	 -e "s#INST_SERVER_DOMAIN#$SERVER_DOMAIN#g" \
 	 -e "s#%PRODUCT_NAME%#$PRODUCT_NAME#g" \
	 -e "s#%PRODUCT_VERSION%#$PRODUCT_VERSION#g" $FILE > $SERVICE_DIR/$FILE

    $ECHO "Loading $FILE..."
    $ECHO "Loading $FILE..." >> $LOG_FILE
    $AMADMIN --runasdn "$ADMIN_DN" --password "${IDSAME_ADMIN_PASSWORD}" --verbose --continue --schema $SERVICE_DIR/$FILE  >> $LOG_FILE 2>&1
  fi
}

###############################################
# Load request
###############################################
LoadRequest() {
  local FILE=$1
  local XML_FILE=""

  XML_FILE="`$BASENAME $FILE .request`.xml"
  if [ -f $REQUEST_DIR/$XML_FILE ]; then
    $ECHO "$FILE already loaded."
    $ECHO "$FILE already loaded." >> $LOG_FILE
  else
    $SED -e "s#%ANONYMOUS_DN%#$ANONYMOUS_DN#g" \
	 -e "s#%DEPLOY_URI%#$DEPLOY_URI#g" \
	 -e "s#%DYNAMIC_URI%#$DYNAMIC_URI#g" \
	 -e "s#%DYNAMIC_URL%#$DYNAMIC_URL#g" \
	 -e "s#%GW_DOMAIN%#$GW_DOMAIN#g" \
	 -e "s#%IDSAME_AMCONSOLE%#$IDSAME_AMCONSOLE#g" \
	 -e "s#%IDSAME_AMSERVER%#$IDSAME_AMSERVER#g" \
	 -e "s#%IDSAME_BASEDIR%#$IDSAME_BASEDIR#g" \
	 -e "s#%IDSAME_PRODUCT_DIR%#$IDSAME_PRODUCT_DIR#g" \
	 -e "s#%IDSAME_LDAPUSER_PASSWORD%#${IDSAME_LDAPUSER_PASSWORD}#g" \
	 -e "s#%IDSAME_USER_NAMING%#$IDSAME_USER_NAMING#g" \
	 -e "s#INST_ENABLE_EPROXY#$ENABLE_EPROXY#g" \
	 -e "s#INST_ENABLE_RPROXY#$ENABLE_RPROXY#g" \
	 -e "s#INST_EPROXY_PORT#$EPROXY_PORT#g" \
	 -e "s#INST_GWNAME#$SRA_GATEWAY_PROFILE#g" \
	 -e "s#INST_PS_SERVER_LIST#$PS_SERVER_LIST#g" \
	 -e "s#INST_RPROXY_PORT#$RPROXY_PORT#g" \
	 -e "s#INST_SERVER_DOMAIN#$SERVER_DOMAIN#g" \
	 -e "s#INST_USE_PROXY#$USE_WEBPROXY#g" \
	 -e "s#INST_WEB_PROXY_DOMAIN|INST_SUBDOMAIN#$FULL_SERVER_DOMAIN#g" \
	 -e "s#%LOGIN_URL%#$LOGIN_URL#g" \
	 -e "s#%ORG_DN%#$ORG_DN#g" \
	 -e "s#%PEOPLE_DN%#$PEOPLE_DN#g" \
	 -e "s#%ROOT_DN%#$ROOT_DN#g" \
	 -e "s#%SRA_LOG_USER_PASSWORD%#${SRA_LOG_USER_PASSWORD}#g" \
	 -e "s#%PRODUCT_NAME%#$PRODUCT_NAME#g" \
	 -e "s#%PRODUCT_VERSION%#$PRODUCT_VERSION#g" $FILE > $REQUEST_DIR/$XML_FILE

    $ECHO "Applying $FILE..."
    $ECHO "Applying $FILE..." >> $LOG_FILE
    $AMADMIN --runasdn "$ADMIN_DN" --password "${IDSAME_ADMIN_PASSWORD}" --verbose --continue --data $REQUEST_DIR/$XML_FILE >> $LOG_FILE 2>&1
    $ECHO "This request has been loaded." > $REQUEST_DIR/$XML_FILE
  fi
}

###############################################
# Load the Resource bundle to Directory Server
###############################################
LoadResourceBundle() {
  local RESBUNDLE_FILE=$1
  local FILE=""
  local NAME=""

  FILE=`$BASENAME $RESBUNDLE_FILE`
  if [ -f $RESBUNDLE_DIR/$FILE ]; then
    $ECHO "$FILE already loaded."
    $ECHO "$FILE already loaded." >> $LOG_FILE
  else
    NAME=`$BASENAME $RESBUNDLE_FILE .properties`
    $AMADMIN --runasdn "$ADMIN_DN" --password "${IDSAME_ADMIN_PASSWORD}" --verbose --addresourcebundle $NAME --resourcebundlefilename $RESBUNDLE_FILE >> $LOG_FILE 2>&1

    $CP $RESBUNDLE_FILE $RESBUNDLE_DIR/$FILE
  fi
}

###############################################
# Get the platform server list.
###############################################
GetServerList() {
  local PLATFORM_SERVER_LIST=""
  IS_SERVER_LIST=""

  PLATFORM_SERVER_LIST="$PS_BASEDIR/$PS_PRODUCT_DIR/export/platformList.xml"
  $CAT >> $PLATFORM_SERVER_LIST << EOF
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
    Copyright (c) 2003 Sun Microsystems, Inc. All rights reserved
    Use is subject to license terms.
-->
<!DOCTYPE Requests SYSTEM "file:$IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/dtd/amAdmin.dtd">
<Requests>
   <SchemaRequests serviceName="iPlanetAMPlatformService" SchemaType="global">
       <GetServiceDefaultValues>
           <Attribute name="iplanet-am-platform-server-list" />
       </GetServiceDefaultValues>
   </SchemaRequests>
</Requests>
EOF

  $AMADMIN --runasdn "$ADMIN_DN" --password "${IDSAME_ADMIN_PASSWORD}" --data $PLATFORM_SERVER_LIST > $PS_BASEDIR/$PS_PRODUCT_DIR/export/toParseList

  GrabConfig $PS_BASEDIR/$PS_PRODUCT_DIR/export/toParseList "iplanet-am-platform-server-list" "="
  if [ "$ANSWER" != "" ]; then
    IS_SERVER_LIST=$ANSWER
    IS_SERVER_LIST=`$ECHO $IS_SERVER_LIST | sed -e "s/\[//" `
    IS_SERVER_LIST=`$ECHO $IS_SERVER_LIST | sed -e "s/\]//" `
    IS_SERVER_LIST=`$ECHO $IS_SERVER_LIST | sed -e "s/,/ /g" `

    for IS_SERVER in $IS_SERVER_LIST; do
      IS_ENTRY=`$ECHO $IS_SERVER | $NAWK '{print substr($0,0,index($0,"|")-1)}'`
      IS_ENTRIES="$IS_ENTRIES $IS_ENTRY"
    done
  fi
  IS_SERVER_LIST=$IS_ENTRIES
  $RM -f $PLATFORM_SERVER_LIST
  $RM -f $PS_BASEDIR/$PS_PRODUCT_DIR/export/toParseList
}

###############################################
# Get the non-authenticated URL list.
###############################################
GetNonAuthURLList() {
  local AMSERVER_URLS=""
  local AMSERVER_URL=""
  local AMCONSOLE_URLS=""
  local AMCONSOLE_URL=""
  local IS_SERVER=""

  AMSERVER_URLS="%IDSAME_AMSERVER%/images %IDSAME_AMSERVER%/login_images %IDSAME_AMSERVER%/js %IDSAME_AMSERVER%/css"
  AMCONSOLE_URLS="%IDSAME_AMCONSOLE%/console/images %IDSAME_AMCONSOLE%/console/css %IDSAME_AMCONSOLE%/console/js"

  for IS_SERVER in $IS_SERVER_LIST; do
    for AMSERVER_URL in $AMSERVER_URLS; do
      NONAUTH_URLS="$NONAUTH_URLS <Value>$IS_SERVER$AMSERVER_URL</Value>"
    done
    for AMCONSOLE_URL in $AMCONSOLE_URLS; do
      NONAUTH_URLS="$NONAUTH_URLS <Value>$IS_SERVER$AMCONSOLE_URL</Value>"
    done
  done
}

###############################################
# Get the FC URL list
###############################################
GetFCUrlList() {
  local IS_SERVER=""
  local PROTOCOL=""
  local HOST=""
  local PORT=""

  for IS_SERVER in $IS_SERVER_LIST; do
    FC_URL_LIST="$FC_URL_LIST <Value>$IS_SERVER</Value>"
    PROTOCOL=`$ECHO $IS_SERVER | $NAWK '{ print substr($0, 0, index($0,":")-1) }'`
    TMP_STR=`$ECHO $IS_SERVER | $NAWK '{ print substr($0, index($0,"/")+2) }'`
    HOST=`$ECHO $TMP_STR | $NAWK '{ print substr($0, 0, index($0,":")-1) }'`
    PORT=`$ECHO $TMP_STR | $NAWK '{ print substr($0, index($0,":")+1) }'`
    if [ "$PORT" = "80" ] || [ "$PORT" = "443" ]; then
      FC_URL_LIST="$FC_URL_LIST<Value>$PROTOCOL://$HOST</Value>"
    fi
  done

  # Add this instance of portalserver to FC_URL_LIST
  if [ "$PS_PROTOCOL" != "$SERVER_PROTOCOL" ] || [ "$PS_HOST" != "$SERVER_HOST" ] || [ "$PS_PORT" != "$SERVER_PORT" ]; then
    FC_URL_LIST="$FC_URL_LIST<Value>$PS_PROTOCOL://$PS_HOST:$PS_PORT</Value>"
    if [ "$PS_PORT" = "80" ] || [ "$PS_PORT" = "443" ]; then
      FC_URL_LIST="$FC_URL_LIST<Value>$PS_PROTOCOL://$PS_HOST</Value>"
    fi
  fi
}

###############################################
# Replace the FC URL list and Non Auth URL list
###############################################
ReplaceFCNonAuth() {
  local FILE_TO_REPLACE=""

  FILE_TO_REPLACE="$PS_BASEDIR/$PS_PRODUCT_DIR/export/request/srapGatewayConfig.request"
  if [ -f $FILE_TO_REPLACE ]; then
    GetServerList
    GetNonAuthURLList
    GetFCUrlList

    if [ "$OSTYPE" = "SunOS" ]; then
      $INSTALLF "SUNWpsgws" $FILE_TO_REPLACE
    fi
    $SED -e "s#INST_FCURLS#$FC_URL_LIST#g" \
         -e "s#INST_IS_NONAUTH_URLS#$NONAUTH_URLS#g" $FILE_TO_REPLACE > $FILE_TO_REPLACE.tmp
    $MV $FILE_TO_REPLACE.tmp $FILE_TO_REPLACE
    if [ "$OSTYPE" = "SunOS" ]; then
      $INSTALLF -f "SUNWpsgws"
    fi
  fi
}

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

if [ `$ID | $NAWK '{print $1}'` != "uid=0(root)" ]; then
  $ECHO "You must be root user. $BELL_CHAR"
  exit 1
fi

ANONYMOUS_DN="%ANONYMOUS_DN%"
DEPLOY_URI="%DEPLOY_URI%"
ENABLE_EPROXY="INST_ENABLE_EPROXY"
ENABLE_RPROXY="INST_ENABLE_RPROXY"
EPROXY_PORT="INST_EPROXY_PORT"
FULL_SERVER_DOMAIN="INST_WEB_PROXY_DOMAIN|INST_SUBDOMAIN"
GW_DOMAIN="%GW_DOMAIN%"
IDSAME_AMCONSOLE="%IDSAME_AMCONSOLE%"
IDSAME_AMSERVER="%IDSAME_AMSERVER%"
IDSAME_BASEDIR="%IDSAME_BASEDIR%"
IDSAME_USER_NAMING="%IDSAME_USER_NAMING%"
LOGIN_URL="%LOGIN_URL%"
ORG_DN="%ORG_DN%"
PEOPLE_DN="%PEOPLE_DN%"
PS_SERVER_LIST="INST_PS_SERVER_LIST"
ROOT_DN="%ROOT_DN%"
RPROXY_PORT="INST_RPROXY_PORT"
SERVER_DOMAIN="INST_SERVER_DOMAIN"
SRA_GATEWAY_PROFILE="INST_GWNAME"
USE_WEBPROXY="INST_USE_PROXY"
PRODUCT_NAME="%PRODUCT_NAME%"
PRODUCT_VERSION="%PRODUCT_VERSION%"

if [ -f $PS_STATE_FILE ]; then
  GrabConfig $PS_STATE_FILE "JDK_DIR" "="
  if [ "$ANSWER" != "" ]; then
    JAVA_HOME=$ANSWER
  else
    $ECHO "Error: Cannot determine JDK_DIR. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $PS_STATE_FILE "IDSAME_BASEDIR" "="
  if [ "$ANSWER" != "" ]; then
    IDSAME_BASEDIR=$ANSWER
  else
    $ECHO "Error: Cannot determine IDSAME_BASEDIR. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $PS_STATE_FILE "BASEDIR" "="
  if [ "$ANSWER" != "" ]; then
    PS_BASEDIR=$ANSWER
  else
    $ECHO "Error: Cannot determine BASEDIR. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $PS_STATE_FILE "DS_HOST" "="
  if [ "$ANSWER" != "" ]; then
    DS_HOST=$ANSWER
  else
    $ECHO "Error: Cannot determine DS_HOST. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $PS_STATE_FILE "DS_PORT" "="
  if [ "$ANSWER" != "" ]; then
    DS_PORT=$ANSWER
  else
    $ECHO "Error: Cannot determine DS_PORT. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $PS_STATE_FILE "DS_DIRMGR_DN" "="
  if [ "$ANSWER" != "" ]; then
    DS_DIRMGR_DN=$ANSWER
  else
    $ECHO "Error: Cannot determine DS_DIRMGR_DN. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $PS_STATE_FILE "IDSAME_AMSERVER" "="
  if [ "$ANSWER" != "" ]; then
    IDSAME_AMSERVER=$ANSWER
  else
    $ECHO "Error: Cannot determine IDSAME_AMSERVER. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $PS_STATE_FILE "IDSAME_AMCONSOLE" "="
  if [ "$ANSWER" != "" ]; then
    IDSAME_AMCONSOLE=$ANSWER
  else
    $ECHO "Error: Cannot determine IDSAME_AMCONSOLE. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $PS_STATE_FILE "DEPLOY_URI" "="
  if [ "$ANSWER" != "" ]; then
    DEPLOY_URI=$ANSWER
  else
    $ECHO "Error: Cannot determine DEPLOY_URI. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $PS_STATE_FILE "LOAD_BALANCER_URL" "="
  if [ "$ANSWER" != "" ]; then
    DYNAMIC_URI=$ANSWER
  else
    $ECHO "Error: Cannot determine DYNAMIC_URI. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $PS_STATE_FILE "SERVER_PROTOCOL" "="
  if [ "$ANSWER" != "" ]; then
    SERVER_PROTOCOL=$ANSWER
  else
    $ECHO "Error: Cannot determine SERVER_PROTOCOL. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $PS_STATE_FILE "SERVER_HOST" "="
  if [ "$ANSWER" != "" ]; then
    SERVER_HOST=$ANSWER
  else
    $ECHO "Error: Cannot determine SERVER_HOST. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $PS_STATE_FILE "SERVER_PORT" "="
  if [ "$ANSWER" != "" ]; then
    SERVER_PORT=$ANSWER
  else
    $ECHO "Error: Cannot determine SERVER_PORT. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $PS_STATE_FILE "PS_PROTOCOL" "="
  if [ "$ANSWER" != "" ]; then
    PS_PROTOCOL=$ANSWER
  else
    $ECHO "Error: Cannot determine PS_PROTOCOL. $BELL_CHAR"
    PS_PROTOCOL=$SERVER_PROTOCOL
  fi
  GrabConfig $PS_STATE_FILE "PS_HOST" "="
  if [ "$ANSWER" != "" ]; then
    PS_HOST=$ANSWER
  else
    $ECHO "Error: Cannot determine PS_HOST. $BELL_CHAR"
    PS_HOST=$SERVER_HOST
  fi
  GrabConfig $PS_STATE_FILE "PS_PORT" "="
  if [ "$ANSWER" != "" ]; then
    PS_PORT=$ANSWER
  else
    $ECHO "Error: Cannot determine PS_PORT. $BELL_CHAR"
    PS_PORT=$SERVER_PORT
  fi
  LOGIN_URL="$DYNAMIC_URI/dt"
  DYNAMIC_URL="$PS_PROTOCOL://$PS_HOST:$PS_PORT"
  PS_SERVER_LIST="$PS_PROTOCOL://$PS_HOST:$PS_PORT"
fi

if [ -f $SRA_STATE_FILE ]; then
  GrabConfig $SRA_STATE_FILE "SERVER_DOMAIN" "="
  if [ "$ANSWER" != "" ]; then
    SERVER_DOMAIN=$ANSWER
  else
    $ECHO "Error: Cannot determine SERVER_DOMAIN. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $SRA_STATE_FILE "GW_PROTOCOL" "="
  if [ "$ANSWER" != "" ]; then
    GW_PROTOCOL=$ANSWER
  else
    $ECHO "Error: Cannot determine GW_PROTOCOL. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $SRA_STATE_FILE "GW_DOMAIN" "="
  if [ "$ANSWER" != "" ]; then
    GW_DOMAIN=$ANSWER
  else
    $ECHO "Error: Cannot determine GW_DOMAIN. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $SRA_STATE_FILE "GW_PORT" "="
  if [ "$ANSWER" != "" ]; then
    GW_PORT=$ANSWER
  else
    $ECHO "Error: Cannot determine GW_PORT. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $SRA_STATE_FILE "SRA_GATEWAY_PROFILE" "="
  if [ "$ANSWER" != "" ]; then
    SRA_GATEWAY_PROFILE=$ANSWER
  else
    $ECHO "Error: Cannot determine SRA_GATEWAY_PROFILE. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $SRA_STATE_FILE "ASSIGN_SERVICE" "="
  if [ "$ANSWER" != "" ]; then
    ASSIGN_SERVICE=$ANSWER
  else
    $ECHO "Error: Cannot determine ASSIGN_SERVICE. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $SRA_STATE_FILE "USE_WEBPROXY" "="
  if [ "$ANSWER" != "" ]; then
    USE_WEBPROXY=$ANSWER
  else
    $ECHO "Error: Cannot determine USE_WEBPROXY. $BELL_CHAR"
    exit 1
  fi

  if [ "$GW_PROTOCOL" = "http" ]; then
    ENABLE_EPROXY="false"
    EPROXY_PORT="443"
    ENABLE_RPROXY="true"
    RPROXY_PORT=$GW_PORT
  else
    ENABLE_EPROXY="true"
    EPROXY_PORT=$GW_PORT
    ENABLE_RPROXY="false"
    RPROXY_PORT=80
  fi

  FULL_SERVER_DOMAIN="$SERVER_DOMAIN"
fi

VERSION_FILE="$PS_BASEDIR/$PS_PRODUCT_DIR/lib/PSversion.properties"
if [ -f $VERSION_FILE ]; then
  GrabConfig $VERSION_FILE "productname" "="
  if [ "$ANSWER" != "" ]; then
    PRODUCT_NAME=$ANSWER
  else
    $ECHO "Error: Cannot determine PRODUCT_NAME. $BELL_CHAR"
    exit 1
  fi
  GrabConfig $VERSION_FILE "productversion" "="
  if [ "$ANSWER" != "" ]; then
    PRODUCT_VERSION=$ANSWER
  else
    $ECHO "Error: Cannot determine PRODUCT_VERSION. $BELL_CHAR"
    exit 1
  fi
fi

$ECHO "" >> $LOG_FILE
$ECHO `$DATE +%D` "at" `$DATE +%r` >> $LOG_FILE
$ECHO "#########################################################" >> $LOG_FILE

LDAPSEARCH="$IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/bin/ldapsearch"
LDAPMODIFY="$IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/bin/ldapmodify"
AMADMIN="$IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/bin/amadmin"

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
SCHEMA_DN="cn=schema"
PEOPLE_DN="ou=People,$ORG_DN"

if [ "$OSTYPE" = "Linux" ]; then
  EXTRA_LIBS="$JAVA_HOME/jre/lib/i386/native_threads"
else
  EXTRA_LIBS=""
fi
IDSAME_PATH="$IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/ldaplib/ldapsdk:$JSS_NSS_NSPR_LIB_DIR"
if [ -z "${LD_LIBRARY_PATH}" ]; then
  LD_LIBRARY_PATH=$IDSAME_PATH:$EXTRA_LIBS
else
  LD_LIBRARY_PATH="$IDSAME_PATH::$EXTRA_LIBS:${LD_LIBRARY_PATH}"
fi
export LD_LIBRARY_PATH

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
if [ "$USER_NAMING" != "" ]; then
  IDSAME_USER_NAMING=$USER_NAMING
fi

if [ ! -d $SERVICE_DIR ]; then
  $MKDIR -p $SERVICE_DIR
fi
if [ ! -d $REQUEST_DIR ]; then
  $MKDIR -p $REQUEST_DIR
fi
if [ ! -d $RESBUNDLE_DIR ]; then
  $MKDIR -p $RESBUNDLE_DIR
fi

DIR="$PS_BASEDIR/$PS_PRODUCT_DIR/export/ldif"
if [ -d $DIR ]; then
  cd $DIR
  FILES=`$LS *.ldif 2>&1`
  if [ $? -eq 0 ]; then
    for FILE in $FILES; do
      LoadSchema $FILE
    done
  fi
fi

DIR="$PS_BASEDIR/$PS_PRODUCT_DIR/export/service"
if [ -d $DIR ]; then
  cd $DIR
  FILES=`$LS *.xml 2>&1`
  if [ $? -eq 0 ]; then
    for FILE in $FILES; do
      LoadService $FILE
    done
  fi
fi

ReplaceFCNonAuth
DIR="$PS_BASEDIR/$PS_PRODUCT_DIR/export/request"
if [ -d $DIR ]; then
  cd $DIR
  FILES=`$LS *.request 2>&1`
  if [ $? -eq 0 ]; then
    for FILE in $FILES; do
      LoadRequest $FILE
    done
  fi
fi

DIR="$PS_BASEDIR/$PS_PRODUCT_DIR/export/locale"
if [ -d $DIR ]; then
  cd $DIR
  FILES=`$LS *.properties 2>&1`
  if [ $? -eq 0 ]; then
    for FILE in $FILES; do
      LoadResourceBundle $FILE
    done
  fi
fi

exit 0
