#!/bin/bash

if ! [ -f /.dockerenv ] && ! grep -q 'devices:/docker' /proc/1/cgroup ; then
  echo "Not running inside docker, exiting to avoid data damage." >&2
  exit 1
fi

set -e
set -u

if [ -z "${1:-}" ] ; then
  echo "Usage: $0 <testsystem>" >&2
  echo
  echo "Usage example: $0 192.168.88.162"
  exit 1
fi

SERVER="${1:-}"
SELECT="${2:-all}"
OUTPUT_DIRECTORY="${3:-/code/}"
FORMAT="${4:-pretty}"

if [ -z "${SERVER}" ] ; then
  echo "Usage: $0 <testsystem> [<testset>] [<output_directory>] [junit]" >&2
  echo
  echo "Usage example: $0 192.168.88.162"
  echo "Usage example: $0 192.168.88.162 fast /results/ junit"
  echo "Possible test set: all, stable, fast, t/api-rest/api-root.t"
  echo "Default test set: all"
  exit 1
fi

cd t

if [ "${SELECT}" = "stable" ] ; then
  echo "Test selection: ${SELECT}"
  SELECT=$(echo rateomat-{01-run,10-prepaid-costs}.t)
elif [ "${SELECT}" = "fast" ] ; then
  echo "Test selection: ${SELECT}"
  SELECT="rateomat-01-run.t"
elif [ "${SELECT}" = "all" ] ; then
  echo "Test selection: all"
  SELECT=$(echo rateomat-*.t)
else
  echo "Test selection: ${SELECT}"
fi


MYSQL_SERVER="${SERVER}"
MYSQL_PORT="3306"
MYSQL_USER="root"
echo "Testing MySQL connection to ${MYSQL_SERVER}:${MYSQL_PORT} ..."
if !  mysql -u "${MYSQL_USER}" -h "${SERVER}" -P "${MYSQL_PORT}" -e 'select now()' >/dev/null ; then
  echo "ERROR: Cannot connect to MySQL on ${MYSQL_SERVER}:${MYSQL_PORT} using user '${MYSQL_USER}', cannot continue!" >&2
  echo "Hint:"
  echo " [CE] remove 'bind-address = ...' from /etc/ngcp-config/templates/etc/mysql/inc/my.mysqld1.cnf.tt2 && ngcpcfg apply 'Allow TCP for MySQL'"
  echo " [CE/PRO] run: mysql -u root -e \"GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY PASSWORD ''; FLUSH PRIVILEGES;\""
  exit 1
fi

echo "NOTE: Please be sure on the test node '${SERVER}':"
echo "      1) rate-o-mat must be stopped (remember about monit)"
echo "      2) config.yml should have: allow_delay_commit=1 ; allow_fake_client_time=1"
sleep 2

echo "################################################################################"
echo "Finished main setup, now running tests ..."

RC=0
result="rate-o-mat-unit-test-results"

export CATALYST_SERVER="https://${SERVER}:1443"
export RATEOMAT_PROVISIONING_DB_HOST="${MYSQL_SERVER}"
export RATEOMAT_BILLING_DB_HOST="${MYSQL_SERVER}"
export RATEOMAT_ACCOUNTING_DB_HOST="${MYSQL_SERVER}"
export RATEOMAT_PL="/code/rate-o-mat.pl"

if [ "${FORMAT}" = "junit" ] ; then
  # shellcheck disable=SC2086
  prove --formatter TAP::Formatter::JUnit -l -IUtils/ $SELECT | \
    tee -a "${OUTPUT_DIRECTORY}/${result}.xml"
  RC=${PIPESTATUS[0]}
else
  # api-threads.t and api-balanceintervals.t are failing with the "-Pretty option" :(
  # shellcheck disable=SC2086
  prove -v --color -l -IUtils/ $SELECT | tee -a "${OUTPUT_DIRECTORY}/${result}.pretty"
  RC=${PIPESTATUS[0]}
fi

echo "Finished test execution, test execution returned with exit code ${RC}."
for file in "${OUTPUT_DIRECTORY}/${result}.pretty" "${OUTPUT_DIRECTORY}/${result}.xml" ; do
  if [ -f "$file" ] ; then
    echo "Test results available at ${file}"
  fi
done

echo "################################################################################"

exit "${RC}"