#!/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 creates the Event Service database 
# -- 
# -- Usage:
# -- cr_event_derby [-h | -help | -p <was_profile_path> \
#                   [-s <server_name> | -c <cluster_name>] ]
# --
# -- Example 1: 
# -- cr_event_derby -p /WebSphere/Appserver/profiles/myprofile -s server1"
# --
# -- Example 2: 
# -- cr_event_derby -p /Websphere/Appserver/profiles/myprofile -c mycluster"
# -- 
# ----------------------------------------------------------------------------

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

# Initialize
CURRENT_DIR=`dirname $0`
if [ "$CURRENT_DIR" = "." ] ; then
   CURRENT_DIR=`pwd`
fi

SERVER_NAME=
CLUSTER_NAME=
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

# Event Serivce Derby database directory
DB_DIR=##DB_DIR#

# Location of the transaction log device
TRANSACTION_LOG_DEVICE=##TRANSACTION_LOG_DEVICE#

# The event service database Name
DB_NAME=##DB_NAME#

# The event catalog database Name
CATALOG_DB_NAME=##CATALOG_DB_NAME#

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 Derby system directory. 
# Databases are created in directories underneath the 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

# The number of memory pages to use to cache data. The Derby default is 40.
PAGE_CACHE_SIZE=4000

LOG_DEVICE=
# Check the existence of the database only if dbPath was not specified
if [ "$DB_PATH" = "" ]; then
	if [ -d $SYSTEM_DIR/$DB_NAME ] ; then
	   echo "The database $DB_NAME already exists in $SYSTEM_DIR"
	   exit 1;
	fi 
	
	if [ -d $SYSTEM_DIR/$CATALOG_DB_NAME ] ; then
	   echo "The database $CATALOG_DB_NAME already exists in $SYSTEM_DIR"
	   exit 1;
	fi 
	
	# Set the create database flag
    CREATE_DB=";create=true$LOG_DEVICE"

	# set the network server connection url
	DERBYURL=

	#set the network server database name
	DBNAMEURL=
else
	# DBPath was specified and we are expecting to use an existing database
	#those environment variables will be used in the derby ij connection url
    SYSTEM_DIR=$DB_PATH
    CREATE_DB=
    DERBYURL=//$DBHOST:$DBPORT/
    DBNAMEURL=$DB_PATH/
    
    #Check if the cei tables already exists in db
    DDLF="$CURRENT_DIR/test.ddl"
    DDLLOG="$CURRENT_DIR/test.log"
    
    #Try to select a row from an CEI table and first
    #we need to generate the ddl script 
    echo "select * from CEI.CEI_T_PROPERTIES;" > $DDLF
    echo "exit;" >> $DDLF
    
    #If the table exist then we will get the header of the column PROPERTY_NAME
    "$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 $DDLF > $DDLLOG
    
    #Search for the column header PROPERTY_NAME
    cat $DDLLOG |grep "PROPERTY_NAME" > /dev/null
    
    #Store the result code, 0 means that the database exists
    rc=$?
	#delete the created temporary files    
    rm -f $DDLF
    rm -f $DDLLOG

	#exit from the script and display an error message 
    if [ $rc -eq 0 ] ; then 
       echo "The database $DB_NAME already exists in $SYSTEM_DIR and contains the Common Event Infrastructure tables"
	   exit 1;
	fi 
fi

# Use transaction log path if specified
if [ ! "$TRANSACTION_LOG_DEVICE" = "" ] ; then 
  # The log device to use. Placing the logs onto a separate device can greatly improve performance, but complicates backup/recovery
  LOG_DEVICE=";logDevice=$TRANSACTION_LOG_DEVICE"
fi

CR_TBL_DDL=$CURRENT_DIR/cr_tbl.derby
CR_TBL_CATALOG_DDL=$CURRENT_DIR/cr_tbl_catalog.derby
METADATA_DDL=$CURRENT_DIR/ins_metadata.derby
CATALOG_SEED_DDL=$CURRENT_DIR/catalogSeed.derby


#  Create event service tables 
"$JAVA_HOME/bin/java" -classpath $DERBY_CLASSPATH -Dderby.system.home=$SYSTEM_DIR -Dij.protocol=jdbc:derby:$DERBYURL -Dij.database="$DBNAMEURL$DB_NAME$CREATE_DB" org.apache.derby.tools.ij $CR_TBL_DDL

# Now populate event service tables 
"$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 $METADATA_DDL

# Create event catalog tables 
"$JAVA_HOME/bin/java" -classpath $DERBY_CLASSPATH -Dderby.system.home=$SYSTEM_DIR -Dij.protocol=jdbc:derby:$DERBYURL -Dij.database="$DBNAMEURL$CATALOG_DB_NAME$CREATE_DB" org.apache.derby.tools.ij $CR_TBL_CATALOG_DDL

# Now populate event catalog tables 
"$JAVA_HOME/bin/java" -classpath $DERBY_CLASSPATH -Dderby.system.home=$SYSTEM_DIR -Dij.protocol=jdbc:derby:$DERBYURL -Dij.database="$DBNAMEURL$CATALOG_DB_NAME" org.apache.derby.tools.ij $CATALOG_SEED_DDL

exit $?
