#!/bin/bash

set -e

cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." || exit 1

if [ $# -ne 1 ]; then
	echo "usage: $0 <major|minor|patch>" >&2
	exit 2
fi
bump="$1"

# make sure we are on the master branch
branch="$(git rev-parse --abbrev-ref HEAD)"
if [ "$branch" != master ]; then
	echo "error: not on master branch" >&2
	exit 3
fi

# bump, commit, tag, and push
cargo set-version --bump "$bump"
version="$(grep -m1 '^version' Cargo.toml | cut -d'"' -f2)"
tag="v$version"
git add Cargo.toml Cargo.lock
git commit -m "Release $tag"
git tag -a "$tag" -m "Release $tag"
git push --atomic origin "$branch" "$tag"
