#!/usr/bin/env bash
set -euo pipefail
cd "$(git rev-parse --show-toplevel)"

CARGO_TOML="Cargo.toml"

# Read current version
current=$(grep -E '^version\s*=' "$CARGO_TOML" | head -1 | sed 's/.*"\(.*\)".*/\1/')
echo "Current version: $current"

# Parse semver parts
IFS='.' read -r major minor patch <<< "$current"
next_patch=$((patch + 1))
next="${major}.${minor}.${next_patch}"

echo "Bumping to: $next"

# Update Cargo.toml
sed -i "0,/^version\s*=/{s/^version\s*=\s*\"${current}\"/version = \"${next}\"/}" "$CARGO_TOML"

# Regenerate Cargo.lock
cargo generate-lockfile --manifest-path "$CARGO_TOML"

# Commit
git add "$CARGO_TOML" Cargo.lock
git commit -m "bump version to ${next}"

# Push. The auto-tag GitHub Actions workflow (.github/workflows/auto-tag.yml)
# detects the Cargo.toml change on main, creates the matching v<version>
# tag, and invokes release.yml. Tagging used to happen here; do not re-add
# it — a local tag plus the CI-created tag would race and produce noisy
# duplicate-tag failures on push.
git push

echo "Pushed bump to ${next}. CI will create the v${next} tag and release."
