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

cd "$(dirname "$0")"

if [[ -z "${PYPI_API_TOKEN:-}" ]]; then
  echo "PYPI_API_TOKEN is not set. Export a project-scoped or account-scoped token:" >&2
  echo "  export PYPI_API_TOKEN=pypi-..." >&2
  exit 1
fi

REPO="${PYPI_REPOSITORY:-pypi}"
case "$REPO" in
  pypi)     REPO_URL="https://upload.pypi.org/legacy/" ;;
  testpypi) REPO_URL="https://test.pypi.org/legacy/" ;;
  *) echo "PYPI_REPOSITORY must be 'pypi' or 'testpypi', got '$REPO'" >&2; exit 1 ;;
esac

rm -rf dist
uv build
uv tool run --from twine twine check dist/*
uv tool run --from twine twine upload \
  --repository-url "$REPO_URL" \
  --username __token__ \
  --password "$PYPI_API_TOKEN" \
  dist/*
