#!/bin/bash
# Auto-bump patch version of AntigravityAlwaysRun~ package.json before each commit
# This runs as a pre-commit hook — no amend needed, no history rewriting
FILE="AntigravityAlwaysRun~/package.json"

if [ ! -f "$FILE" ]; then
  exit 0
fi

# Note: The release script (release-extension.py) uses --no-verify to skip this hook.

# Read current version
CURRENT=$(grep -o '"version": "[^"]*"' "$FILE" | head -1 | grep -o '[0-9]*\.[0-9]*\.[0-9]*')
if [ -z "$CURRENT" ]; then
  exit 0
fi

# Split and bump patch
IFS='.' read -r MAJOR MINOR PATCH <<< "$CURRENT"
PATCH=$((PATCH + 1))
NEW_VERSION="$MAJOR.$MINOR.$PATCH"

# Replace in file (compatible with both macOS BSD sed and GNU sed)
if sed --version >/dev/null 2>&1; then
  # GNU sed (Linux/Windows Git Bash)
  sed -i "s/\"version\": \"$CURRENT\"/\"version\": \"$NEW_VERSION\"/" "$FILE"
else
  # BSD sed (macOS)
  sed -i '' "s/\"version\": \"$CURRENT\"/\"version\": \"$NEW_VERSION\"/" "$FILE"
fi

# Stage the bumped file so it's included in this commit
git add "$FILE"

echo "[AlwaysRun] Version bumped: $CURRENT -> $NEW_VERSION"
