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

# Usage: ./make_release [next|major|minor|patch|<x.y.z>]
# Only the semver is printed on stdout; everything else goes to stderr.
MODE="${1:-next}"

case "$MODE" in
  next|major|minor|patch)
    VERSION="$(svu "$MODE")"
    ;;
  *)
    VERSION="${MODE#v}"
    ;;
esac
VERSION="${VERSION#v}"

sed -i "s/^version = .*/version = \"$VERSION\"/" pyproject.toml
git add pyproject.toml

git commit -sm "bump to $VERSION" 1>&2 || true
if [[ ! -v NO_TAG ]]; then
  git tag "$VERSION"
  git push origin "$VERSION" 1>&2
fi
git push origin HEAD 1>&2

printf '%s\n' "$VERSION"
