#!/bin/sh
# configure — auto-vendor tdc before compilation
#
# Resolution order:
#  1. Sibling tdc tree + tools/vendor_tdc.sh both present → re-vendor fresh.
#     (Development workflow.)
#  2. Pre-vendored src/tdc/ exists → use it as-is.
#     (CRAN tarball install — tools/ is .Rbuildignore'd, so vendor_tdc.sh
#     is absent here even when the TDC env var still points somewhere
#     valid in the surrounding CI environment.)
#  3. Otherwise → error out.
#
# POSIX sh only — bash is not guaranteed on all CRAN build hosts.

set -eu

PKG_ROOT=`cd "\`dirname "$0"\`" && pwd`
TDC=${TDC-"$PKG_ROOT/../tdc"}

if [ -f "$PKG_ROOT/tools/vendor_tdc.sh" ] \
   && [ -d "$TDC/include/tdc" ] && [ -d "$TDC/src" ]; then
  sh "$PKG_ROOT/tools/vendor_tdc.sh"
elif [ -d "$PKG_ROOT/src/tdc/include/tdc" ]; then
  echo "configure: using pre-vendored src/tdc/"
else
  echo "configure: ERROR - no tdc source found" >&2
  echo "  expected sibling repo at $TDC" >&2
  echo "  or pre-vendored copy at src/tdc/" >&2
  exit 1
fi
