#!/usr/bin/env bash
set -euo pipefail

# Verify that the stub moraine sdist refuses to build from source with
# the RFC #219 error message. Used by ci-packaging.yml.
#
# The sdist is generated by scripts/build-python-sdist.py and contains
# an in-tree PEP 517 backend whose build hooks all raise. Any pip/uv
# attempt to build a wheel from the sdist must fail and print the
# refusal message pointing users at the real install paths.

usage() {
  cat <<'EOF'
usage: scripts/ci/assert-sdist-refuses.sh <path/to/moraine_cli-X.Y.Z.tar.gz>
EOF
}

if [[ $# -ne 1 ]]; then
  usage
  exit 64
fi

sdist="$1"
if [[ ! -f "$sdist" ]]; then
  echo "sdist not found: $sdist" >&2
  exit 1
fi

venv_dir="$(mktemp -d)"
log="$(mktemp)"
trap 'rm -rf "$venv_dir" "$log"' EXIT

python3 -m venv "$venv_dir"
"$venv_dir/bin/pip" install --quiet --upgrade pip >/dev/null

# Must fail. `--no-binary moraine-cli` forces pip to build from the
# sdist rather than picking up a cached wheel.
if "$venv_dir/bin/pip" install --no-binary moraine-cli "$sdist" >"$log" 2>&1; then
  echo "ERROR: sdist install should have failed but succeeded" >&2
  cat "$log" >&2
  exit 1
fi

if ! grep -q "moraine ships as prebuilt binary wheels only" "$log"; then
  echo "ERROR: sdist install failed, but without the expected refusal message" >&2
  cat "$log" >&2
  exit 1
fi

echo "sdist refusal OK: $sdist"
