#!/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 "DEPLOY_INSTANCE" "="
if [ "$ANSWER" != "" ]; then
  DEPLOY_INSTANCE=$ANSWER
else
  $ECHO "Error: Cannot determine DEPLOY_INSTANCE. $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 "SERVER_PROTOCOL" "="
if [ "$ANSWER" != "" ]; then
  SERVER_PROTOCOL=$ANSWER
else
  $ECHO "Error: Cannot determine SERVER_PROTOCOL. $BELL_CHAR"
  exit 1
fi
GrabConfig $STATE_FILE "SERVER_HOST" "="
if [ "$ANSWER" != "" ]; then
  SERVER_HOST=$ANSWER
else
  $ECHO "Error: Cannot determine SERVER_HOST. $BELL_CHAR"
  exit 1
fi
GrabConfig $STATE_FILE "SERVER_PORT" "="
if [ "$ANSWER" != "" ]; then
  SERVER_PORT=$ANSWER
else
  $ECHO "Error: Cannot determine SERVER_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
GrabConfig $STATE_FILE "LOAD_BALANCER_URL" "="
if [ "$ANSWER" != "" ]; then
  LOAD_BALANCER_URL=$ANSWER
else
  $ECHO "Error: Cannot determine LOAD_BALANCER_URL. $BELL_CHAR"
  exit 1
fi

AMADMIN="$IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/bin/amadmin"
DPADMIN="$PS_BASEDIR/$PS_PRODUCT_DIR/bin/dpadmin"
RUN_CS_CLI="$PS_VAR_DIR/https-$DEPLOY_INSTANCE$DEPLOY_URI/run-cs-cli"
SEARCH_SERVER="$LOAD_BALANCER_URL/search" 

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

if [ ! -s $PREV_GLOBAL_DP ]; then

  FILE="$PS_BASEDIR/$PS_PRODUCT_DIR/samples/desktop/dp-org.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

  FILES="$PS_BASEDIR/$PS_PRODUCT_DIR/samples/desktop/dp-org.xml $PS_BASEDIR/$PS_PRODUCT_DIR/samples/discussions/dp-org.xml $PS_BASEDIR/$PS_PRODUCT_DIR/samples/Subscriptions/dp-org.xml $PS_BASEDIR/$PS_PRODUCT_DIR/samples/AddressBook/addressBookChannels.xml $PS_BASEDIR/$PS_PRODUCT_DIR/samples/Calendar/calendarChannels.xml $PS_BASEDIR/$PS_PRODUCT_DIR/samples/Mail/mailChannels.xml $PS_BASEDIR/$PS_PRODUCT_DIR/samples/InstantMessaging/dp-IMChannel.xml"
  $ECHO "Loading $FILES."
  $DPADMIN modify -m -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -d "$ORG_DN" $FILES
  
  $ECHO "Modifying searchServer attribute in search channel to $SEARCH_SERVER." 
  $DPADMIN modify -m -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -d "$ORG_DN" -p Search << EOF 
<?xml version="1.0" encoding="utf-8" standalone="no"?> 
<!DOCTYPE DisplayProfile SYSTEM "jar://resources/psdp.dtd"> 
<Properties> 
  <String name="searchServer" value="$SEARCH_SERVER"/> 
</Properties> 
EOF
  
  $ECHO "Modifying searchServer attribute in Discussions channel to $SEARCH_SERVER." 
  $DPADMIN modify -m -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -d "$ORG_DN" -p Discussions << EOF 
<?xml version="1.0" encoding="utf-8" standalone="no"?> 
<!DOCTYPE DisplayProfile SYSTEM "jar://resources/psdp.dtd"> 
<Properties> 
  <String name="searchServer" value="$SEARCH_SERVER"/> 
</Properties> 
EOF
  
  $ECHO "Modifying searchServer attribute in DiscussionLite channel to $SEARCH_SERVER." 
  $DPADMIN modify -m -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -d "$ORG_DN" -p DiscussionLite << EOF 
<?xml version="1.0" encoding="utf-8" standalone="no"?> 
<!DOCTYPE DisplayProfile SYSTEM "jar://resources/psdp.dtd"> 
  
<Properties> 
  <String name="searchServer" value="$SEARCH_SERVER"/> 
</Properties> 
EOF
  
  $ECHO "Adding NetMail to application channel."
  $DPADMIN modify -m -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -d "$ORG_DN" -p App << EOF
<?xml version="1.0" encoding="utf-8" standalone="no"?> 
<!DOCTYPE DisplayProfile SYSTEM "jar://resources/psdp.dtd"> 
  
<Properties>
  <Collection name="targets">
    <String value="NetMail Lite|$DEPLOY_URI/NetMailServlet?nsid=newHTMLSession"/>
    <String value="NetMail|$DEPLOY_URI/NetMailServlet?nsid=newAppletSession"/>
  </Collection>
  <Collection name="userApps">
    <String value="NetMail Lite"/>
    <String value="NetMail"/>
  </Collection>
</Properties>
EOF

  $ECHO "Adding NetMail to bookmark channel."
  $DPADMIN modify -m -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -d "$ORG_DN" -p Bookmark << EOF
<?xml version="1.0" encoding="utf-8" standalone="no"?> 
<!DOCTYPE DisplayProfile SYSTEM "jar://resources/psdp.dtd"> 
  
<Properties>
  <Collection name="targets">
    <String value="NetMail Local Installer|$DEPLOY_URI/NetMailServlet?nsid=newInstallSession"/>
  </Collection>
</Properties>
EOF
fi

FILE="$PS_BASEDIR/$PS_PRODUCT_DIR/samples/search/ps-help.soif"
if [ "$OSTYPE" = "SunOS" ]; then
  $INSTALLF "SUNWpssep" $FILE
fi
$CP $FILE $FILE-tmp
$SED -e "s#%URL%#$DEPLOY_URI/docs/en#g" $FILE-tmp > $FILE
$RM $FILE-tmp
if [ -x $RUN_CS_CLI ]; then
  $ECHO "Loading sample search content from $FILE"
  $RUN_CS_CLI rdmgr $FILE
fi
if [ "$OSTYPE" = "SunOS" ]; then
  $INSTALLF -f "SUNWpssep"
fi

DIR="$PS_VAR_DIR/https-$DEPLOY_INSTANCE$DEPLOY_URI/config"
if [ -d $DIR ]; then
  FILE="$PS_BASEDIR/$PS_PRODUCT_DIR/samples/search/ps-tax.soif"
  $CP $FILE $DIR/taxonomy.rdm
  if [ -x $RUN_CS_CLI ]; then
    $ECHO "Loading sample taxonomy content from $FILE"
    $RUN_CS_CLI rdmgr -T -I
  fi
fi

if [ -x $RUN_CS_CLI ]; then
  FILE="$PS_BASEDIR/$PS_PRODUCT_DIR/samples/discussions/discussions.soif"
  $ECHO "Loading sample discussion content from $FILE"
  $RUN_CS_CLI rdmgr -y discussion $FILE
fi

if [ ! -s $PREV_GLOBAL_DP ]; then
  FILES="$PS_BASEDIR/$PS_PRODUCT_DIR/samples/desktop/dp-anon.xml $PS_BASEDIR/$PS_PRODUCT_DIR/samples/discussions/dp-anon.xml"
  $ECHO "Loading $FILES."
  $DPADMIN modify -m -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -d "$AUTHLESSANON_DN" $FILES
fi

FILE="$PS_BASEDIR/$PS_PRODUCT_DIR/web-src/WEB-INF/classes/Profiler.properties-template"
if [ -f $FILE ]; then
  TARGET_FILE="$PS_BASEDIR/$PS_PRODUCT_DIR/web-src/WEB-INF/classes/Profiler.properties"
  if [ "$OSTYPE" = "SunOS" ]; then
    $INSTALLF "SUNWpssub" $TARGET_FILE
  fi
  $CAT $FILE | $SED -e "s!%LOAD_BALANCER_URL%!$LOAD_BALANCER_URL!" | $SED -e "s!%PS_HOST%!$SERVER_HOST!" | $SED -e "s!%SMTP_SERVER%!$SERVER_HOST!" > $TARGET_FILE
  if [ "$OSTYPE" = "SunOS" ]; then
    $INSTALLF -f "SUNWpssub"
  fi
else
  $ECHO "$FILE doesn't exist"
  exit 1
fi

exit 0
