#!/bin/ksh

####################################################################################
# Defines.
####################################################################################

AWK=/usr/bin/awk
ECHO=/usr/bin/echo
GREP=/usr/bin/grep
ID=/usr/bin/id
RM=/usr/bin/rm
SED=/usr/bin/sed
UNIQ=/usr/bin/uniq

OSTYPE=`/bin/uname -s`
if [ "$OSTYPE" = "Linux" ]; then
  ECHO=/bin/echo
  GREP=/bin/grep
  RM=/bin/rm
  SED=/bin/sed
fi

BELL_CHAR='\a'

PS_STATE_FILE="/etc/opt/SUNWps/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/SUNWps/bin/dpadmin"
    
    FILE="/etc/opt/SUNWam/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 channel from MyFrontPage.
####################################################################################

RemoveNetletChannel() {

    $ECHO "Removing netlet channel..."
    $DPADMIN remove -t channel -n "Netlet" -u "$ADMIN_DN" -w "${IDSAME_ADMIN_PASSWORD}" -d "$ORG_DN"

}

####################################################################################
# Remove netfile links from App channel. Currently not supported by dpadmin.
####################################################################################

# RemoveNetfileFromAppChannel() {
	

# }

####################################################################################
# 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 channel from MyFrontPage.
RemoveNetletChannel

# Remove netfile links from App channel.
# RemoveNetfileFromAppChannel

exit 0
