#!/usr/bin/env bash

set -euo pipefail

if [ -z "${1:-}" ]; then
	echo "Please specify a version compatible with \`hatch version\`." >&2
	exit 1
elif ! git diff --quiet; then
	echo "Please commit or stash all changes before running this script." >&2
	exit 1
fi

cd "$(dirname "$(dirname "$0")")" || {
	echo "Couldn't change directories" >&2
	exit 1
}

if [ ! -x ./.venv/bin/hatchling ]; then
	read -r -p "Hatchling is not installed. Install it? [y/N]: " response
	response=${response,,}
	response=${response// /}

	if [ "$response" = "y" ]; then
		rye sync
	else
		echo "Exiting." >&2
		exit 1
	fi
fi

./.venv/bin/hatchling version "$1"
version=$(./.venv/bin/hatchling version)

git add --verbose --all
git commit --verbose --message="chore: bump version to $version"
git tag --annotate --message="$version" "$version"
git push --verbose --follow-tags
gh release create --verify-tag --generate-notes "$version"
