<reponame>bqqbarbhg/spear
import sys
import zstd
from collections import namedtuple
import json

licenses = []

License = namedtuple("License", "name type thanks text")

def parse_licenses(text):
    global licenses

    parts = [p.strip() for p in text.split("---")]
    for part in parts:
        header, text = (p.strip() for p in part.split("\n\n", 1))
        name, ltype, thanks = (p.strip() for p in header.split("\n", 3))
        thanks = [p.strip() for p in thanks.split("\n")]
        licenses.append(License(name, ltype, thanks, text))

for arg in sys.argv[1:]:
	with open(arg) as f:
		parse_licenses(f.read())

encoded = json.dumps([l._asdict() for l in licenses]).encode("utf-8")
print("// Generated by misc/gen-licenses.py {}".format(" ".join(sys.argv[1:])))
print("static const uint32_t uncompressedSize = {};".format(len(encoded)))
compressed = zstd.ZSTD_compress(encoded, 20)

COLS = 32
print("static const char compressedLicenses[] =")
for base in range(0, len(compressed), COLS):
    print("\t\"{}\"".format("".join("\\x{:02x}".format(b) for b in compressed[base:base+COLS])))
print(";")
import sys
import zstd
from collections import namedtuple
import json

licenses = []

License = namedtuple("License", "name type thanks text")

def parse_licenses(text):
    global licenses

    parts = [p.strip() for p in text.split("---")]
    for part in parts:
        header, text = (p.strip() for p in part.split("\n\n", 1))
        name, ltype, thanks = (p.strip() for p in header.split("\n", 3))
        thanks = [p.strip() for p in thanks.split("\n")]
        licenses.append(License(name, ltype, thanks, text))

for arg in sys.argv[1:]:
	with open(arg) as f:
		parse_licenses(f.read())

encoded = json.dumps([l._asdict() for l in licenses]).encode("utf-8")
print("// Generated by misc/gen-licenses.py {}".format(" ".join(sys.argv[1:])))
print("static const uint32_t uncompressedSize = {};".format(len(encoded)))
compressed = zstd.ZSTD_compress(encoded, 20)

COLS = 32
print("static const char compressedLicenses[] =")
for base in range(0, len(compressed), COLS):
    print("\t\"{}\"".format("".join("\\x{:02x}".format(b) for b in compressed[base:base+COLS])))
print(";")