#!/bin/sh
# Git hook: prepare-commit-msg
# Automatically bumps version in pyproject.toml and stages it before committing.

# Locate Python executable
PYTHON_BIN=""
if [ -n "$VIRTUAL_ENV" ] && [ -x "$VIRTUAL_ENV/bin/python" ]; then
    PYTHON_BIN="$VIRTUAL_ENV/bin/python"
elif [ -n "$VIRTUAL_ENV" ] && [ -x "$VIRTUAL_ENV/Scripts/python.exe" ]; then
    PYTHON_BIN="$VIRTUAL_ENV/Scripts/python.exe"
elif [ -x "venv/bin/python" ]; then
    PYTHON_BIN="venv/bin/python"
elif [ -x "venv/Scripts/python.exe" ]; then
    PYTHON_BIN="venv/Scripts/python.exe"
elif command -v python3 >/dev/null 2>&1; then
    PYTHON_BIN="python3"
elif command -v python >/dev/null 2>&1; then
    PYTHON_BIN="python"
fi

if [ -z "$PYTHON_BIN" ]; then
    echo "[version-bump] Warning: Python not found, skipping version bump."
    exit 0
fi

# Run version bump script with commit message file ($1) and commit source ($2)
"$PYTHON_BIN" scripts/bump_version.py "$1" "$2"
STATUS=$?

if [ $STATUS -eq 0 ]; then
    git add pyproject.toml
fi

exit 0
