set -x

################################################################################
#
# The variables which need to be adjusted.
#
# Top directory of old MySQL version (4.1)
  TOP_DIR41='/home/matthias/Arbeit/mysql-4.1/src-X'
# Top directory of new MySQL version (5.0)
  TOP_DIR50='/home/matthias/Arbeit/mysql-5.0/src-X'
#
# Directory for the test databases ...
VAR_DIR="/dev/shm/var"
#
################################################################################
MY_PWD=`pwd`


####### Some plausibilty checks and basic preparations
#
# Terminate if we don't have the necessary directories
for dir_name in "$TOP_DIR41" "$TOP_DIR50"
do
  if [ ! -d "$dir_name" ]
  then
    echo -e "\n $dir_name does not exist or is no directory -- abort\n"
    exit
  fi
done
#
#
# Create the directory for the test databases
VAR_DIR=${VAR_DIR:-"`pwd`"}
if [ ! -d "$VAR_DIR" ]
then
  mkdir "$VAR_DIR"
  if [ ! -d "$VAR_DIR" ]
  then
    echo -e "\n creation of directory $VAR_DIR failed -- abort\n"
    exit
  fi
fi
#

# Copy the files with test scripts and expected results to the
# standard places
for dir_name in "$TOP_DIR41" "$TOP_DIR50"
do
  rm "$dir_name"/mysql-test/t/mlfloat*.test
  cp mlfloat*.test   "$dir_name"/mysql-test/t
  rm "$dir_name"/mysql-test/r/mlfloat*.*
  cp mlfloat.result  "$dir_name"/mysql-test/r
  echo "Dummy"    >  "$dir_name"/mysql-test/r/mlfloat1.result 
done

TZ=GMT-3

export TOP_DIR41 TOP_DIR50 VAR_DIR TZ

SUITE_TIMEOUT=10
TESTCASE_TIMEOUT=2


# ----------------------------------------------------------------------
# To be executed as first test (MySQL 4.1)
#

# Protocol file for the the STDOUT, STDERR output
PROT="$MY_PWD"/x.prt
rm -f "$PROT"

#### (Re)start the server and remove already existing databases, ....
cd "$TOP_DIR41"/mysql-test
./mysql-test-run.pl --socket="$VAR_DIR"/tmp/master.sock --vardir="$VAR_DIR" \
        --skip-ndb --start-and-exit \
        --testcase-timeout=$TESTCASE_TIMEOUT --suite-timeout=$SUITE_TIMEOUT \
        2>&1 >> "$PROT"

#### Execute the tests
cd "$TOP_DIR41"/mysql-test
./mysql-test-run.pl --socket="$VAR_DIR"/tmp/master.sock --vardir="$VAR_DIR" \
        --extern --user=root --force \
        --testcase-timeout=$TESTCASE_TIMEOUT --suite-timeout=$SUITE_TIMEOUT \
        mlfloat 2>&1 >> "$PROT"

cd "$TOP_DIR41"
./client/mysqldump --socket="$VAR_DIR"/tmp/master.sock --user=root \
        --all-databases --add-drop-table > "$MY_PWD"/dump.tmp \
        2>> "$PROT"

#### Stop the server
cd "$TOP_DIR41"
./client/mysqladmin --no-defaults --user=root --password= \
        --socket="$VAR_DIR"/tmp/master.sock --port=9306 \
        --connect_timeout=5 --shutdown_timeout=70 shutdown \
        2>&1 >> "$PROT"

cd "$TOP_DIR41"/mysql-test
# cat "$PROT"

# ----------------------------------------------------------------------
# To be executed after switch to the different software version (MySQL 5.0)
#

# Protocol file for the the STDOUT, STDERR output
PROT="$MY_PWD"/y.prt
rm -f "$PROT"

#### (Re)start the server and remove already existing databases, ....
cd "$TOP_DIR50"/mysql-test
./mysql-test-run.pl --socket="$VAR_DIR"/tmp/master.sock --vardir="$VAR_DIR" \
        --skip-ndb --start-and-exit \
        --testcase-timeout=$TESTCASE_TIMEOUT --suite-timeout=$SUITE_TIMEOUT \
        2>&1 >> "$PROT"

cd "$TOP_DIR50"
# Load the dumped data
./client/mysql --socket="$VAR_DIR"/tmp/master.sock --user=root \
        < "$MY_PWD"/dump.tmp \
        2>&1 >> "$PROT"

# 2.10.2. Upgrading from Version 4.1 to 5.0
# .....
# you should run the mysql_fix_privilege_tables  script as described in
#     Section 2.10.8, “Upgrading the Grant Tables”.
# 2.10.8. Upgrading the Grant Tables
# ....
#         shell> mysql_fix_privilege_tables
# You must run this script while the server is running. It attempts to connect
# to the server running on the local host as root. If your root account
# requires a password, indicate the password on the command line. For MySQL 4.1
# and up, specify the password like this:
#         shell> mysql_fix_privilege_tables --password=root_password
# ....
#  After running the script, stop the server and restart it.
cd "$TOP_DIR50"
./scripts/mysql_fix_privilege_tables --verbose --socket="$VAR_DIR"/tmp/master.sock \
        2>> "$PROT"


#### Stop the server
cd "$TOP_DIR50"
./client/mysqladmin --no-defaults --user=root --password= \
        --socket="$VAR_DIR"/tmp/master.sock --port=9306 \
        --connect_timeout=5 --shutdown_timeout=70 shutdown \
        2>&1 >> "$PROT"

cd "$TOP_DIR50"/mysql-test
./mysql-test-run.pl --socket="$VAR_DIR"/tmp/master.sock --vardir="$VAR_DIR" \
        --mysqld=--innodb_data_file_path=ibdata1:50M:autoextend --skip-ndb \
        --testcase-timeout=$TESTCASE_TIMEOUT --suite-timeout=$SUITE_TIMEOUT \
        --start-dirty --script-debug \
        2>&1 >> "$PROT"

#### Execute the tests
cd "$TOP_DIR50"/mysql-test
./mysql-test-run.pl --socket="$VAR_DIR"/tmp/master.sock --vardir="$VAR_DIR" \
        --extern --user=root --force \
        --testcase-timeout=$TESTCASE_TIMEOUT --suite-timeout=$SUITE_TIMEOUT       \
        mlfloat1 2>&1 >> "$PROT"

# Collect the protocols
rm "$MY_PWD"/*.reject
for dir_name in "$TOP_DIR41" "$TOP_DIR50"
do
  cp "$dir_name"/mysql-test/r/mlfloat*.reject "$MY_PWD"/
done

exit;
