#!/usr/bin/env bash

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

# Make sure all running processes are cleaned up
_finish

# If there is not .env file yet, copy .env.example.
[[ -f .env ]] || cp .env.example .env

# 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

export CYPRESS_BASE_URL=http://127.0.0.1:8888
export DRUPAL_CYPRESS=on

# Start drush server
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."


vendor/bin/drush cypress:open

# Make sure all running processes are cleaned up
_finish