#!/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() {

  GRABCONFIG_FILE=$1
  GRABCONFIG_KEY=$2
  GRABCONFIG_SEPARATOR=$3

  ANSWER=`$GREP "^$GRABCONFIG_KEY$GRABCONFIG_SEPARATOR" $GRABCONFIG_FILE | $UNIQ | $SED -e "s/$GRABCONFIG_KEY$GRABCONFIG_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

}

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

CheckStateFiles() {

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

}

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

Initialize() {

    # 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

    # 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

    # 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

}

####################################################################################
# Remove netlet provider.
####################################################################################

RemoveNetletProvider() {

    $ECHO "Removing netlet provider..."
    $DPADMIN remove -t provider -n "NetletProvider" -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -g

}

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

# Make sure that the user is root
CheckUser

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

# Initialize the global variables.
Initialize

# Remove netlet provider.
RemoveNetletProvider

exit 0
