#!/usr/bin/env python
# Use:
#    find * \( -name '*.rst' -o -name '*.txt' -o -name '*.py' -o -name '*.[ch]' \) | egrep -v 'Doc/build/|Doc/tools/|rfc-index.txt' | python rfc_obsoleted.py

import collections, re, sys

# A local copy of http://www.ietf.org/download/rfc-index.txt.
RFC_INDEX = 'rfc-index.txt'

with open(RFC_INDEX) as f:
    index = f.read()
rfcs = {}
for m in re.finditer(r'^(\d+) ((?:.|\n)+?)\n\n', index, re.MULTILINE):
    n = int(m.group(1))
    s = ' '.join(m.group(2).split())
    rfcs[n] = s

d = collections.defaultdict(set)
for fn in sys.stdin:
    fn = fn.rstrip()
    with open(fn) as f:
        for m in re.finditer(r'(?:RFC|rfc)\s*(\d+)', f.read()):
            n = int(m.group(1))
            d[n].add(fn)

for n in sorted(d):
    s = rfcs.get(n, '')
    if 'Obsoleted' not in s: continue
    m = re.match(r'^(.*?\.) .*(\(Obsoleted by.*?\))', s)
    print('%s: %s %s' % (n, m.group(1), m.group(2)))
    for fn in sorted(d[n]):
        print('    %s' % fn)
