#!/usr/bin/env bash

set -eux

absolute_path() {
  (cd "$1" && pwd)
}

scripts_path=$(absolute_path "$(dirname "$0")" )
release_dir="${scripts_path}/.."

export BOSH_DEPLOYMENT=bpm
export BOSH_NON_INTERACTIVE=true
export BOSH_ENVIRONMENT=vbox

bosh create-release \
  --dir "${release_dir}" --force

# Currently bosh will upload event if it has it, so check first
STEMCELL=$(bosh stemcells | grep warden | awk '{print $1}')
if [ -z "$STEMCELL" ]; then
  bosh upload-stemcell https://s3.amazonaws.com/bosh-core-stemcells/456.30/bosh-stemcell-456.30-warden-boshlite-ubuntu-xenial-go_agent.tgz --sha1 61791b4d37ee3aacb9db36bb819c1fbcc4785a9e
fi

bosh upload-release \
  --dir "${release_dir}"

bosh deploy \
  "${release_dir}/manifests/bosh-lite.yml"

# This is the IP of the first VM in the bosh in the concourse container.
# It will not change unless we deploy more instances/jobs etc or the upstream
# docker image changes its cloudconfig.
AGENT_HOST="$(bosh vms | grep running | grep bpm | awk '{print $4}')"
agent_port=1337

pushd "${release_dir}/src/bpm/acceptance" > /dev/null
  ginkgo -mod=vendor -r -p -race -randomizeAllSpecs -- --agent-uri="http://${AGENT_HOST}:${agent_port}"
popd > /dev/null

# We run the errand twice to make sure there is no state left on the VM which
# would prevent this.
bosh run-errand test-errand
bosh run-errand test-errand
