#!/bin/bash

export GOPATH="$PWD/.gopath"
cd .gopath/src/github.com/openark/orchestrator

setup_mysql() {
  if [ ! -f "/dbdeployer/dbdeployer" ] ; then
    return
  fi

  if [ -d "/mysql-tarballs-downloaded" ] ; then
    # if we downloaded MySQL, use it for tests
    tar_file=(/mysql-tarballs-downloaded/*)
  else
    # use bundled version
    tar_file=(mysql-tarballs/*)
  fi

  mkdir -p ~/opt/mysql
  /dbdeployer/dbdeployer unpack $tar_file

  pushd ~/opt/mysql
  version=(*)
  popd
 
  echo tar_file: $tar_file
  echo version: $version

  /dbdeployer/dbdeployer deploy single $version --sandbox-home $PWD/sandboxes --sandbox-directory orc-sandbox --port=3306

  mkdir -p bin
  ln -s /go/src/github.com/openark/orchestrator/sandboxes/orc-sandbox/use bin/mysql
  chmod +x bin/mysql
  PATH=bin:$PATH
}

integration_tests() {
  echo "# Running integration tests"
  ./tests/integration/test.sh || exit 1
}

setup_mysql
integration_tests
