#!/usr/bin/qsh
#
#  IBM Confidential OCO Source Material
#
#  5724-I63, 5724-H88, 5655-N02, 5733-W70 (C) COPYRIGHT International Business Machines Corp. 2004, 2005
#
#  The source code for this program is not published or otherwise divested
#  of its trade secrets, irrespective of what has been deposited with the
#  U.S. Copyright Office.
#
# -----------------------------------------------------------------------------
# -- This batch file removes the Event Service database 
# -- 
# -- Usage:
# -- rm_event_derby [-h | -help | -p <was_profile_path> \
#                   [-s <server_name> | -c <cluster_name>] ]
# --
# -- Example 1: 
# -- rm_event_derby -p /WebSphere/Appserver/profiles/myprofile -s server1"
# --
# -- Example 2: 
# -- rm_event_derby -p /Websphere/Appserver/profiles/myprofile -c mycluster"
# -- 
# ----------------------------------------------------------------------------

#-----------------------------------------
# Print usage
#-----------------------------------------
print_usage() {
   echo "Usage: rm_event_derby [-h | -help | -p <was_profile_path> \
                   [-s <server_name> | -c <cluster_name>] ]"
   echo ""
   echo "Example 1:"
   echo "rm_event_derby -p /WebSphere/Appserver/profiles/myprofile -s server1"
   echo ""
   echo "Example 2:"
   echo "rm_event_derby -p /Websphere/Appserver/profiles/myprofile -c mycluster"
   echo ""
   exit 1
}

# Initialized
SERVER_NAME=
CLUSTER_NAME=

if [ "$1" = "" ]; then
   # Generated WebSphere Information
   WAS_HOME=##WAS_HOME#
   WAS_NODE=##NODE_NAME#
   SERVER_NAME=##SERVER_NAME#
   CLUSTER_NAME=##CLUSTER_NAME#
   PROFILE_PATH=##WAS_PROFILE_PATH#
else
   # Get user input
   if [ "$1" = "-p" ]; then

      if [ "$2" = "" ]; then
         print_usage
      else
         PROFILE_PATH=$2
         if [ "$3" = "-s" ]; then
            # Server parameter is specified
            if [ "$4" = "" ]; then
               print_usage
            else
               # Set server name
               SERVER_NAME=$4
            fi
         else
            if [ "$3" = "-c" ]; then
               # Cluster parameter is specified
               if [ "$4" = "" ]; then
                  print_usage
               else
                  # Set cluster name
                  CLUSTER_NAME=$4
               fi
            else
               print_usage
            fi
         fi 
      fi
   else
      print_usage
   fi 
fi

# Setup WebSphere environment
. $PROFILE_PATH/bin/setupCmdLine

DB_DIR=##DB_DIR#

# The Derby system directory. 
if [ "$CLUSTER_NAME" = "" ]; then 
   SYSTEM_DIR=$USER_INSTALL_ROOT/databases/event/$WAS_NODE/$SERVER_NAME/$DB_DIR
else
   SYSTEM_DIR=$USER_INSTALL_ROOT/databases/event/$CLUSTER_NAME/$DB_DIR
fi

if [ -d "$SYSTEM_DIR" ] ; then
   rm -rf "$SYSTEM_DIR"
   echo "The Derby database $SYSTEM_DIR has been removed successfully."
else
   echo "The Derby database $SYSTEM_DIR does not exist."
fi

exit $?
