#!/bin/sh
#
# Copyright (c) 2006 MySQL AB
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

echo "######################################################################"
echo "#"
echo "# Test BUG#16218 - Crash on insert delayed"
echo "#"
echo "# Part 1 - to be run on 4.x. Creates a table to be used from 5.0 later."
echo "#"

#######################################################################
#
# Settings. CAUTION: Paths are not space safe.
#

# BASEDIR="install directory"
# DATADIR="databases directory e.g. $BASEDIR/var"
DATADIR="${DATADIR:-$BASEDIR/var}"
TREEROOT="${TREEROOT:-$HOME}"

MYSQLD="$BASEDIR/libexec/mysqld"
MYSQLC="$BASEDIR/bin/mysql"
MYSQLA="$BASEDIR/bin/mysqladmin"
MYSQLT="$BASEDIR/bin/mysqltest"

PORT_1="${MYSQL_TCP_PORT:+--port=$MYSQL_TCP_PORT}"
SOCK_1="${MYSQL_UNIX_PORT:+--socket=$MYSQL_UNIX_PORT}"
USER_1="-u root -D test"
CLNT_1="-A -v -f"
DATA_1="--basedir=$BASEDIR --datadir=$DATADIR"
SERV_1="--log-error --core"
#SERV_1="$SERV_1 --skip-concurrent-insert"
DBUG_1=
#DBUG_1="--debug=d,query,enter:i:O,$DATADIR/mysqld.trace"
#DBUG_1="--debug=d,thrlock:i:O,$DATADIR/mysqld.trace"
#DBUG_1="--debug=t:d,query,send_data:i:O,$DATADIR/mysqld.trace"
#DBUG_1="--debug=t:d:i:O,$DATADIR/mysqld.trace"
DDD=
#DDD=1

cd $DATADIR || exit $?
rm -f *.trace *.err *.pid core* test/#sql* bug16218*

#######################################################################
#
# Functions.

sleep_until_file_created ()
{
  file=$1
  loop=$2
  org_time=$2
  while (test $loop -gt 0)
  do
    if [ -r $file ]
    then
      return 0
    fi
    sleep 1
    loop=`expr $loop - 1`
  done
  echo "ERROR: $file was not created in $org_time seconds;  Aborting"
  exit 1;
}

sleep_until_file_deleted ()
{
  pid=$1;
  file=$2
  loop=$3
  while (test $loop -gt 0)
  do
    if [ ! -r $file ]
    then
      if test $pid != "0"
      then
        wait_for_pid $pid
      fi
      return
    fi
    sleep 1
    loop=`expr $loop - 1`
  done
}

echo "######################################################################"
echo "#"
echo "# Starting database server."
"$MYSQLD" $PORT_1 $SOCK_1 $DATA_1 $SERV_1 $DBUG_1 &
SERV_1_PID=$!
echo "Process_id $SERV_1_PID"
echo
sleep_until_file_created `uname -n`.pid 400


echo "######################################################################"
echo "#"
echo "# Creating table."
"$MYSQLC" $PORT_1 $SOCK_1 $USER_1 $CLNT_1 <<EOF

    DROP TABLE IF EXISTS t1;

    CREATE TABLE t1 (
        ipa    int(11)      NOT NULL default '0',
        gsku   int(11)      NOT NULL default '0',
        name   varchar(9)   NOT NULL default '',
        pass   varchar(8)   NOT NULL default '',
        serial varchar(22)  default NULL,
        sid    int(11)      NOT NULL default '0',
        msg    varchar(255) NOT NULL default '',
        time   int(11)      NOT NULL default '0'
        ) ENGINE=MyISAM;

    show variables like 'data%';

EOF
echo "#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
echo "# Note the above datadir. You will have to supply it to part 2."
echo "#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
echo
sleep 1


echo "######################################################################"
echo "#"
echo "# Stopping database server."
"$MYSQLA" $PORT_1 $SOCK_1 -u root shutdown
sleep_until_file_deleted 0 `uname -n`.pid 60


echo "# End of Test."
echo "#"
echo "######################################################################"