#!/bin/sh
# =======================================================================
# Aperi Database Creation/Drop Script
# =======================================================================
# Helper Functions
# =======================================================================

usage(){
    echo
    echo "db2cmd /c db2config.sh DB2AdminID DB2AdminPassword DBName CDFlag LogSize LogLocation"
    echo "                          SHEAPTHRES DBHEAP APPLHEAPSZ APP_CTL_HEAP_SZ APPGROUP_MEM_SZ"
    echo "                          SORTHEAP STMTHEAP MAXLOCKS LOCKLIST MAXAPPLS LOGPRIMARY LOGSECOND"
    echo "where:"
    echo "   DB2AdminID       = the DB2 Administrator user ID"
    echo "   DB2AdminPassword = the DB2 Administrator password"
    echo "   DBName           = the IBM Aperi Database name"
    echo "   CDFlag           = the Create/Drop flag"
    echo "   LogSize          = size of db log file"
    echo "   LogPath          = path to db log file"
    echo "   DB2 CFG ARGs     = SHEAPTHRES, DBHEAP, APPLHEAPSZ, APP_CTL_HEAP_SZ, APPGROUP_MEM_SZ,"
    echo "   		        SORTHEAP, STMTHEAP, MAXLOCKS, LOCKLIST, MAXAPPLS, LOGPRIMARY, LOGSECOND"
    echo "   The IBM Aperi database will be enabled for on-line backups."
    touch /tmp/done
    exit 1
}

errExit(){
    if test "$DBEXISTS" = "NO"
    then
        echo Dropping $DBNAME database because an error was encountered
        db2 drop database $DBNAME
        echo $DBNAME database dropped
    fi
    echo IBM Aperi database creation or configuration failed.
    touch /tmp/done
    exit 1
}

configDB(){

    db2 list application show detail

# default for SHEAPTHRES = 65537
    db2 update dbm cfg using SHEAPTHRES $SHEAPTHRES
    if test $? -ge 4; then errExit; fi

# default for DBHEAP = 1000
    db2 update db cfg for $DBNAME using DBHEAP $DBHEAP
    if test $? -ge 4; then errExit; fi

# default for APPLHEAPSZ = 10240
    db2 update db cfg for $DBNAME using APPLHEAPSZ $APPLHEAPSZ
    if test $? -ge 4; then errExit; fi

# default for APP_CTL_HEAP_SZ = 1024
    db2 update db cfg for $DBNAME using APP_CTL_HEAP_SZ $APP_CTL_HEAP_SZ
    if test $? -ge 4; then errExit; fi

# default for APPGROUP_MEM_SZ = 40000
    db2 update db cfg for $DBNAME using APPGROUP_MEM_SZ $APPGROUP_MEM_SZ
    if test $? -ge 4; then errExit; fi

# default for SORTHEAP = 4096
    db2 update db cfg for $DBNAME using SORTHEAP $SORTHEAP
    if test $? -ge 4; then errExit; fi

# default for STMTHEAP = 10240
    db2 update db cfg for $DBNAME using STMTHEAP $STMTHEAP
    if test $? -ge 4; then errExit; fi

# default for MAXLOCKS = 50
    db2 update db cfg for $DBNAME using MAXLOCKS $MAXLOCKS
    if test $? -ge 4; then errExit; fi

# default for LOCKLIST = 10000
    db2 update db cfg for $DBNAME using LOCKLIST $LOCKLIST
    if test $? -ge 4; then errExit; fi

# default for MAXAPPLS = 150
    db2 update db cfg for $DBNAME using MAXAPPLS $MAXAPPLS
    if test $? -ge 4; then errExit; fi

# default for logfilsiz = 2500
    db2 update db cfg for $DBNAME using logfilsiz $LOGSIZE
    if test $? -ge 4; then errExit; fi

# default for newlogpath = ""
    if [ "$LOGPATH" != "-1" ]
    then
	chmod o=rwx $LOGPATH
	db2 update db cfg for $DBNAME using newlogpath $LOGPATH
    fi
    if test $? -ge 4; then errExit; fi

# default for logprimary = 8
    db2 update db cfg for $DBNAME using logprimary $LOGPRIMARY
    if test $? -ge 4; then errExit; fi

# default for logsecond = 16
    db2 update db cfg for $DBNAME using logsecond $LOGSECOND
    if test $? -ge 4; then errExit; fi

# TPC is designed with lock requests never timeout (LOCKTIMEOUT -1 )
# do not change this value
    db2 update db cfg for $DBNAME using locktimeout -1
    if test $? -ge 4; then errExit; fi

# default for auto_maint = off
    db2 update db cfg for $DBNAME using auto_maint on
    if test $? -ge 4; then errExit; fi

# default for auto_tbl_maint = off
    db2 update db cfg for $DBNAME using auto_tbl_maint on
    if test $? -ge 4; then errExit; fi

# default for auto_runstats = off
    db2 update db cfg for $DBNAME using auto_runstats on
    if test $? -ge 4; then errExit; fi

    db2updv8 -d $DBNAME -u $DB2ADMIN -p "$DB2ADMPASS"
    if test $? -ge 4; then errExit; fi

    db2 connect to $DBNAME user $DB2ADMIN using "$DB2ADMPASS"
    if test $? -ge 4; then errExit; fi

    cd $DBHOME/sqllib/bnd
    db2 bind @db2cli.lst blocking all grant public sqlerror continue CLIPKG 30
}

dropErrExit(){
    echo Error was encountered dropping database $DBNAME
    echo IBM Aperi database drop failed.
    touch /tmp/done
    exit 1
}

dropDB(){
    db2 disconnect $DBNAME > /dev/null 2>&1
    #catalog, drop and uncatalog the db first.
    db2 catalog database $DBNAME > /dev/null 2>&1
    echo Dropping database $DBNAME
    db2 drop database $DBNAME > /dev/null 2>&1
    if test $? -ge 4; then dropErrExit; fi
    db2 uncatalog database $DBNAME > /dev/null 2>&1
}

# -----------------------------------------------------------------------
# Initialize variables
# -----------------------------------------------------------------------

    SCRIPTNAME=$0

    if test $# -lt 18
    then
        usage
    else
        DB2ADMIN=$1
        DB2ADMPASS="$2"
        DBNAME=$3
	CDFLAG=$4
	LOGSIZE=$5
	LOGPATH=$6
	SHEAPTHRES=$7
	DBHEAP=$8
        APPLHEAPSZ=$9
	shift 9
        APP_CTL_HEAP_SZ=$1
        APPGROUP_MEM_SZ=$2
	SORTHEAP=$3
	STMTHEAP=$4
	MAXLOCKS=$5
	LOCKLIST=$6
	MAXAPPLS=$7
	LOGPRIMARY=$8
	LOGSECOND=$9
    fi

    DBEXISTS="NO"
# -----------------------------------------------------------------------
# Create the database if it does not exist
# -----------------------------------------------------------------------
    # Work around for Redhat Linux
    eval DBHOME=~$DB2ADMIN; export DBHOME

    if test -f $DBHOME/sqllib/db2profile
     then
       . $DBHOME/sqllib/db2profile
     else
       echo Unable to read $DBHOME/sqllib/db2profile
       touch /tmp/done
       exit 1
    fi

    db2 connect to $DBNAME user $DB2ADMIN using "$DB2ADMPASS" > /dev/null 2>&1
    if test $? -ge 4
    then
        echo Database $DBNAME does not exist
	if [ "x$CDFLAG" == "xCreate" ]
	then
	    #catalog, drop and uncatalog the db first.
	    db2 catalog database $DBNAME > /dev/null 2>&1
            db2 drop database $DBNAME > /dev/null 2>&1
            db2 uncatalog database $DBNAME > /dev/null 2>&1
            echo Creating database $DBNAME
            db2 create db $DBNAME
            if test $? -ge 4; then errExit; fi
            configDB
	else
       	    touch /tmp/done
	    exit 0
	fi
    else
        if [ "x$CDFLAG" == "xCreate" ]
        then
	    DBEXISTS="YES"
	    echo Database $DBNAME already exists
            db2 disconnect $DBNAME > /dev/null 2>&1
	    configDB
	else
	    dropDB
	fi
    fi

# -----------------------------------------------------------------------
# Success exit point
# -----------------------------------------------------------------------
    if [ "x$CDFLAG" == "xCreate" ]
    then
        db2 commit work
        db2 disconnect $DBNAME
	echo IBM Aperi database creation and configuration completed successfully.
    else
	echo IBM Aperi database drop completed successfully.
    fi
    touch /tmp/done
    exit 0
