#!/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 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=

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
      exit 1
   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#

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

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

LOG_DEVICE=

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 

# 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
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"


#  Create event service tables 
"$JAVA_HOME/bin/java" -classpath $DERBY_CLASSPATH -Dderby.system.home=$SYSTEM_DIR -Dij.protocol=jdbc:derby: -Dij.database="$DB_NAME;create=true$LOG_DEVICE" 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: -Dij.database=$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: -Dij.database="$CATALOG_DB_NAME;create=true$LOG_DEVICE" 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: -Dij.database=$CATALOG_DB_NAME org.apache.derby.tools.ij $CATALOG_SEED_DDL

exit $?
