#!/bin/sh

# set this to the base directory of the mysql installation
mysqlbase=/Users/eday/mysql/mysql-6.0.4-alpha-osx10.4-i686

# set this to a directory where temporary databases can be created
mysqldata=/Users/eday/data

# set this to the path of the other script included in this test
ins_script=/Users/eday/data/repl_bug_ins.sh

# you shouldn't need to edit anything below here
my_master="bin/mysql --protocol=tcp --port=3307"
my_slave="bin/mysql --protocol=tcp --port=3308"

cd $mysqlbase

# setup master node
rm -rf $mysqldata/test.master
mkdir $mysqldata/test.master
scripts/mysql_install_db --user=mysql --datadir=$mysqldata/test.master \
    --log-error=$mysqldata/test.master/error
chown -R mysql:mysql $mysqldata/test.master

bin/mysqld --user=mysql --server-id=1 --port=3307 \
    --basedir=$mysqlbase \
    --log-bin=master --binlog-format=row \
    --datadir=$mysqldata/test.master \
    --log-error=$mysqldata/test.master/error \
    --pid-file=$mysqldata/test.master/pid &

master_pid=$!

# setup slave node
rm -rf $mysqldata/test.slave
mkdir $mysqldata/test.slave
scripts/mysql_install_db --user=mysql --datadir=$mysqldata/test.slave \
    --log-error=$mysqldata/test.slave/error
chown -R mysql:mysql $mysqldata/test.slave

bin/mysqld --user=mysql --server-id=2 --port=3308 \
    --basedir=$mysqlbase \
    --log-bin=master --log-slave-updates --binlog-format=row \
    --relay-log=$mysqldata/test.slave/relay \
    --datadir=$mysqldata/test.slave \
    --log-error=$mysqldata/test.slave/error \
    --pid-file=$mysqldata/test.slave/pid &

slave_pid=$!

echo
echo "Server PIDs: master=$master_pid slave=$slave_pid"
echo
echo "Sleeping 3 seconds so servers can open TCP ports"
sleep 3

echo
echo "Setting up replication"
echo "CHANGE MASTER TO " \
    "MASTER_HOST='localhost'," \
    "MASTER_PORT=3307," \
    "MASTER_USER='root'," \
    "MASTER_LOG_FILE=''," \
    "MASTER_LOG_POS=4;" | $my_slave
echo "START SLAVE;" | $my_slave

echo
echo "Running Insert Scripts"

$ins_script a 10000 | $my_master &
ins_a=$!
$ins_script b 10000 | $my_master &
ins_b=$!
wait $ins_a $ins_b

echo
echo "Waiting for slave to catch up with master"
while [ 1 ]
do
    m_pos=`echo "SHOW MASTER STATUS\G" | $my_master | grep Position | \
        sed 's/.*Position: *\(.*\) */\1/'`
    s_pos=`echo "SHOW SLAVE STATUS\G" | $my_slave | \
        grep Read_Master_Log_Pos | \
        sed 's/.*Read_Master_Log_Pos: *\(.*\) */\1/'`

    echo "$m_pos $s_pos"

    if [ "$m_pos" = "$s_pos" ]

    then
        break
    fi

    sleep 1;
done

echo
echo "Waiting for SQL thread to catch up with I/O thread"
s_pos_last=`echo "SHOW SLAVE STATUS\G" | $my_slave | \
    grep Relay_Log_Pos | sed 's/.*Relay_Log_Pos: *\(.*\) */\1/'`
while [ 1 ]
do
    sleep 1;

    s_pos=`echo "SHOW SLAVE STATUS\G" | $my_slave | \
        grep Relay_Log_Pos | sed 's/.*Relay_Log_Pos: *\(.*\) */\1/'`

    echo "$s_pos_last $s_pos"

    if [ "$s_pos_last" = "$s_pos" ]
    then
        break
    fi

    s_pos_last=$s_pos
done

echo
echo "#####################################################"
echo "THE MASTER COUNTS SHOULD BE EQUAL TO THE SLAVE COUNTS"
echo "     IF THEY ARE NOT, THE BUG HAS BEEN REPRODUCED"
echo "#####################################################"

echo
echo "Insert counts from master:"
echo "SELECT COUNT(*) AS master_test_a FROM test.a;" | $my_master
echo "SELECT COUNT(*) AS master_test_b FROM test.b;" | $my_master

echo
echo "Insert counts from slave:"
echo "SELECT COUNT(*) AS slave_test_a FROM test.a;" | $my_slave
echo "SELECT COUNT(*) AS slave_test_b FROM test.b;" | $my_slave

kill $master_pid $slave_pid
wait $master_pid $slave_pid

echo
echo "Examining the binlogs to see where things went wrong."
echo "It appears to be the slave SQL thread, since the relay log is"
echo "accurate but the slave binlog is not"

echo
echo "Event count in master binlog";
bin/mysqlbinlog $mysqldata/test.master/master.0* | grep BINLOG | wc

echo
echo "Event count in slave relay log";
bin/mysqlbinlog $mysqldata/test.slave/relay.0* | grep BINLOG | wc

echo
echo "Event count in slave binlog";
bin/mysqlbinlog $mysqldata/test.slave/master.0* | grep BINLOG | wc
