#!/bin/sh
#
# Database Creation - IBM WebServices Gateway
#
# The arguments are:
# "1: Database name"
# "2: SQL definition file"

progName=`basename "$0"`

if [ $# -ne 2 ]; then
  echo 'ERROR: The following arguments are required:' >&2
  echo '  1: Database name' >&2
  echo '  2: SQL definition file' >&2
  echo "Example: ./${progName} wsgwdb /tmp/WSGWDB.sql" >&2
  exit 1
fi

dbName="$1"
sqlFile="$2"

echo 'Terminating db2...'
db2 terminate || exit $?

echo 'Dropping database...'
# Don't check the return value here as wsgwdb might not be there.
db2 drop database ${dbName}

echo 'Creating Database...'
db2 create database ${dbName} || exit $?

echo 'Connecting to Database...'
db2 connect to ${dbName} || exit $?

echo "Importing ${sqlFile}..."
db2 -tvf ${sqlFile} || exit $?

echo 'Terminating DB2...'
db2 terminate || exit $?

# Do NOT change this message.  It must be the last message output by this
# script for the calling script to test against for success.
echo 'WSGW database creation successful'
exit 0
