#!/bin/bash

# Renice shell script.
renice -n 1 $$

git rev-parse || exit 1

NOTEST=false
while getopts d:nu: opt; do
  case $opt in
    d)
      DBTYPE=$OPTARG
      ;;
    n)
      NOTEST=true
      ;;
    u)
      UPDATEFROMCOMMIT=$OPTARG
      ;;
  esac
done

if [ $OPTIND -le $# ] || [ "x$DBTYPE" != "xmysql" -a "x$DBTYPE" != "xpostgresql" -a "x$DBTYPE" != "xsqlite" ]; then
  echo "Usage: $0 -d mysql|postgresql|sqlite [-u UPDATEFROMCOMMIT]"
  exit 1
fi

set +e
TESTID=$(git rev-parse HEAD)-${UPDATEFROMCOMMIT:-HEAD}
DIR=~/public_html/testbed/w-$DBTYPE-$TESTID
rm -Rf "$DIR"
git clone ~/public_html/w "$DIR"
cd "$DIR"
git branch testcommit
[ -n "${UPDATEFROMCOMMIT}" ] && git checkout "${UPDATEFROMCOMMIT}"
case "$DBTYPE" in
  mysql)
    MYSQLDBNAME="$(echo "$TESTID" | tr .- __)"
    MYSQLUSERID_PREFIX="${TESTID:0:40}"
    MYSQLUSERID_SUFFIX="$(echo "${UPDATEFROMCOMMIT:-HEAD}" | tr . _)"
    MYSQLUSERID=${MYSQLUSERID_PREFIX:0:16 - ${#MYSQLUSERID_SUFFIX} - 1}_${MYSQLUSERID_SUFFIX}
    mysql -e "DROP DATABASE IF EXISTS $MYSQLDBNAME;"
    mysql -e "DROP USER $MYSQLUSERID;"
    mysql -e "CREATE USER $MYSQLUSERID@localhost IDENTIFIED BY 'abc';"
    mysql -e "CREATE DATABASE $MYSQLDBNAME;"
    mysql -e "GRANT ALL ON $MYSQLDBNAME.* TO $MYSQLUSERID;"
    php ./maintenance/install.php --dbtype=mysql \
                                  --dbuser "$MYSQLUSERID" \
                                  --dbpass abc \
                                  --dbname "$MYSQLDBNAME" \
                                  --pass testpass \
                                  --server http://localhost \
                                  --scriptpath /~tim/testbed/w-$DBTYPE-$TESTID \
                                  mysqltest WikiAdmin
    ;;
  postgresql)
    psql -c "DROP DATABASE IF EXISTS \"$TESTID\";" -p 5433 template1
    psql -c "DROP USER IF EXISTS \"$TESTID\";" -p 5433 template1
    psql -c "CREATE USER \"$TESTID\" WITH PASSWORD 'abc';" -p 5433 template1
    psql -c "CREATE DATABASE \"$TESTID\" WITH OWNER \"$TESTID\";" -p 5433 template1
    php ./maintenance/install.php --dbtype=postgres \
                                  --dbport 5433 \
                                  --dbuser "$TESTID" \
                                  --dbpass abc \
                                  --dbname "$TESTID" \
                                  --pass testpass \
                                  --server http://localhost \
                                  --scriptpath /~tim/testbed/w-$DBTYPE-$TESTID \
                                  postgresqltest WikiAdmin
    ;;
  sqlite)
    mkdir data
    php ./maintenance/install.php --dbtype=sqlite \
                                  --dbpath=./data \
                                  --pass testpass \
                                  --server http://localhost \
                                  --scriptpath /~tim/testbed/w-$DBTYPE-$TESTID \
                                  sqlitetest WikiAdmin
    ;;
esac

if [ -n "${UPDATEFROMCOMMIT}" ]; then
  git checkout testcommit
  php ./maintenance/update.php
fi

if [ "x$NOTEST" != "xtrue" ]; then
  php ./tests/parserTests.php
  php ./tests/phpunit/phpunit.php --group Parser --exclude-group Broken,ParserFuzz,Stub --
fi
