"""Fetch original, unmodified OFL font files from Google Fonts' repository."""
from pathlib import Path
from urllib.request import urlopen
from urllib.parse import quote
import hashlib
import json

ROOT = Path(__file__).resolve().parents[1]
ITEMS = [
    ('anton', 'Anton-Regular.ttf'),
    ('anton', 'OFL.txt'),
    ('epilogue', 'Epilogue[wght].ttf'),
    ('epilogue', 'OFL.txt'),
    ('notosansjp', 'NotoSansJP[wght].ttf'),
    ('notosansjp', 'OFL.txt'),
]
manifest = []
for family, name in ITEMS:
    dest = ROOT / 'fonts' / family / name
    dest.parent.mkdir(parents=True, exist_ok=True)
    url = 'https://raw.githubusercontent.com/google/fonts/main/ofl/' + family + '/' + quote(name)
    data = urlopen(url, timeout=90).read()
    dest.write_bytes(data)
    manifest.append({'file': str(dest.relative_to(ROOT)), 'source': url, 'sha256': hashlib.sha256(data).hexdigest()})
    print(f'{family}/{name}: {len(data):,} bytes', flush=True)
(ROOT / 'fonts' / 'sources.json').write_text(json.dumps(manifest, ensure_ascii=False, indent=2) + '\n')
