#!/bin/bash
# Pre-commit hook for cppup
# This hook runs cargo fmt and cargo clippy before allowing a commit

set -e

echo "Running pre-commit checks..."

# Check if cargo is available
if ! command -v cargo &> /dev/null; then
    echo "Error: cargo not found. Please install Rust."
    exit 1
fi

# Run cargo fmt check
echo "1/2 Checking code formatting..."
if ! cargo fmt --all -- --check; then
    echo "Error: Code formatting check failed."
    echo "Run 'cargo fmt --all' to fix formatting issues."
    exit 1
fi

# Run cargo clippy
echo "2/2 Running clippy lints..."
if ! cargo clippy --all-targets --all-features -- -D warnings; then
    echo "Error: Clippy found issues."
    echo "Fix the issues above before committing."
    exit 1
fi

echo "All pre-commit checks passed!"
exit 0
