#!/bin/ksh

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

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

BELL_CHAR='\a'

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

###############################################
# Delete line in file
###############################################
DeleteLine() {
  local FILE=$1
  local MATCH=$2

  $CP -p $FILE $FILE-tmp
  $SED -e "/$MATCH/d" $FILE-tmp > $FILE
  $RM $FILE-tmp
}

###############################################
# Undeploy
###############################################
Undeploy() {
  local NAME=$1

  $ECHO "Undeploying $NAME..."
  if [ "$DEPLOY_TYPE" = "SUNONE" ]; then
    eval $DEPLOY_DIR/bin/asadmin undeploy -u $DEPLOY_ADMIN -w "${DEPLOY_ADMIN_PASSWORD}" -p $DEPLOY_ADMIN_PORT -H $SERVER_HOST --instance $DEPLOY_INSTANCE --type web $NAME
  elif [ "$DEPLOY_TYPE" = "WEBLOGIC" ]; then
    eval $JAVA_HOME/bin/java -classpath $DEPLOY_DIR/lib/weblogic.jar weblogic.deploy -url $DEPLOY_ADMIN_PROTOCOL://$DEPLOY_ADMIN_HOST:$DEPLOY_ADMIN_PORT -component $NAME:$DEPLOY_INSTANCE delete "${DEPLOY_ADMIN_PASSWORD}" $NAME
  elif [ "$DEPLOY_TYPE" = "WEBSPHERE" ]; then
    $ECHO "
EnterpriseApp stop /EnterpriseApp:$NAME/
EnterpriseApp remove /EnterpriseApp:$NAME/ -recursive
" > /tmp/undeploy.tcl
    eval $DEPLOY_DIR/bin/wscp.sh -f /tmp/undeploy.tcl
    eval $DEPLOY_DIR/bin/GenPluginCfg.sh -adminNodeName $SERVER_HOST
  elif [ "$DEPLOY_TYPE" = "IWS" ]; then
    IWS_SERVER_HOME="$DEPLOY_DIR"
    export IWS_SERVER_HOME

    eval $IWS_SERVER_HOME/bin/https/httpadmin/bin/wdeploy delete -u /$NAME -i $DEPLOY_INSTANCE -v "https-$DEPLOY_INSTANCE" hard
  fi
}

###############################################
# 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 "JDK_DIR" "="
JAVA_HOME=$ANSWER

GrabConfig $STATE_FILE "DEPLOY_TYPE" "="
DEPLOY_TYPE=$ANSWER

if [ "$DEPLOY_TYPE" = "SUNONE" ]; then
  GrabConfig $STATE_FILE "DEPLOY_DIR" "="
  DEPLOY_DIR=$ANSWER

  GrabConfig $STATE_FILE "DEPLOY_DOMAIN" "="
  DEPLOY_DOMAIN=$ANSWER

  GrabConfig $STATE_FILE "DEPLOY_INSTANCE" "="
  DEPLOY_INSTANCE=$ANSWER

  GrabConfig $STATE_FILE "DEPLOY_ADMIN" "="
  DEPLOY_ADMIN=$ANSWER

  GrabConfig $STATE_FILE "DEPLOY_ADMIN_PORT" "="
  DEPLOY_ADMIN_PORT=$ANSWER
elif [ "$DEPLOY_TYPE" = "WEBLOGIC" ]; then
  GrabConfig $STATE_FILE "DEPLOY_DIR" "="
  DEPLOY_DIR=$ANSWER

  GrabConfig $STATE_FILE "DEPLOY_DOMAIN" "="
  DEPLOY_DOMAIN=$ANSWER

  GrabConfig $STATE_FILE "DEPLOY_INSTANCE" "="
  DEPLOY_INSTANCE=$ANSWER

  GrabConfig $STATE_FILE "DEPLOY_ADMIN_PROTOCOL" "="
  DEPLOY_ADMIN_PROTOCOL=$ANSWER

  GrabConfig $STATE_FILE "DEPLOY_ADMIN_HOST" "="
  DEPLOY_ADMIN_HOST=$ANSWER

  GrabConfig $STATE_FILE "DEPLOY_ADMIN_PORT" "="
  DEPLOY_ADMIN_PORT=$ANSWER
elif [ "$DEPLOY_TYPE" = "WEBSPHERE" ]; then
  GrabConfig $STATE_FILE "DEPLOY_DIR" "="
  DEPLOY_DIR=$ANSWER

  GrabConfig $STATE_FILE "DEPLOY_VIRTUAL_HOST" "="
  DEPLOY_VIRTUAL_HOST=$ANSWER

  GrabConfig $STATE_FILE "DEPLOY_NODE" "="
  DEPLOY_NODE=$ANSWER

  GrabConfig $STATE_FILE "DEPLOY_INSTANCE" "="
  DEPLOY_INSTANCE=$ANSWER
else
  GrabConfig $STATE_FILE "DEPLOY_DIR" "="
  DEPLOY_DIR=$ANSWER

  GrabConfig $STATE_FILE "DEPLOY_INSTANCE" "="
  DEPLOY_INSTANCE=$ANSWER
fi

GrabConfig $STATE_FILE "IDSAME_BASEDIR" "="
IDSAME_BASEDIR=$ANSWER

GrabConfig $STATE_FILE "BASEDIR" "="
PS_BASEDIR=$ANSWER

GrabConfig $STATE_FILE "SERVER_PROTOCOL" "="
SERVER_PROTOCOL=$ANSWER

GrabConfig $STATE_FILE "SERVER_HOST" "="
SERVER_HOST=$ANSWER

GrabConfig $STATE_FILE "SERVER_PORT" "="
SERVER_PORT=$ANSWER

PORTLETDEPLOYER="$PS_BASEDIR/$PS_PRODUCT_DIR/bin/pdeploy"

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

if [ -x $PORTLETDEPLOYER ]; then
  DIRS=`$LS $PS_VAR_DIR/tmp/portlet/*_portlet.xml`
  for DIR in $DIRS; do
    Undeploy `$BASENAME $DIR | $SED -e "s/_portlet.xml//"`
  done
fi

exit 0
