#!/usr/bin/qsh
#
# Licensed Materials - Property of IBM
# (C) Copyright IBM Corp. 2004, 2010.  ALL RIGHTS RESERVED 
# 5724-I63, 5724-H88, 5655-N02, 5733-W70
# US Government Users Restricted Rights - Use, duplication, or disclosure
# restricted by GSA ADP Schedule Contract with IBM Corp.
#
# ----------------------------------------------------------------------------
#
# -- 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=
DB_PATH=
DBHOST=
DBPORT=
NODE_NAME_ND=
DB_PATH=##dbPath#
DBHOST=##dbHostName#
DBPORT=##dbPort#
NODE_NAME_ND=##nodeName#

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 
   if [ "$NODE_NAME_ND" = "" ]; then
   SYSTEM_DIR=$USER_INSTALL_ROOT/databases/event/$WAS_NODE/$SERVER_NAME/$DB_DIR
   else
   SYSTEM_DIR=$USER_INSTALL_ROOT/databases/event/$NODE_NAME_ND/$SERVER_NAME/$DB_DIR
   fi
else
   SYSTEM_DIR=$USER_INSTALL_ROOT/databases/event/$CLUSTER_NAME/$DB_DIR
fi

#Remove the db folder only if dbPath was not specified
if [ "$DB_PATH" = "" ]; then
	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."
	   exit 124
	fi
else
	#DBPath was specified and we need to drop the tables form the existing database

	if [ "$CLUSTER_NAME" = "" ]; then 
      SYSTEM_DIR=$USER_INSTALL_ROOT/databases/event/$WAS_NODE/$SERVER_NAME
    else
      SYSTEM_DIR=$USER_INSTALL_ROOT/databases/event/$CLUSTER_NAME
    fi
	
	#Set up the folder where the sripts will be generated
	SCRIPTS_DIR=$SYSTEM_DIR/dbscripts/derby
	
	#Set up the system dir to be dbPath
	SYSTEM_DIR=$DB_PATH
    
    #Set up the connection URL 
    DERBYURL=//$DBHOST:$DBPORT/
    DBNAMEURL=$DB_PATH/
    DERBY_CLASSPATH="$WAS_HOME/derby/lib/derby.jar:$WAS_HOME/derby/lib/derbytools.jar:$WAS_HOME/derby/lib/derbyclient.jar:$WAS_HOME/derby/lib/derbynet.jar"
    
    #The file will contain the result of the drop command
    LOGFILE="$SCRIPTS_DIR/test.log"
    
    #Run the derby script that drops the CEI tables and schema
    "$JAVA_HOME/bin/java" -classpath $DERBY_CLASSPATH -Dderby.system.home=$SYSTEM_DIR -Dij.protocol=jdbc:derby:$DERBYURL -Dij.database="$DBNAMEURL$DB_NAME" org.apache.derby.tools.ij "$SCRIPTS_DIR/drop_db.derby" > $LOGFILE
    
    #Search for the text ERROR in order to determine if there was an error at database remove
    cat $LOGFILE |grep "ERROR" > /dev/null
    
    #Store the result code, 0 means that there was an error 
    rc=$?
	#delete the created temporary files    
    rm -f $LOGFILE
    
    if [ $rc -eq 0 ] ; then 
    	echo "Failed to remove the Derby Event Service database $SYSTEM_DIR/$DB_NAME"
    	exit 2
    fi
fi

exit $?
