#!/bin/bash
#
# Copyright (c) 2006 rPath, Inc.
#
# This program is distributed under the terms of the Common Public License,
# version 1.0. A copy of this license should have been distributed with this
# source file in a file called LICENSE. If it is not present, the license
# is always available at http://www.opensource.org/licenses/cpl.php.
#
# This program is distributed in the hope that it will be useful, but
# without any waranty; without even the implied warranty of merchantability
# or fitness for a particular purpose. See the Common Public License for
# full details.

TMPFILE=$(mktemp /tmp/mysql.XXXXXX)
TESTDIR=/tmp/MySQLtestDir

rm -rf $TESTDIR
mkdir -p $TESTDIR/data $TESTDIR/tmp
trap "rm -rf $TESTDIR $TMPFILE" 0 9 15

cat >>$TMPFILE <<EOF
[mysqld]\n")
datadir=$TESTDIR/data
tmpdir=$TESTDIR/tmp
socket=$TESTDIR/socket
log_error=$TESTDIR/log
pid_file=$TESTDIR/pid
port=$(($PPID+1024))
sql_mode=TRADITIONAL,NO_AUTO_VALUE_ON_ZERO
lower_case_table_names=1
default-table-type=InnoDB
EOF

/usr/bin/mysql_install_db --defaults-file=$TMPFILE > /dev/null
/usr/sbin/mysqld --defaults-file=$TMPFILE &
i=0
while true ; do
    echo "show databases;" | mysql -S $TESTDIR/socket -u root mysql >/dev/null 2>&1 && break || sleep 1
    i=$((i+1))
    if [ $i -gt 10 ] ; then
	echo "Could not start MySQL server"
	exit 0
    fi
done
trap "kill -TERM $(cat $TESTDIR/pid) ; sleep 3 ; rm -rf $TMPFILE $TESTDIR" 0 9 15

mysql -S $TESTDIR/socket -u root test <<EOF
create temporary table foo(id integer);
EOF
