#!/bin/bash


TSLIST=
if [ $# -ne 0 ]
then
   TSLIST=`echo $1 | sed 's/,/ /g'`
fi

###############
# database defs
###############
DBXML=./db/chkdb.xml
DBNAME=chkdb
# PAGESIZE=256
# PAGESIZE=1024
# PAGESIZE=131072
# PAGESIZE=65536
# PAGESIZE=32768
PAGESIZE=16384
# PAGESIZE=8192
# PAGESIZE=4096
# PAGESIZE=2048
# PAGESIZE=1024
# PAGESIZE=512
DBHOST=localhost
ADMPORT=2000
DBPORT=2200
LOGPORT=3000
PIDFILE=./db/pid
LOCKFILE=./cego.lock
ADMINUSER=cgadm
ADMINPWD=cgadm
DBUSER=lemke
DBPWD=lemke

DEBUGLEVEL=DEBUG
# DEBUGLEVEL=NOTICE

### end of customizing ###

rm -rf db
mkdir db
rm -f $DBXML

CEGOPROG=../../src/cego
ECHO="/bin/echo -n"
ECHONL="/bin/echo"

function evalRet()
{
    if [ $1 -eq 0 ]
    then
        $ECHONL "... operation ok"
    else
        $ECHONL "... operation failed"
        exit 1
    fi
}

$ECHO "Init database xml ..."
$CEGOPROG --mode=init --dbxml=${DBXML} --dbname=$DBNAME --hostname=$DBHOST --pgsize=$PAGESIZE --dbport=$DBPORT --admport=$ADMPORT --logport=$LOGPORT --pidfile=$PIDFILE --csmode=ID --qescmode=OFF --loglevel=$DEBUGLEVEL --lockfile=$LOCKFILE
evalRet $?


$ECHO "Creating admin user ..."
$CEGOPROG --mode=adduser --dbxml=${DBXML} --user=${ADMINUSER}/${ADMINPWD} --role=admin --lockfile=$LOCKFILE
evalRet $?

$ECHO "Creating role ..."
$CEGOPROG --mode=addrole --dbxml=${DBXML} --role=ALL --lockfile=$LOCKFILE
evalRet $?


$ECHO "Creating database user ..."
$CEGOPROG --mode=adduser --dbxml=${DBXML} --user=${DBUSER}/${DBPWD} --role=ALL --lockfile=$LOCKFILE
evalRet $?


for TS in $TSLIST
do

    TSROOT=./db
    TSTICKET=$TSROOT/${TS}ticket.xml
    SYSSIZE=300
    TMPSIZE=5000
    LOGFILESIZE=1000000
    LOGFILENUM=3
    APPSIZE=50000
    SORTAREASIZE=10000000

    $ECHO "Defining tableset $TS..."
    $CEGOPROG --mode=define --tableset=${TS} -dbxml=${DBXML} --tsdef="tsroot=${TSROOT},tsticket=${TSTICKET},syssize=${SYSSIZE},tmpsize=${TMPSIZE},appsize=${APPSIZE},logfilesize=${LOGFILESIZE},logfilenum=${LOGFILENUM},sortareasize=${SORTAREASIZE}" --lockfile=$LOCKFILE
    evalRet $?

    $ECHO "Creating tableset $TS..."
    $CEGOPROG --mode=create --dbxml=${DBXML} --tableset=${TS} --lockfile=$LOCKFILE
    evalRet $?

    $ECHO "Adding tableset $TS to role ..."
    $CEGOPROG --mode=addperm --dbxml=${DBXML} --permid=${TS}_P --role=ALL --tableset=$TS --filter="ALL" --perm="ALL" --lockfile=$LOCKFILE 
    evalRet $?
    
done

exit 0

