#!/bin/sh

HOOK_DIR=".git/hooks"
HOOK_FILE="$HOOK_DIR/pre-commit"

echo "Setting up pre-commit hook in $HOOK_FILE..."

# Create hooks directory if it doesn't exist (unlikely in a git repo, but good practice)
mkdir -p "$HOOK_DIR"

# Create the pre-commit script
cat > "$HOOK_FILE" << 'EOF'
#!/bin/sh
# Auto-increment patch version for every commit
# --no-git-tag-version prevents creating a git tag for every commit (too noisy)
# The version bump happens BEFORE the commit object is created.

echo "🤖 Bumping patch version..."
if npm version patch --no-git-tag-version; then
    # Add the updated package.json and package-lock.json to the index
    # so they are included in the CURRENT commit.
    git add package.json package-lock.json
    echo "✅ Version bumped and added to commit."
else
    echo "❌ Version bump failed. Aborting commit."
    exit 1
fi
EOF

# Make executable
chmod +x "$HOOK_FILE"

echo "✅ Git pre-commit hook installed successfully!"
