#!/bin/bash

display_usage() {
  echo -e "Usage:\n\t$0 <version> \n\nExample:\n\t$0 45"
}

if [ -z "${HOST}" ]; then
  export HOST=127.0.0.1
fi

if [ -z "${WORDPRESS_VERSION}" ]; then
  if [ $# -eq 0 ]; then
    display_usage
    exit 1
  fi

  export WORDPRESS_VERSION="$1"
fi

export MYSQL_PWD=root
export WORDPRESS_DATABASE=wordpress_${WORDPRESS_VERSION}
export WORDPRESS_PORT=80${WORDPRESS_VERSION}
export WORDPRESS_URL=http://wordpress

function wait_for_service_port {
  while ! curl -s localhost:$1 > /dev/null; do
    sleep 0.25
  done
}

function prepare_test_config {
  mv src/config/class-tiny-config.php src/config/class-tiny-config.php.bak
  mv src/vendor/tinify/Tinify/Client.php src/vendor/tinify/Tinify/Client.php.bak
  cp test/fixtures/class-tiny-config.php src/config/class-tiny-config.php
  cp test/fixtures/Client.php src/vendor/tinify/Tinify/Client.php
}

function restore_config {
  mv src/config/class-tiny-config.php.bak src/config/class-tiny-config.php
  mv src/vendor/tinify/Tinify/Client.php.bak src/vendor/tinify/Tinify/Client.php
}

function start_services {
  if [ "${KEEP_ALIVE}" ]; then
    docker-compose up -d --build > /dev/null || { trap - EXIT; exit 1; }
  else
    docker-compose up -d --build --force-recreate > /dev/null || { trap - EXIT; exit 1; }
  fi
}

function stop_services {
  docker-compose down
}

function wait_for_services {
  echo "Waiting for PhantomJS..."
  wait_for_service_port 8910

  echo "Waiting for WordPress..."
  wait_for_service_port 80${WORDPRESS_VERSION}

  echo "Waiting for MySQL..."
  wait_for_service_port 3306
}

function setup {
  prepare_test_config
  start_services
  wait_for_services
}

function teardown {
  restore_config
  if ! [ "${KEEP_ALIVE}" ]; then
    stop_services
  fi
}

trap teardown EXIT
setup

vendor/bin/phpunit test/integration
