#!/usr/bin/env bash
# Build artefacts_route_recorder (sdist and wheel) and upload it to TestPyPI, or
# to PyPI with `bin/release production`. The version comes from the git tag on
# HEAD (setuptools-scm). Needs the dev extra:  pip install -e ".[dev]"

set -euo pipefail
cd "$(dirname "${BASH_SOURCE[0]}")/.."

production=${1:-no}
pkg=artefacts_route_recorder

if [[ -d venv ]]
then
  source venv/bin/activate
fi

# Get the new version
new_version=$(python -m setuptools_scm)

if [[ ! "$new_version" =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]+$ ]]
then
  >&2 echo "The release version must be a semver string specified by a Git tag on the current commit. Currently found ${new_version}. Most likely resolution is to commit/remove all pending changes in the repository, then issue the \`git tag\` command before running this script."
  exit 1
fi

python -m build

files=("dist/${pkg}-${new_version}-py3-none-any.whl" "dist/${pkg}-${new_version}.tar.gz")
if [[ "$production" = "production" ]]
then
  twine upload "${files[@]}"
else
  twine upload -r testpypi "${files[@]}"
fi
