#!/usr/bin/env python3
import lzma
import os
import sys


def open_dimacs(path):
    if path.endswith(".xz"):
        return lzma.open(path, "rt", encoding="ascii", errors="ignore")
    return open(path, "rt", encoding="ascii", errors="ignore")


def kind(cl):
    signs = "".join("+" if x > 0 else "-" for x in cl)
    return f"{len(cl)}:{signs}"


def main():
    for path in sys.argv[1:]:
        print("FILE", os.path.basename(path))
        cur_kind = None
        start = None
        count = 0
        mn = 10**18
        mx = 0
        examples = []
        clause_no = 0
        def flush(end_clause):
            nonlocal cur_kind, start, count, mn, mx, examples
            if cur_kind is None:
                return
            print(f"{start:9d}-{end_clause:9d} count={count:9d} kind={cur_kind:8s} vars=[{mn},{mx}] ex={examples[:3]}")
        with open_dimacs(path) as fh:
            cl = []
            for line in fh:
                if line.startswith("c") or line.startswith("p"):
                    continue
                for t in line.split():
                    x = int(t)
                    if x == 0:
                        clause_no += 1
                        k = kind(cl)
                        if k != cur_kind:
                            flush(clause_no - 1)
                            cur_kind = k
                            start = clause_no
                            count = 0
                            mn = 10**18
                            mx = 0
                            examples = []
                        count += 1
                        for y in cl:
                            ay = abs(y)
                            mn = min(mn, ay)
                            mx = max(mx, ay)
                        if len(examples) < 3:
                            examples.append(cl[:])
                        cl = []
                    else:
                        cl.append(x)
        flush(clause_no)


if __name__ == "__main__":
    main()
