#!/bin/bash
#set -x

 Usage of the file
if [[ $# -ne 3 ]]
then
echo "Usage "
echo $0 " --db=dbname --instowner=instanceowner --cpath=containerpath"
echo $0 " --db=pimdb --instowner=db2inst1 --cpath=/u01"
 exit 0
fi
echo "Last update of script: MDMPIM 6.0.0-FP006 2009-03-19"

DBName=`echo $1 |cut -d "=" -f2`;export DBName
INSTOwner=`echo $2 |cut -d "=" -f2`;export INSTOwner
DBDir=`echo $3 |cut -d "=" -f2`;export DBDir

mkdir -p ${DBDir}/db2data/${DBName}/logs

# This script is used to create MDMPIM database

echo "------------------------------------------------------------------------"
echo "Attaching to the instance ${INSTowner} and starting the db instance"
echo "*** IGNORE SQL1026N message if the db manager is already active ***"
echo "------------------------------------------------------------------------"

db2 -v "attach to ${INSTOwner}"
db2start

echo "------------------------------------------------------------------------"
echo "Creating the database ${DBName} with UTF-8 Character Set"
echo "------------------------------------------------------------------------"

db2 -v "CREATE DATABASE ${DBName} AUTOMATIC STORAGE YES ON '${DBDir}/db2data/${DBName}' USING CODESET UTF-8 TERRITORY US"
db2 -v "activate database ${DBName}"

echo "------------------------------------------------------------------------"
echo "Creating MDMPIM specific bufferpools"
echo "*** IGNORE SQL20189W messages ***" 
echo "------------------------------------------------------------------------"

db2 -v "connect to ${DBName}"
db2 -tvf bp-dbm-db-cfg-3tsp.sql

echo "------------------------------------------------------------------------"
echo "Creating MDMPIM Specific tablespaces"
echo "*** It takes time to create the tablespaces. ***"
echo "*** Be patient. DO NOT kill the statement while running ***"
echo "------------------------------------------------------------------------"
db2 -v "CREATE REGULAR TABLESPACE USERS PAGESIZE 16K MANAGED BY AUTOMATIC STORAGE BUFFERPOOL USERSBP NO FILE SYSTEM CACHING"
db2 -v "CREATE REGULAR TABLESPACE INDX PAGESIZE 16K MANAGED BY AUTOMATIC STORAGE BUFFERPOOL INDXBP NO FILE SYSTEM CACHING"
db2 -v "CREATE REGULAR TABLESPACE BLOB_TBL_DATA PAGESIZE 16K MANAGED BY AUTOMATIC STORAGE BUFFERPOOL BLOBBP NO FILE SYSTEM CACHING"
db2 -v "CREATE USER TEMPORARY TABLESPACE TEMP_USER PAGESIZE 32K MANAGED BY SYSTEM USING ('/${DBDir}/${DBName}/usertemp01') EXTENTSIZE 32 PREFETCHSIZE 32 BUFFERPOOL TEMPUSRBP"
db2 -v "CREATE SYSTEM TEMPORARY TABLESPACE TEMP_SYSTEM PAGESIZE 32K MANAGED BY SYSTEM USING ('/${DBDir}/${DBName}/systemtemp01') EXTENTSIZE 32 PREFETCHSIZE 32 BUFFERPOOL TEMPSYSBP"

echo "------------------------------------------------------------------------"
echo "Updating Transaction Logs Specific Configuration parameters"
echo "*** IGNORE SQL1363W messages ***"
echo "------------------------------------------------------------------------"

db2 -v "update db cfg for ${DBName} using NEWLOGPATH ${DBDir}/${DBName}/logs"
db2 -v "connect reset"

echo "------------------------------------------------------------------------"
echo "Updating DB2 Registry Variables"
echo "------------------------------------------------------------------------"

db2set DB2ENVLIST=EXTSHM
db2set DB2_RR_TO_RS=ON
db2set DB2COMM=tcpip
db2set DB2CODEPAGE=1208
db2set DB2AUTOSTART=yes
db2set DB2_SKIPINSERTED=ON
db2set DB2_EVALUNCOMMITTED=ON
db2set DB2_SKIPDELETED=ON
db2set DB2_PARALLEL_IO=*

echo "------------------------------------------------------------------------"
echo "Updating DB2 Registry Variables is DONE"
echo "Bouncing the database to apply all the parameter changes"
echo "------------------------------------------------------------------------"

db2stop force
db2start

echo "**************************************************************************"
echo "*** Following statement is going to take time to activate the database ***"
echo "*** DO NOT INTERRRUPT the command and DO NOT KILL the statement ***"
echo "**************************************************************************"

db2 -v "activate database ${DBName}"

echo "------------------------------------------------------------------------"
echo "${DBName} Database creation script completed. Check for errors"
echo "------------------------------------------------------------------------"
