#! /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
MY_TMP_DIR=$CWD/var/tmp
TIMEFILE="$CWD/var/tmp/mysqltest-time"
DASHBLANK="----	----	-------"
MYSQLD_SRC_DIRS="strings mysys include extra regex isam merge myisam \
 myisammrg heap sql"
GCOV_MASTER_MSG=/tmp/mysqld-master-gcov.out #gcov output
GCOV_MASTER_ERR=/tmp/mysqld-master-gcov.err  
GCOV_SLAVE_MSG=/tmp/mysqld-slave-gcov.out #gcov output
GCOV_SLAVE_ERR=/tmp/mysqld-slave-gcov.err  
GCOV_SLAVE_SRC=/tmp/mysqld-slave-src/

[ -d $MY_TMP_DIR ]  || mkdir -p $MY_TMP_DIR

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

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

#++
# mysqld Environment Parameters
#--
MYRUN_DIR=var/run
MASTER_MYPORT=9306
MASTER_MYDDIR="$CWD/var/lib"
MASTER_MYSOCK="$CWD/var/tmp/mysql.sock"
MASTER_MYPID="$CWD/var/run/mysqld.pid"
MASTER_MYLOG="$CWD/var/log/mysqld.log"
MASTER_MYERR="$CWD/var/log/mysqld.err"

SLAVE_MYPORT=9307
SLAVE_MYDDIR="$CWD/var/slave-data"
SLAVE_MYSOCK="$CWD/var/tmp/mysql-slave.sock"
SLAVE_MYPID="$CWD/var/run/mysqld-slave.pid"
SLAVE_MYLOG="$CWD/var/log/mysqld-slave.log"
SLAVE_MYERR="$CWD/var/log/mysqld-slave.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=$MASTER_MYSOCK --database=$DB --user=$DBUSER --password=$DBPASSWD --silent"
INSTALL_DB="$MYBIN/mysql-test_install_db"
GDB_MASTER_INIT=/tmp/gdbinit.master
GDB_SLAVE_INIT=/tmp/gdbinit.slave

if [ "$1" = "-gcov" ];
then
  DO_GCOV=1
  shift 1
fi  

if [ "$1" = "-gdb" ];
then
  DO_GDB=1
  shift 1
fi  


#++
# Terminal Modifications
#--
MOVE_TO_COL="$ECHO -n [300C[20D"
SETCOLOR_SUCCESS="$ECHO -n [1;32m"
SETCOLOR_FAILURE="$ECHO -n [1;31m"
SETCOLOR_WARNING="$ECHO -n [1;33m"
SETCOLOR_NORMAL="$ECHO -n [0;39m"

#++
# 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
}

prompt_user ()
{
 echo $1
 read
}

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`     
	raw=`$PRINTF %.4d $raw`           
	whole=`$PRINTF %.2s $raw`         
	xwhole=`$EXPR $whole \* 100`      
	deci=`$EXPR $raw - $xwhole`       

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

mysql_install_db () {
    $RM -rf $MASTER_MYDDIR $SLAVE_MYDDIR $SLAVE_MYLOG $MASTER_MYLOG \
     $SLAVE_MYERR $MASTER_MYERR
    [ -d $MYRUN_DIR ] || mkdir -p $MYRUN_DIR 
    $INSTALL_DB
    if [ $? != 0 ]; then
	error "Could not install master test DBs"
	exit 1
    fi
    $INSTALL_DB -slave
    if [ $? != 0 ]; then
	error "Could not install slave test DBs"
	exit 1
    fi
    return 0
}

gcov_prepare () {
    $FIND $BASEDIR -name \*.gcov \
    -or -name \*.da | $XARGS $RM
    $RM -rf $GCOV_SLAVE_SRC
    for d in $MYSQLD_SRC_DIRS; do
	cd $BASEDIR/$d
        mkdir -p $GCOV_SLAVE_SRC/$d  
	for f in *.h *.cc *.c; do
	  cp $f $GCOV_SLAVE_SRC/$d
	done
	cd $CWD
   done    
}

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

    $ECHO "gcov master info in $GCOV_MASTER_MSG, errors in $GCOV_MASTER_ERR"
    $ECHO "gcov slave info in $GCOV_SLAVE_MSG, errors in $GCOV_SLAVE_ERR"
}

start_master()
{
    cd $BASEDIR # for gcov
    #start master
    master_args="--no-defaults --log-bin \
    	    --server-id=1 \
            --basedir=$CWD \
	    --port=$MASTER_MYPORT \
	    --core-file \
            --datadir=$MASTER_MYDDIR \
	    --pid-file=$MASTER_MYPID \
	    --socket=$MASTER_MYSOCK \
            --log=$MASTER_MYLOG \
	    --language=english $EXTRA_MASTER_OPT"
    if [ x$DO_GDB = x1 ]
    then
      echo "set args $master_args" > $GDB_MASTER_INIT
      xterm -title "Master" -e gdb -x $GDB_MASTER_INIT $MYSQLD &
      prompt_user "Hit enter to continue after you've started the master"
    else	    
      $MYSQLD $master_args  >> $MASTER_MYERR 2>&1 &
    fi  
  MASTER_RUNNING=1
}

start_slave()
{
    [ x$SKIP_SLAVE = x1 ] && return
    [ -d $GCOV_SLAVE_SRC ] && cd $GCOV_SLAVE_SRC
    slave_args="--no-defaults --server-id=2 \
	    --master-user=root \
	    --master-connect-retry=1 \
	    --master-host=127.0.0.1 \
	    --master-port=$MASTER_MYPORT \
	    --core-file \
            --basedir=$CWD \
            --datadir=$SLAVE_MYDDIR \
	    --pid-file=$SLAVE_MYPID \
	    --port=$SLAVE_MYPORT \
	    --socket=$SLAVE_MYSOCK \
            --log=$SLAVE_MYLOG \
            --language=english $EXTRA_SLAVE_OPT"
    if [ x$DO_GDB = x1 ]
    then
      echo "set args $slave_args" > $GDB_SLAVE_INIT
      xterm -title "Slave" -e gdb -x $GDB_SLAVE_INIT $MYSQLD &
      prompt_user "Hit enter to continue after you've started the slave"
    else	    
      $MYSQLD $slave_args  >> $SLAVE_MYERR 2>&1 &
    fi
    SLAVE_RUNNING=1
}

mysql_start () {
    start_master
    start_slave
    cd $CWD
    return 1
}

stop_slave ()
{
  if [ x$SLAVE_RUNNING = x1 ]
  then
    $MYSQLADMIN --socket=$SLAVE_MYSOCK -u root shutdown
    SLAVE_RUNNING=0
  fi  
}

stop_master ()
{
  if [ x$MASTER_RUNNING = x1 ]
  then
    $MYSQLADMIN --socket=$MASTER_MYSOCK -u root shutdown
    MASTER_RUNNING=0
  fi
}

mysql_stop ()
{
 stop_master
 stop_slave
 return 1
}

mysql_restart () {

    return 1
    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
}

run_testcase ()
{
 tf=$1
 tname=`$BASENAME $tf`
 tname=`$ECHO $tname | $CUT -d . -f 1`
 master_opt_file=$TESTDIR/$tname-master.opt
 slave_opt_file=$TESTDIR/$tname-slave.opt
 SKIP_SLAVE=`$EXPR \( match $tname rpl \) = 0`
 
 if [ -f $master_opt_file ] ;
 then
  EXTRA_MASTER_OPT=`cat $master_opt_file`
  stop_master
  start_master
 else
  if [ ! -z EXTRA_MASTER_OPT ] || [ x$MASTER_RUNNING != x1 ] ;
  then
    EXTRA_MASTER_OPT=""
    stop_master
    start_master
  fi  
 fi

 if [ -f $slave_opt_file ] ;
 then
  EXTRA_SLAVE_OPT=`cat $slave_opt_file`
  stop_slave
  start_slave
 else
  if [ ! -z EXTRA_SLAVE_OPT ] || [ x$SLAVE_RUNNING != x1 ] ;
  then
    EXTRA_SLAVE_OPT=""
    stop_slave
    start_slave
    
  fi  
 fi

 cd $CWD
  
 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"
	$SETCOLOR_NORMAL && $ECHO -n "$tname		$timestr"

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

	total_inc

    if [ $res != 0 ]; then
        $ECHO "failed output"
	$CAT $TIMEFILE
	$ECHO
	$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  
 
}


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

[ "$DO_GCOV" ] && gcov_prepare 


mysql_install_db

#do not automagically start deamons if we are in gdb or running only one test
#case
if [ -z $DO_GDB ] && [ -z $1 ]
then
 $SETCOLOR_NORMAL && $ECHO -n "Starting mysqld for Testing" 
 mysql_start
 res=$?
 res=1
 [ $res != 1 ] && echo_notok && error "Starting mysqld"
 [ $res = 1 ] && echo_ok
fi

#$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

if [ -z $1 ] ;
then
    for tf in $TESTDIR/*.$TESTSUFFIX
    do
     run_testcase $tf
    done
else
 tf=$TESTDIR/$1.$TESTSUFFIX
 run_testcase $tf
fi

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

if [ -z $DO_GDB ] ;
then
    $SETCOLOR_NORMAL && $ECHO -n "Shutdown mysqld"
    mysql_stop
    res=$?
    res=1
    [ $res != 1 ] && echo_notok && error "Shutdown mysqld"
    [ $res = 1 ] && echo_ok
fi

$SETCOLOR_NORMAL

$ECHO
report_stats
$ECHO

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

exit 0
