#!/bin/bash
# Pre-commit hook to run linter on staged files

# Get list of staged .lisp files
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep '\.lisp$')

# Always check critical files that affect all users
CRITICAL_FILES="runtime/ocicl-runtime.lisp setup.lisp"
for file in $CRITICAL_FILES; do
    if [ -f "$file" ]; then
        STAGED_FILES="$STAGED_FILES $file"
    fi
done

if [ -z "$STAGED_FILES" ]; then
    # No Lisp files staged, allow commit
    exit 0
fi

echo "Running linter on staged files..."

# Run linter on staged files (deduplicate with sort -u)
./ocicl lint $(echo $STAGED_FILES | tr ' ' '\n' | sort -u)

if [ $? -ne 0 ]; then
    echo ""
    echo "❌ Lint failed! Please fix the issues before committing."
    echo "   To bypass this check, use: git commit --no-verify"
    exit 1
fi

echo "✅ Lint passed!"
exit 0
