#!/usr/bin/env bash

setup_suite(){
  if [[ -z "${ABRA_DIR}" ]]; then
    echo 'set $ABRA_DIR before running the test suite' >&3
    exit 1
  fi

  if [[ -z "${TEST_SERVER}" ]]; then
    echo 'set $TEST_SERVER before running the test suite' >&3
    exit 1
  fi

  if [[ ! -f "$PWD/abra" ]]; then
    make build-abra
  fi

  if [[ ! -f "$PWD/kadabra" ]]; then
    make build-kadabra
  fi

  if [[ -d "$ABRA_DIR" ]]; then
    rm -rf "$ABRA_DIR"
  fi

  # NOTE(d1): hack to copy over a local copy of the catalogue from the typical
  # $HOME/.abra directory if it exists. This avoids a costly git clone over the
  # network for every test invocation
  if [[ ! -d "$ABRA_DIR/catalogue" ]]; then
    if [[ -d "$HOME/.abra/catalogue" ]]; then
      mkdir -p "$ABRA_DIR"
      cp -r "$HOME/.abra/catalogue" "$ABRA_DIR"
      git -C "$ABRA_DIR/catalogue" checkout .
    fi
  fi
}

teardown_suite(){
  if [[ -z "${ABRA_DIR}" ]]; then
    echo 'set $ABRA_DIR before running the test suite' >&3
    exit 1
  fi

  if [[ -d "$ABRA_DIR" ]]; then
    rm -rf "$ABRA_DIR"
  fi
}
