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,'')
en=[b for b in blocks if g(b,'Enabled')=='Yes' and g(b,'Action')=='Allow']
print(f"total inbound rule blocks parsed: {len(blocks)}")
print(f"enabled + allow: {len(en)}")
ctl=[b for b in en if '5470' in g(b,'LocalPort')]
print(f"POSITIVE CONTROL rows mentioning 5470: {len(ctl)}")
for b in ctl: print("   ",g(b,'Rule Name'),"| prof",g(b,'Profiles'),"| proto",g(b,'Protocol'),"| lport",g(b,'LocalPort'),"| remote",g(b,'RemoteIP'),"| prog",g(b,'Program'))
assert ctl, "POSITIVE CONTROL FAILED - parser void"

print("\n=== A. blanket-in-netsh (Program Any AND LocalPort Any) ===")
bl=[b for b in en if g(b,'Program') in ('Any','') and g(b,'LocalPort') in ('Any','')]
print("count:",len(bl))
for b in bl: print("   ",g(b,'Rule Name'),"| prof",g(b,'Profiles'),"| proto",g(b,'Protocol'),"| remote",g(b,'RemoteIP'))

print("\n=== B. occupied explicit LocalPort set (enabled inbound allow) ===")
occ=set()
for b in en:
    lp=g(b,'LocalPort')
    if lp and lp!='Any':
        for t in lp.split(','): occ.add(t.strip())
nums=sorted([int(t) for t in occ if t.isdigit()])
print("distinct explicit tokens:",len(occ))
print("numeric ports:",' '.join(map(str,nums)))
print("non-numeric tokens:",sorted([t for t in occ if not t.isdigit()]))

print("\n=== C. rules naming candidate interpreters ===")
for needle in ('python','pwsh','powershell','\py.exe','spt.exe'):
    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'),"| lport",g(b,'LocalPort'),"| prof",g(b,'Profiles'),"| remote",g(b,'RemoteIP'))
