#!/usr/bin/env bash

function _cleanup {
  echo "Cleaning up server processes."
  pkill -f ":8888"
}

# Make sure all running processes are cleaned up
_cleanup

# If there is no vendor directory, run composer install.
[[ -d vendor ]] || composer install
# If there is no node modules directory, run yarn.
[[ -d node_modules ]] || yarn

source .envrc

# Initial setup, so drush bootstrap doesn't fail.
[[ -d web/sites/default/files/.sqlite ]] || (vendor/bin/silverback setup && vendor/bin/drush en cypress -y)

# Start drush serve to serve Drupal.
drush serve -q :8888 &

DRUSH_SERVE_WAIT=0
until nc -z 127.0.0.1 8888 || ((DRUSH_SERVE_WAIT > 19)); do sleep 1 && echo "Waited $DRUSH_SERVE_WAIT seconds for drush serve."; done
echo "Drupal server operational."

export SIMPLETEST_BASE_URL=http://127.0.0.1:8888

export CYPRESS_BASE_URL=http://127.0.0.1:8888

if [[ $1 ]]; then
  # Run Drupal unit tests for the current module test suite
  if [[ -d web/modules/custom/$1 ]]; then vendor/bin/phpunit web/modules/custom/$1 || exit 1; fi
  if [[ -d web/modules/contrib/$1 ]]; then vendor/bin/phpunit web/modules/contrib/$1 || exit 1; fi

  # Run unit tests in locally modified modules.
  if [[ -d packages/drupal ]]; then vendor/bin/phpunit packages/drupal || exit 1; fi
  vendor/bin/drush cypress:run "$1:**/*.feature" || exit 1;
else
  # Run Drupal unit tests for custom modules
  if [[ -d web/modules/custom ]]; then vendor/bin/phpunit web/modules/custom || exit 1; fi
  # Run unit tests in locally modified modules.
  if [[ -d packages/drupal ]]; then vendor/bin/phpunit packages/drupal || exit 1; fi
  vendor/bin/drush cypress:run || exit 1;
fi


echo "====================="
echo "All tests successful!"
echo "====================="
_cleanup
