#!/bin/bash -eux

set -o pipefail

readonly VERSION="${1:-$(date +%Y.%-m.%-d)}"

cd "$(jj root)"

if jj show --stat | tail -n -1 | grep -q '^0 files changed'
then
  jj describe -m "chore: version ${VERSION}"
else
  jj new -m "chore: version ${VERSION}"
fi

cargo set-version "${VERSION}"

cargo upgrade
cargo update

cargo check
cargo test

jj new

git tag -f -m "Version ${VERSION}" "v${VERSION}"
git cliff | head -n -3 > CHANGELOG.md
git tag -d "v${VERSION}"

jj squash -u

git tag -f -m "Version ${VERSION}" "v${VERSION}"
