#!/bin/sh
# Bootstrap fresh new Birdhouse instance to run testsuite at
# https://github.com/Ouranosinc/PAVICS-e2e-workflow-tests.
#
# This is a stable interface for test automation shielding it from knowing the
# intimate details of how to bootstrap a fresh new instance.
#
# Assume Birdhouse instance is already fully up (`birdhouse compose up -d` has
# been called).
#

THIS_FILE="$(readlink -f "$0" || realpath "$0")"
THIS_DIR="$(dirname "${THIS_FILE}")"
COMPOSE_DIR="${COMPOSE_DIR:-$(dirname "${THIS_DIR}")}"
SCRIPTS_DIR="${COMPOSE_DIR}/scripts"

set -x
# Populate test .nc file on Thredds.
# Need to open temporary Thredds "testdata/secure/" on Birdhouse production to anonymous group.
# Need write-access to DATASET_ROOT (/data/datasets/).
"${SCRIPTS_DIR}/bootstrap-testdata"

if [ -n "$(docker ps --filter name=solr)" ]; then
  # Index Thredds catalog.
  # Need to open temporary Thredds "testdata/secure/" on local Birdhouse host to anonymous group.
  # Only crawl the subset enough to pass canarie-api monitoring
  # see config/canarie-api/docker_configuration.py.template
  "${SCRIPTS_DIR}/trigger-birdhousecrawler" target_files=birdhouse/testdata/flyingpigeon/cmip5

  # For crawler to complete, assuming minimal dataset from bootstrap-testdata so
  # should be super fast to finish crawling.
  sleep 5
fi

# Create test user.
# Need "optional-components/secure-thredds" activated to pre-create group thredds-secure-authtest-group.
"${SCRIPTS_DIR}/create-magpie-authtest-user"

# Check if instance properly provisioned for testsuite.
"${SCRIPTS_DIR}/check-instance-ready"
