#!/bin/sh
# Auto-bumps the patch version in pyproject.toml on every commit, unless
# the commit already touches the version line itself (manual major/minor
# bumps aren't overridden). Installed via:
#   git config core.hooksPath scripts

set -e

PYPROJECT="pyproject.toml"

if git diff --cached -- "$PYPROJECT" | grep -q '^[+-]version = '; then
    exit 0
fi

current=$(grep -m1 '^version = ' "$PYPROJECT" | sed -E 's/version = "([0-9]+)\.([0-9]+)\.([0-9]+)"/\1.\2.\3/')
major=$(echo "$current" | cut -d. -f1)
minor=$(echo "$current" | cut -d. -f2)
patch=$(echo "$current" | cut -d. -f3)
next="$major.$minor.$((patch + 1))"

sed -E "s/^version = \"$current\"/version = \"$next\"/" "$PYPROJECT" > "$PYPROJECT.tmp"
mv "$PYPROJECT.tmp" "$PYPROJECT"
git add "$PYPROJECT"
