#!/usr/bin/env python3
import json, re, sys
sys.stdout.reconfigure(encoding='utf-8')
s = open('jb_apps.json', encoding='utf-8', errors='replace').read()
# find the first '{' that starts the JSON object (skip stderr lines)
m = re.search(r'^\s*\{\s*$', s, re.M)
if m:
    s2 = s[m.start():]
else:
    i = s.find('"com.apple.')
    s2 = s[i-2:]
try:
    apps = json.loads(s2)
except Exception as e:
    # fall back: regex scan for bundle ids of interest
    bids = re.findall(r'"([A-Za-z0-9.\-]+\.(?:gsm|vn)[A-Za-z0-9.\-]*)"', s, re.I)
    print('fallback bids:', set(bids))
    sys.exit()
print('apps:', len(apps))
for bid, a in apps.items():
    bl = bid.lower()
    if any(k in bl for k in ('gsm', 'vn.', 'sileo', 'zebra', 'frida', 'substrate',
                             'loadcontroller', 'droplet', 'palera1n', 'checkra1n',
                             'dopamine', 'httpsms')):
        print(' ', bid, '|', a.get('CFBundleDisplayName'), '|', a.get('CFBundleShortVersionString'))
