#! /bin/sh

#++
# Access Definitions
#--
DB=test
DBUSER=test
DBPASSWD=


#++
# Misc. Definitions
#--
CWD=`pwd`
cd ..
BASEDIR=`pwd`
cd $CWD
MYBIN="$CWD/mybin"
TESTVER=3.23
TESTDIR="$CWD/t/$TESTVER"
TESTSUFFIX=test
TOT_PASS=0
TOT_FAIL=0
TOT_TEST=0
USERT=0
SYST=0
REALT=0
TIMEFILE="$CWD/var/tmp/mysqltest-time"
DASHBLANK="----	----	-------"
MYSQLD_SRC_DIRS="strings mysys include extra regex isam merge myisam \
 myisammrg heap sql"
GCOV_MSG=/tmp/mysqld-gcov.out #gcov output
GCOV_ERR=/tmp/mysqld-gcov.err  

[ -z $COLUMNS ] && COLUMNS=80
E=`expr $COLUMNS - 8`
C=0

while [ $C != $E ]
do
  DASH72="${DASH72}-"
  C=`expr $C + 1`
done

#++
# mysqld Environment Parameters
#--
MYPORT=9306
MYDDIR="$CWD/var/lib"
MYSOCK="$CWD/var/tmp/mysql.sock"
MYPID="$CWD/var/run/mysqld.pid"
MYLOG="$CWD/var/log/mysqld.log"
MYERR="$CWD/var/log/mysqld.err"

#++
# Program Definitions
#--
BASENAME=`which basename`
CAT=/bin/cat
CUT=/usr/bin/cut
ECHO=/bin/echo
EXPR=`which expr`
FIND=/usr/bin/find
GCOV=`which gcov`
PRINTF=/usr/bin/printf
RM=/bin/rm
TIME=/usr/bin/time
TR=/usr/bin/tr
XARGS=`which xargs`
MYSQLD="$BASEDIR/sql/mysqld"
MYSQL_TEST="$BASEDIR/client/mysqltest"
MYSQLADMIN="$BASEDIR/client/mysqladmin"
MYSQL_TEST="$MYSQL_TEST --socket=$MYSOCK --database=$DB --user=$DBUSER --password=$DBPASSWD --silent"
INSTALL_DB="$MYBIN/mysql-test_install_db"

[ "$1" = "-gcov" ] && DO_GCOV=1

#++
# Terminal Modifications
#--
MOVE_TO_COL="$ECHO -n "
SETCOLOR_SUCCESS="$ECHO -n "
SETCOLOR_FAILURE="$ECHO -n "
SETCOLOR_WARNING="$ECHO -n "
SETCOLOR_NORMAL="$ECHO -n "

#++
# Function Definitions
#--
echo_ok() {
  $MOVE_TO_COL && $SETCOLOR_NORMAL
  $ECHO -n "[   "
  $SETCOLOR_SUCCESS
  $ECHO -n "ok"
  $SETCOLOR_NORMAL
  $ECHO "   ]"
  return 0
}

echo_notok() {
  $MOVE_TO_COL && $SETCOLOR_NORMAL
  $ECHO -n "[ "
  $SETCOLOR_FAILURE
  $ECHO -n "not ok"
  $SETCOLOR_NORMAL
  $ECHO " ]"
  return 0
}

echo_pass () {
  $MOVE_TO_COL && $SETCOLOR_NORMAL
  $ECHO -n "[  "
  $SETCOLOR_SUCCESS
  $ECHO -n "pass"
  $SETCOLOR_NORMAL
  $ECHO "  ]"
  return 0
}

echo_fail () {
  $MOVE_TO_COL && $SETCOLOR_NORMAL
  $ECHO -n "[  "
  $SETCOLOR_FAILURE
  $ECHO -n "fail"
  $SETCOLOR_NORMAL
  $ECHO "  ]"
  return 0
}

error () {

    $SETCOLOR_FAILURE
    $ECHO -n "Error: " && $SETCOLOR_NORMAL && $ECHO $1
    $SETCOLOR_NORMAL
    exit 1
}

pass_inc () {
    TOT_PASS=`$EXPR $TOT_PASS + 1`
}

fail_inc () {
    TOT_FAIL=`$EXPR $TOT_FAIL + 1`
}

total_inc () {
    TOT_TEST=`$EXPR $TOT_TEST + 1`
}

report_stats () {
    if [ $TOT_FAIL = 0 ]; then
	$ECHO "All tests successful."
    else
	$ECHO -n "Failed ${TOT_FAIL}/${TOT_TEST} tests, "

	xten=`$EXPR $TOT_PASS \* 10000`   #
	raw=`$EXPR $xten / $TOT_TEST`     # My God
	raw=`$PRINTF %.4d $raw`           # This is such a ...
	whole=`$PRINTF %.2s $raw`         # Narttu!!
	xwhole=`$EXPR $whole \* 100`      # Hynda!!!
	deci=`$EXPR $raw - $xwhole`       #

	$ECHO "${whole}.${deci}% successful."
    fi
}

mysql_install_db () {
    `$INSTALL_DB`
    if [ $? != 0 ]; then
	error "Could not install test DBs"
	exit 1
    fi
    return 0
}

gcov_clean () {
    $FIND $BASEDIR -name \*.gcov \
    -or -name \*.da | $XARGS $RM
}

gcov_collect () {
    $ECHO "Collecting source coverage info..."
    [ -f $GCOV_MSG ] && $RM $GCOV_MSG
    [ -f $GCOV_ERR ] && $RM $GCOV_ERR
    for d in $MYSQLD_SRC_DIRS; do
	cd $BASEDIR/$d
	for f in *.h *.cc *.c; do
	    $GCOV $f 2>>$GCOV_ERR  >>$GCOV_MSG
	done
	cd $CWD
    done

    $ECHO "gcov info in $GCOV_MSG, errors in $GCOV_ERR"
}

mysql_start () {

    cd $BASEDIR # for gcov
    $MYSQLD --no-defaults \
            --skip-networking \
            --basedir=$CWD \
            --datadir=$MYDDIR \
	    --pid-file=$MYPID \
	    --socket=$MYSOCK \
            --log=$MYLOG \
            --language=english >> $MYERR 2>&1 &
    cd $CWD
    return 1
}

mysql_stop () {

    `$MYSQLADMIN --socket=$MYSOCK -u root shutdown`

    return 1
}

mysql_restart () {

    mysql_stop
    res=$?
    [ $res != 1 ] && echo_notok && error "Stopping mysqld"

    mysql_start
    res=$?
    [ $res != 1 ] && echo_notok && error "Starting mysqld"

    return 1
}

mysql_loadstd () {

    sleep 2
    return 1
}

[ "$DO_GCOV" -a ! -x "$GCOV" ] && error "No gcov found"

[ "$DO_GCOV" ] && gcov_clean # clean up stale gcov files if running with gcov

mysql_install_db

$SETCOLOR_NORMAL && $ECHO -n "Starting mysqld for Testing"
mysql_start
res=$?
res=1
[ $res != 1 ] && echo_notok && error "Starting mysqld"
[ $res = 1 ] && echo_ok

$SETCOLOR_NORMAL && $ECHO -n "Loading Standard Test Database"
mysql_loadstd
res=$?
[ $res != 1 ] && echo_notok && error "Loading STD"
[ $res = 1 ] && echo_ok

$SETCOLOR_NORMAL && $ECHO -n "Starting Tests for MySQL $TESTVER Series"
$SETCOLOR_SUCCESS && echo_ok

$ECHO
$ECHO " TEST			USER	SYSTEM	ELAPSED		     RESULT"
$ECHO $DASH72

for tf in $TESTDIR/*.$TESTSUFFIX
do
 if [ -f $tf ] ; then
    mytime=`$TIME -p $MYSQL_TEST < $tf 2> $TIMEFILE`
    res=$?

    if [ $res != 1 ]; then
	mytime=`$CAT $TIMEFILE | $TR '\n' '-'`

	USERT=`$ECHO $mytime | $CUT -d - -f 2 | $CUT -d ' ' -f 2`
	SYST=`$ECHO $mytime | $CUT -d - -f 3 | $CUT -d ' ' -f 2`
	REALT=`$ECHO $mytime | $CUT -d - -f 1 | $CUT -d ' ' -f 2`
    else
	USERT="...."
	SYST="...."
	REALT="...."
    fi

	timestr="$USERT	$SYST	$REALT"
	tf=`$BASENAME $tf`
	$SETCOLOR_NORMAL && $ECHO -n "$tf		$timestr"

	[ $res != 1 ] && pass_inc && echo_pass
	[ $res = 1 ] && fail_inc && echo_fail

	total_inc

    if [ $res != 0 ]; then
	$ECHO
	$SETCOLOR_NORMAL && $ECHO -n "Restarting mysqld	$DASHBLANK"
	mysql_restart
	$SETCOLOR_SUCCESS && echo_ok
	$SETCOLOR_NORMAL && $ECHO -n "Resuming Tests		$DASHBLANK"
	$SETCOLOR_SUCCESS && echo_ok
	$ECHO
    fi
  fi  
done

$ECHO $DASH72
$ECHO
$SETCOLOR_NORMAL && $ECHO -n "Ending Tests for MySQL $TESTVER Series"
$SETCOLOR_SUCCESS && echo_ok
$RM $TIMEFILE

$SETCOLOR_NORMAL && $ECHO -n "Shutdown mysqld"
mysql_stop
res=$?
res=1
[ $res != 1 ] && echo_notok && error "Shutdown mysqld"
[ $res = 1 ] && echo_ok

$SETCOLOR_NORMAL

$ECHO
report_stats
$ECHO

[ "$DO_GCOV" ] && gcov_collect # collect coverage information

exit 0