import subprocess
import json
import os

def load_baseline():
    """Load deployed honeyfile paths from baseline.json. Returns: list of paths"""
    baseline_path = os.path.join(
        os.path.dirname(os.path.dirname(__file__)), 'logs', 'baseline.json'
    )
    if not os.path.exists(baseline_path):
        return []
    with open(baseline_path) as f:
        return json.load(f).get('honeyfiles', [])

def apply_rules():
    """Apply auditd watch rules for every honeyfile. Returns: None"""
    paths = load_baseline()
    if not paths:
        print("[monitor] No honeyfiles in baseline — run hivebee install first")
        return
    for path in paths:
        subprocess.run(
            ['auditctl', '-w', path, '-p', 'rwxa', '-k', 'hivebee'],
            capture_output=True
        )
        print(f"[monitor] Watching: {path}")

def clear_rules():
    """Remove all auditd rules on shutdown. Returns: None"""
    subprocess.run(['auditctl', '-D'], capture_output=True)
    print("[monitor] All auditd rules cleared")

if __name__ == "__main__":
    apply_rules()
