#!/bin/bash
# Source from a validator of this spore. Selects the afdata binary this tree
# builds (AFDATA_BIN wins when set), exports it, and loads the Bash kit from it.
#
# Falling back to an installed afdata would judge the tree being edited with an
# older release, so the check would pass or fail for the wrong reason: a
# missing build is an error, not a reason to look on PATH.
#
# The caller defines ROOTPATH (the spore root) first.

if [ -z "${AFDATA_BIN:-}" ]; then
  if [ -x "$ROOTPATH/target/debug/afdata" ]; then
    AFDATA_BIN="$ROOTPATH/target/debug/afdata"
  elif [ -x "$ROOTPATH/target/debug/afdata.exe" ]; then
    AFDATA_BIN="$ROOTPATH/target/debug/afdata.exe"
  else
    printf '%s\n' "$(basename "$0"): no AFDATA_BIN and no target/debug/afdata; run scripts/test.sh, or build with: cargo build --bin afdata" >&2
    exit 1
  fi
fi
export AFDATA_BIN

_AFDATA_BASH_SOURCE="$("$AFDATA_BIN" shell bash)"
# shellcheck source=/dev/stdin
source /dev/stdin <<<"$_AFDATA_BASH_SOURCE"
unset _AFDATA_BASH_SOURCE
