#!/bin/bash

# Create hooks directory if it doesn't exist
mkdir -p .git/hooks

# Path to pre-commit hook
HOOK_PATH=".git/hooks/pre-commit"

# Check if pre-commit hook already exists
if [ -f "$HOOK_PATH" ]; then
    echo "Pre-commit hook already exists. Adding 'make test' command..."
    
    # Check if the hook already contains make test
    if grep -q "make test" "$HOOK_PATH"; then
        echo "Hook already contains 'make test'. No changes needed."
        exit 0
    fi
    
    # Create a temporary file
    TMP_FILE=$(mktemp)
    
    # Extract shebang line
    head -n 1 "$HOOK_PATH" > "$TMP_FILE"
    
    # Add make test command after shebang
    echo "" >> "$TMP_FILE"
    echo "echo \"Running tests before commit...\"" >> "$TMP_FILE"
    echo "make test" >> "$TMP_FILE"
    echo "if [ \$? -ne 0 ]; then" >> "$TMP_FILE"
    echo "    echo \"Tests failed. Commit aborted.\"" >> "$TMP_FILE"
    echo "    exit 1" >> "$TMP_FILE"
    echo "fi" >> "$TMP_FILE"
    echo "" >> "$TMP_FILE"
    
    # Add the rest of the original hook
    tail -n +2 "$HOOK_PATH" >> "$TMP_FILE"
    
    # Replace original hook with modified version
    mv "$TMP_FILE" "$HOOK_PATH"
    
else
    # Create new pre-commit hook
    echo "Creating new pre-commit hook with 'make test'..."
    
    cat > "$HOOK_PATH" << 'EOF'
#!/bin/bash

echo "Running tests before commit..."
make test
if [ $? -ne 0 ]; then
    echo "Tests failed. Commit aborted."
    exit 1
fi

# Exit successfully
exit 0
EOF

fi

# Make the pre-commit hook executable
chmod +x "$HOOK_PATH"

echo "Test hook installed successfully!" 