#!/bin/bash -eux

function main() {
  local scripts_dir
  scripts_dir=$(cd "$(dirname "${0}")" && pwd)

  local root_dir
  root_dir=$(cd "${scripts_dir}/.." && pwd)

  export PATH="${root_dir}/bin:${PATH}"

  install_ginkgo "${root_dir}"
  pushd "${root_dir}" > /dev/null
    ginkgo \
      -race \
      -succinct \
      precheck

    local test_args
    test_args=(
      "-r"
      "-randomizeAllSpecs"
      "-randomizeSuites"
      "-failFast"
      "-succinct"
      "-skipPackage" "precheck"
      "-slowSpecThreshold" "300"
    )

    if [ "${1-}" != "cf-tls-upgrade" ]; then
      test_args+=(
        "-race"
        "-skipPackage" "cf-tls-upgrade"
      )
    fi

    ginkgo \
      "${test_args[@]}" \
      "$@"
  popd > /dev/null
}

function install_ginkgo() {
  local dir
  dir="${1}"

  mkdir -p "${dir}/src/github.com/onsi"
  pushd "${dir}/src/github.com/onsi" > /dev/null
    ln -sf "${dir}/vendor/github.com/onsi/ginkgo"
    GOPATH="${dir}" go install github.com/onsi/ginkgo/ginkgo
  popd > /dev/null
  rm "${dir}/src/github.com/onsi/ginkgo"
}

main "$@"
