#!/bin/bash -eu

set -o pipefail

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

cd "$(git rev-parse --show-toplevel)"

cargo set-version "${VERSION}"

cargo upgrade
cargo update

cargo check
cargo test --bins

RUST_LOG=warn cargo run -- list-systemd-options | head -n -1 > systemd_options.md

git add Cargo.{toml,lock} systemd_options.md

git commit -m "chore: version ${VERSION}"
git tag -f -m "Version ${VERSION}" "v${VERSION}"

git cliff | head -n -3 > CHANGELOG.md
git add CHANGELOG.md
git commit --amend --no-edit

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