#!/bin/ksh

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

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

BELL_CHAR='\a'

PS_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//"`

}

####################################################################################
# Make sure that the user is root.
####################################################################################

CheckUser() {

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

}

####################################################################################
# Make sure required state files are present.
####################################################################################

CheckStateFiles() {

    if [ ! -f $PS_STATE_FILE ]; then
      $ECHO "Error: $PS_STATE_FILE does not exist. $BELL_CHAR"
      exit 1
    fi

}

####################################################################################
# Initialize all global variables.
####################################################################################

Initialize() {

    # Identity server base directory.
    GrabConfig $PS_STATE_FILE "IDSAME_BASEDIR" "="
    if [ "$ANSWER" != "" ]; then
      IDSAME_BASEDIR=$ANSWER
    else
      $ECHO "Error: Cannot determine IDSAME_BASEDIR. $BELL_CHAR"
      exit 1
    fi
    
    # Portal server base directory.
    GrabConfig $PS_STATE_FILE "BASEDIR" "="
    if [ "$ANSWER" != "" ]; then
      PS_BASEDIR=$ANSWER
    else
      $ECHO "Error: Cannot determine BASEDIR. $BELL_CHAR"
      exit 1
    fi
    
    # Others.
    DPADMIN="$PS_BASEDIR/$PS_PRODUCT_DIR/bin/dpadmin"
    
    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

}

####################################################################################
# Add netfile links to application channel.
####################################################################################

AddNetfileToAppChannel() {

    FILE="$PS_BASEDIR/$PS_PRODUCT_DIR/samples/netfile/netfiledp.xml"
    $ECHO "Loading $FILE..."
    $DPADMIN modify -m -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -d "$ORG_DN" -p App $FILE

}

####################################################################################
# Add netlet channel to display profile
####################################################################################

AddNetletChannelToDP() {

    FILE="$PS_BASEDIR/$PS_PRODUCT_DIR/samples/netlet/netletChannel.xml"
    $ECHO "Loading $FILE..."
    $DPADMIN add -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -d "$ORG_DN" $FILE
    
}

####################################################################################
# Add netlet channel to all required containers.
####################################################################################

AddNetletToAllContainers() {

    NETLET_SELECTED="$PS_BASEDIR/$PS_PRODUCT_DIR/samples/netlet/netletSelected.xml"
    NETLET_AVAILABLE="$PS_BASEDIR/$PS_PRODUCT_DIR/samples/netlet/netletAvailable.xml"

    $ECHO "Adding Netlet to MyFrontPageTabPanelContainer ..."
    $DPADMIN modify -m -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -d "$ORG_DN" -p MyFrontPageTabPanelContainer $NETLET_AVAILABLE
    $DPADMIN modify -m -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -d "$ORG_DN" -p MyFrontPageTabPanelContainer $NETLET_SELECTED

    $ECHO "Adding Netlet to JSPTabCustomTableContainer ..."
    $DPADMIN modify -m -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -d "$ORG_DN" -p JSPTabCustomTableContainer $NETLET_AVAILABLE

    $ECHO "Adding Netlet to FrameCustomTableContainer ..."
    $DPADMIN modify -m -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -d "$ORG_DN" -p FrameCustomTableContainer $NETLET_AVAILABLE

    $ECHO "Adding Netlet to MyFrontPageFramePanelContainer ..."
    $DPADMIN modify -m -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -d "$ORG_DN" -p MyFrontPageFramePanelContainer $NETLET_AVAILABLE

    $ECHO "Adding Netlet to MyFrontPageTemplatePanelContainer ..."
    $DPADMIN modify -m -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -d "$ORG_DN" -p MyFrontPageTemplatePanelContainer $NETLET_AVAILABLE

    $ECHO "Adding Netlet to TemplateTabCustomTableContainer ..."
    $DPADMIN modify -m -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -d "$ORG_DN" -p TemplateTabCustomTableContainer $NETLET_AVAILABLE

    $ECHO "Adding Netlet to ToolsTemplatePanelContainer ..."
    $DPADMIN modify -m -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -d "$ORG_DN" -p ToolsTemplatePanelContainer $NETLET_AVAILABLE

    $ECHO "Adding Netlet to PredefinedFrontPageTabPanelContainer ..."
    $DPADMIN modify -m -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -d "$ORG_DN" -p PredefinedFrontPageTabPanelContainer $NETLET_AVAILABLE

    $ECHO "Adding Netlet to PredefinedFrontPageFramePanelContainer ..."
    $DPADMIN modify -m -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -d "$ORG_DN" -p PredefinedFrontPageFramePanelContainer $NETLET_AVAILABLE

    $ECHO "Adding Netlet to PredefinedFrontPageTemplatePanelContainer ..."
    $DPADMIN modify -m -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -d "$ORG_DN" -p PredefinedFrontPageTemplatePanelContainer $NETLET_AVAILABLE

    $ECHO "Adding Netlet to PredefinedToolsTemplatePanelContainer ..."
    $DPADMIN modify -m -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -d "$ORG_DN" -p PredefinedToolsTemplatePanelContainer $NETLET_AVAILABLE

}

####################################################################################
# Add proxylet channel to display profile
####################################################################################

AddProxyletChannelToDP() {

    FILE="$PS_BASEDIR/$PS_PRODUCT_DIR/samples/proxylet/proxyletChannel.xml"
    $ECHO "Loading $FILE..."
    $DPADMIN add -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -d "$ORG_DN" $FILE
    
}

####################################################################################
# Add proxylet channel to all required containers.
####################################################################################

AddProxyletToAllContainers() {

    PROXYLET_SELECTED="$PS_BASEDIR/$PS_PRODUCT_DIR/samples/proxylet/proxyletSelected.xml"
    PROXYLET_AVAILABLE="$PS_BASEDIR/$PS_PRODUCT_DIR/samples/proxylet/proxyletAvailable.xml"

    $ECHO "Adding Proxylet to MyFrontPageTabPanelContainer ..."
    $DPADMIN modify -m -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -d "$ORG_DN" -p MyFrontPageTabPanelContainer $PROXYLET_AVAILABLE
    $DPADMIN modify -m -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -d "$ORG_DN" -p MyFrontPageTabPanelContainer $PROXYLET_SELECTED

    $ECHO "Adding Proxylet to JSPTabCustomTableContainer ..."
    $DPADMIN modify -m -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -d "$ORG_DN" -p JSPTabCustomTableContainer $PROXYLET_AVAILABLE

    $ECHO "Adding Proxylet to FrameCustomTableContainer ..."
    $DPADMIN modify -m -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -d "$ORG_DN" -p FrameCustomTableContainer $PROXYLET_AVAILABLE

    $ECHO "Adding Proxylet to MyFrontPageFramePanelContainer ..."
    $DPADMIN modify -m -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -d "$ORG_DN" -p MyFrontPageFramePanelContainer $PROXYLET_AVAILABLE

    $ECHO "Adding Proxylet to MyFrontPageTemplatePanelContainer ..."
    $DPADMIN modify -m -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -d "$ORG_DN" -p MyFrontPageTemplatePanelContainer $PROXYLET_AVAILABLE

    $ECHO "Adding Proxylet to TemplateTabCustomTableContainer ..."
    $DPADMIN modify -m -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -d "$ORG_DN" -p TemplateTabCustomTableContainer $PROXYLET_AVAILABLE

    $ECHO "Adding Proxylet to ToolsTemplatePanelContainer ..."
    $DPADMIN modify -m -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -d "$ORG_DN" -p ToolsTemplatePanelContainer $PROXYLET_AVAILABLE

    $ECHO "Adding Proxylet to PredefinedFrontPageTabPanelContainer ..."
    $DPADMIN modify -m -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -d "$ORG_DN" -p PredefinedFrontPageTabPanelContainer $PROXYLET_AVAILABLE

    $ECHO "Adding Proxylet to PredefinedFrontPageFramePanelContainer ..."
    $DPADMIN modify -m -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -d "$ORG_DN" -p PredefinedFrontPageFramePanelContainer $PROXYLET_AVAILABLE

    $ECHO "Adding Proxylet to PredefinedFrontPageTemplatePanelContainer ..."
    $DPADMIN modify -m -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -d "$ORG_DN" -p PredefinedFrontPageTemplatePanelContainer $PROXYLET_AVAILABLE

    $ECHO "Adding Proxylet to PredefinedToolsTemplatePanelContainer ..."
    $DPADMIN modify -m -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -d "$ORG_DN" -p PredefinedToolsTemplatePanelContainer $PROXYLET_AVAILABLE

}

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

# Make sure that the user is root.
CheckUser

# Make sure that the required state files are present.
CheckStateFiles

# Initialize all global variables.
Initialize

# Add netfile links to Application channel.
AddNetfileToAppChannel

# Add netlet channel to display profile.
AddNetletChannelToDP

# Add netlet channel to all required containers.
AddNetletToAllContainers

# Add proxylet channel to display profile.
AddProxyletChannelToDP

# Add proxylet channel to all required containers.
AddProxyletToAllContainers

exit 0
