#!/bin/sh

export LANG=en_US.UTF-8
unset LANGUAGE LC_ALL LC_CTYPE

MYSQL="mysql --defaults-file=/etc/mysql/debian.cnf -v mysql"

(
    set -eux

    service mariadb start || service mysql start || exit 77

    # work around bug in Ubuntu jammy (2022-03-02)
    test -d /var/run/mysqld && chmod -v 755 /var/run/mysqld

    # Create the MySQL test database and user.
    echo "== creating the MySQL test database"
    cat debian/tests/mysql.testschema.sql | $MYSQL
    ./mysql_init.sh

    for v in $(pg_buildext installed-versions); do
        case $v in 9*|10) ;;
            *) NO_JIT="-o jit=off" ;;
        esac

        # tun jit off (https://github.com/EnterpriseDB/mysql_fdw/pull/227)
        PG_VIRTUALENV_UNSHARE="" MYSQL_USER_NAME=edb MYSQL_PWD=edb pg_buildext ${NO_JIT:-} installcheck-$v
    done
)
status=$?

(
    # Cleanup the MySQL test database (even in case of an error above)
    echo "== dropping the MySQL test database"
    echo "DROP USER 'edb'@'localhost'" | $MYSQL
    echo "DROP DATABASE mysql_fdw_regress" | $MYSQL
    echo "DROP DATABASE mysql_fdw_regress1" | $MYSQL

    service mariadb stop || service mysql stop
)

exit $status
