import re,sys
blocks=[];cur=None
for raw in open(sys.argv[1],encoding='utf-8',errors='replace'):
    line=raw.rstrip('\r\n')
    m=re.match(r'^([A-Za-z][A-Za-z /]*?):\s{2,}(.*)$',line)
    if not m: continue
    k,v=m.group(1).strip(),m.group(2).strip()
    if k=='Rule Name': cur={'Rule Name':v};blocks.append(cur)
    elif cur is not None: cur[k]=v
g=lambda b,k: b.get(k,'')
print("field keys seen:",sorted({k for b in blocks for k in b}))
en=[b for b in blocks if g(b,'Enabled')=='Yes' and g(b,'Action')=='Allow']
print(f"blocks={len(blocks)} enabled+allow={len(en)}")

# CONTROLS
ctl_port=[b for b in en if '5470' in g(b,'LocalPort')]
ctl_prog=[b for b in en if 'spt.exe' in g(b,'Program').lower()]
print(f"CONTROL port-5470 rows: {len(ctl_port)}   CONTROL spt.exe rows: {len(ctl_prog)}")
assert ctl_port and ctl_prog, "CONTROL FAILED - parser void"
for b in ctl_prog: print("   spt:",g(b,'Rule Name'),"| proto",g(b,'Protocol'),"| lport",g(b,'LocalPort'),"| prof",g(b,'Profiles'),"| remote",g(b,'RemoteIP'),"| prog",g(b,'Program'))

print("\n=== A. TRUE blanket admits: Program empty/Any AND LocalPort Any, split by TCP-relevance ===")
bl=[b for b in en if g(b,'Program') in ('Any','') and g(b,'LocalPort') in ('Any','')]
print("program-Any + port-Any count:",len(bl))
tcp=[b for b in bl if g(b,'Protocol') in ('TCP','Any')]
print("  of which TCP-relevant (proto TCP or Any):",len(tcp))
priv=[b for b in tcp if 'Private' in g(b,'Profiles')]
print("  of which also Private profile:",len(priv))
for b in priv: print("   !",g(b,'Rule Name'),"| proto",g(b,'Protocol'),"| prof",g(b,'Profiles'),"| remote",g(b,'RemoteIP'),"| src",g(b,'Rule source'))

print("\n=== C. rules naming candidate interpreters ===")
for needle in ('python','pwsh','powershell','\py.exe','spt.exe','http'):
    hits=[b for b in en if needle.lower() in g(b,'Program').lower()]
    print(f"  '{needle}' -> {len(hits)}")
    for b in hits: print("      ",g(b,'Rule Name'),"==>",g(b,'Program'),"| proto",g(b,'Protocol'),"| lport",g(b,'LocalPort'),"| prof",g(b,'Profiles'),"| remote",g(b,'RemoteIP'))
