#!/bin/sh

# for MSYS to disable path conversion in command line argument
export MSYS2_ARG_CONV_EXCL="*"

###############
# database defs
###############
DBXML=db/cegodb.xml
DBNAME=cegodb
PAGESIZE=16384
ADMPORT=2000
DBPORT=2200
LOGPORT=3000
PIDFILE=./db/pid
ADMINUSER=cgadm
ADMINPWD=cgadm

###############
# tableset defs 
###############
TS=TS1
TSROOT=./db
TSTICKET=./db/tsticket.xml
SYSSIZE=300
TMPSIZE=1000
LOGFILESIZE=1000000
LOGFILENUM=3
APPSIZE=3000
DBUSER=lemke
DBPWD=lemke

DEBUGLEVEL=DEBUG

### end of customizing ###

CACHEFLAG=$1

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

rm -rf db
mkdir db

$ECHO "Init database xml ..."
$CEGO --mode=init --dbxml=${DBXML} --dbname=$DBNAME --hostname=$DBHOST --pgsize=$PAGESIZE --dbport=$DBPORT --admport=$ADMPORT --logport=$LOGPORT --pidfile=$PIDFILE --csmode=NONE

if [ $? -eq 0 ]
then
    $ECHONL "... operation ok"
else
    $ECHONL "... operation failed"
    exit 1
fi

$ECHO "Creating database role ..."
$CEGO --mode=addrole --dbxml=${DBXML} --role=ALL
if [ $? -eq 0 ]
then
    $ECHONL "... operation ok"
else
    $ECHONL "... operation failed"
    exit 1
fi

$ECHO "Creating admin user ..."
$CEGO --mode=adduser --dbxml=${DBXML} --user=${ADMINUSER}/${ADMINPWD} --role=admin
if [ $? -eq 0 ]
then
    $ECHONL "... operation ok"
else
    $ECHONL "... operation failed"
    exit 1
fi

$ECHO "Creating database user ..."
$CEGO --mode=adduser --tableset=${TS} --dbxml=${DBXML} --user=${DBUSER}/${DBPWD} --role=ALL
if [ $? -eq 0 ]
then
    $ECHONL "... operation ok"
else
    $ECHONL "... operation failed"
    exit 1
fi

$ECHO "Defining tableset ..."
DEF="tsroot=${TSROOT},tsticket=${TSTICKET},syssize=${SYSSIZE},tmpsize=${TMPSIZE},logfilesize=${LOGFILESIZE},logfilenum=${LOGFILENUM},appsize=${APPSIZE}"

$CEGO --mode=define --tableset=${TS} -dbxml=${DBXML} --tsdef=$DEF
if [ $? -eq 0 ]
then
    $ECHONL "... operation ok"
else
    $ECHONL "... operation failed"
    exit 1
fi

$ECHO "Creating database role permission ..."
$CEGO --mode=addperm --dbxml=${DBXML} --role=ALL --permid=P1 --tableset=${TS} --perm=ALL --filter=ALL
if [ $? -eq 0 ]
then
    $ECHONL "... operation ok"
else
    $ECHONL "... operation failed"
    exit 1
fi


$ECHO "Creating tableset ..."
$CEGO --mode=create --dbxml=${DBXML} --tableset=${TS}
if [ $? -eq 0 ]
then
    $ECHONL "... operation ok"
else
    $ECHONL "... operation failed"
    exit 1
fi

if [ "$CACHEFLAG" = "Y" ]
then
    $ECHO "Enabling querycache ..."
    $CEGO --mode=querycache --dbxml=${DBXML} --tableset=${TS} --enable --maxsize=1000000 --maxentry=100 --hashrange=10 --threshold=0
    if [ $? -eq 0 ]
    then
        $ECHONL "... operation ok"
    else
        $ECHONL "... operation failed"
        exit 1
    fi
fi

