#!/usr/bin/env bash
set -eux

# Assert that current branch is main
git rev-parse --abbrev-ref HEAD | grep -q main

# Assert that there are no uncommitted changes
git diff-index --quiet HEAD --

NEW_VERSION="$1"
if ! [[ "$NEW_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
    echo "You need to specify a version number in the format X.Y.Z"
    exit 1
fi

echo "Updating package.json."
yarn version --no-git-tag-version --new-version "$NEW_VERSION"
git commit -am "Release v$NEW_VERSION." --allow-empty

# Commit and tag new version
VERSION_TAG="v$NEW_VERSION"
git add .
git commit -m "Bump version to $VERSION_TAG" --allow-empty
git push origin main
git tag -af "$VERSION_TAG" -m "Version $NEW_VERSION"
git push -f origin "$VERSION_TAG"
