#!/bin/sh


printUsage()
{
    echo "Usage : $0 <tableset> < local | remote | direct > <checkfile> [ quiet ]" 
}

DBHOST=cego
DBPORT=2200
DBUSER=xxlemke
DBPWD=xxlemke

QMODE="N"

if [ $# -lt 3 ]
then
    printUsage
    exit 1
fi

if [ $# = 4 ]
then
    if  [ $4 = "quiet" ]
    then
	QMODE="Y"
    fi
fi

TS=$1
CEGO=../../src/cego
CGCLT=../../src/cgclt

if [ $2 = "local" ]
then
    NAME=`cat $3 | grep "###" | sed 's/--//'`
    echo "Batch execute $3$NAME ... \c"
    $CEGO --mode=batch --dbxml=./db/chkdb.xml --user=lemke/lemke --poolsize=10000 --batchfile=$3 --tableset=$TS  > ./db/check.log 2> ./db/error.log
    retCode=$?
elif [ $2 = "remote" ]
then
    echo "Client execute $3 ...\c"
    $CGCLT -server=${DBHOST} --port={DBPORT} --user=${DBUSER}/${DBPWD} ---tableset=$TS --batchfile=$3 > ./db/check.log 2> ./db/error.log
    retCode=$?
elif [ $2 = "direct" ]
then
    $CEGO --mode=batch --dbxml=./db/chkdb.xml --user=lemke/lemke --poolsize=10000 --batchfile=$3 --tableset=$TS --cleanup    
    exit $?
else
    printUsage
    exit 1
fi

if [ $QMODE = "N" ]
then
    echo ""
    cat ./db/check.log
    cat ./db/error.log
else
    
    if [ $retCode -eq 0 ]
    then
	echo "operation ok"
    else
	echo "operation failed for following reason"
	echo "-------------------------------------"
	cat ./db/error.log
	echo "-------------------------------------"
	exit 1
    fi
fi
rm -rf ./db/check.log
rm -rf ./db/error.log
