#!/usr/bin/env bash
# OpenRunner Patent Lab — post-commit hook
# Scans the latest commit for novelty signals and logs to OpenRunner.
# Install: openrunner install-hooks
# Or manually: cp this to .git/hooks/post-commit

set -e

# Check if openrunner is available
if ! command -v openrunner &>/dev/null; then
    exit 0
fi

# Check if patent-lab scanning is enabled
SETTINGS_FILE="$HOME/.openrunner/settings.json"
if [ -f "$SETTINGS_FILE" ]; then
    PATENT_SCAN=$(python3 -c "
import json, sys
try:
    s = json.load(open('$SETTINGS_FILE'))
    print(s.get('patent_scan_enabled', 'false'))
except: print('false')
" 2>/dev/null || echo "false")
    if [ "$PATENT_SCAN" != "true" ] && [ "$PATENT_SCAN" != "True" ]; then
        exit 0
    fi
fi

# Run scan in background (don't block commit)
COMMIT_HASH=$(git rev-parse HEAD 2>/dev/null)
if [ -n "$COMMIT_HASH" ]; then
    openrunner patent-scan --commit "$COMMIT_HASH" --min-score 30 &>/dev/null &
    disown 2>/dev/null || true
fi
