#!/bin/bash

PREFIX=~/mysql-bug-22364/mysql-standard-5.0.33-linux-x86_64

if [ ! -e mysql-5.0.33.tar.gz ]
then
  wget http://dev.mysql.com/get/Downloads/MySQL-5.0/mysql-5.0.33.tar.gz/from/http://mysql.mirror.rafal.ca/
fi
echo Decompressing MySQL
tar zxf mysql-5.0.33.tar.gz
cd mysql-5.0.33

echo Compiling MySQL
export CFLAGS="-O2 -pipe"
export CXX="gcc"
#export CHOST="x86_64-pc-linux-gnu"
export CXXFLAGS="-O2 -pipe"
./configure --prefix=$PREFIX --enable-assembler --with-mysqld-ldflags=-all-static
make -j3
echo Installing MySQL to $PREFIX
make install

cd $PREFIX
./bin/mysql_install_db
./libexec/mysqld &

echo Generating bug_data.csv
let i=0; while ( [ $i -lt 523843 ] ); do echo $i,$i >> bug_data.csv;let i=$i+1;done

echo Reproducing bug
echo "drop table if exists table_a; drop table if exists table_b; create table table_a (id int(10) unsigned default null, a integer, b integer, INDEX idx (a,b)); create table table_b (id int(10) unsigned auto_increment, a integer, b integer, primary key table_a (id)); load data infile '${PREFIX}/bug_data.csv' into table table_a fields terminated by ',' (a,b); insert into table_b (a,b) select a,b from table_a; update table_a as t join table_b as t2 on (t.a=t2.a AND t.b=t2.b) set t.id=t2.id;" | ./bin/mysql -u root -h localhost -vv test > bug_output.txt

kill -9 `cat var/*.pid`

cat bug_output.txt
echo "----------"
echo "This output is available in ${PREFIX}/bug_output.txt"
