#!/bin/sh

TS=TS1
PIDFILE=/tmp/cego.pid
LOCKFILE=/tmp/cego.lock
INITBATCH=/tmp/b.sql

rm -rf $LOCKFILE
./mkdb $TS

echo "create table t1 ( a int, b string(100));" > $INITBATCH
echo "create table t2 ( a int, b string(100));" >> $INITBATCH
echo "create table t3 ( a int, b string(100));" >> $INITBATCH

../../src/cego --mode=batch --dbxml=./db/chkdb.xml --user=lemke/lemke --poolsize=1000 --batchfile=$INITBATCH --tableset=$TS

dodel=no

while true
do

    rm -rf $LOCKFILE
    
    echo "Starting daemon ..."

    nohup ../../src/cego --mode=daemon --forceload --nolockstat --pidfile=$PIDFILE --numdbthread=30 --dbxml=./db/chkdb.xml --poolsize=3000 --tableset=$TS --lockfile=$LOCKFILE &


    echo "Waiting 30 sec for recovery .." 
    sleep 30

    for t in t1 t2 t3
    do

	if [ $dodel = "no" ]
	then
	    echo "Inserting into $t ..."
	    ../../src/cgblow --mode=insert --server=localhost --port=2200 --attrformat=i:10000,s:3 --table=$t --tableset=TS1 --user=lemke/lemke --append --interval=1000 --count=20000 --dotransaction &
	else

	    echo "Deleting from $t ..."
	    ../../src/cgblow --mode=delete --server=localhost --port=2200 --condformat=a:i:1000 --table=$t --tableset=TS1 --user=lemke/lemke --append --interval=100 --count=3000 --dotransaction &
	fi

    done

    if [ $dodel = "no" ]
    then
	dodel=yes
    else
	dodel=no
    fi

    sleep 15
    
    echo "Crashing daemon .."
    kill -9 `cat $PIDFILE`
    sleep 2

done    