from pathlib import Path
import shutil,json,hashlib
from fontTools.ttLib import TTFont
from fontTools.varLib.instancer import instantiateVariableFont
ROOT=Path(__file__).resolve().parents[2];F=ROOT/'fonts';P=F/'production';P.mkdir(exist_ok=True)
jobs=[('Imperial Script','ImperialScript-Regular.ttf','ImperialScript-Regular.ttf',{}),('EB Garamond','EBGaramond[wght].ttf','EBGaramond-Regular.ttf',{'wght':400}),('EB Garamond','EBGaramond[wght].ttf','EBGaramond-Medium.ttf',{'wght':500}),('EB Garamond','EBGaramond-Italic[wght].ttf','EBGaramond-Italic.ttf',{'wght':400}),('Baskervville','Baskervville[wght].ttf','Baskervville-Regular.ttf',{'wght':400}),('Cinzel','Cinzel[wght].ttf','Cinzel-Medium.ttf',{'wght':500}),('Source Sans 3','SourceSans3[wght].ttf','SourceSans3-Regular.ttf',{'wght':400}),('Source Sans 3','SourceSans3[wght].ttf','SourceSans3-Semibold.ttf',{'wght':600}),('Cormorant Upright','CormorantUpright-SemiBold.ttf','CormorantUpright-Semibold.ttf',{}),('Lora','Lora[wght].ttf','Lora-Regular.ttf',{'wght':400})]
rows=[]
for family,src,dest,axes in jobs:
    t=TTFont(F/family/src)
    if axes:
        t=instantiateVariableFont(t,axes,inplace=False)
        style=('Italic' if 'Italic' in dest else 'Semibold' if axes.get('wght')==600 else 'Medium' if axes.get('wght')==500 else 'Regular')
        naming={1:family,2:style,3:family+' '+style+' local static instance 2026-10-07',4:family+' '+style,6:family.replace(' ','')+'-'+style,16:family,17:style}
        for record in list(t['name'].names):
            if record.nameID in naming:t['name'].setName(naming[record.nameID],record.nameID,record.platformID,record.platEncID,record.langID)
        t.save(P/dest)
    else:shutil.copy2(F/family/src,P/dest)
    shutil.copy2(F/family/'OFL.txt',P/(dest.removesuffix('.ttf')+'-OFL.txt'))
    rows.append({'family':family,'original_file':f'fonts/{family}/{src}','production_file':f'fonts/production/{dest}','instance_axes':axes,'weight':TTFont(P/dest)['OS/2'].usWeightClass,'licence':f'fonts/production/{dest.removesuffix(".ttf")}-OFL.txt','sha256':hashlib.sha256((P/dest).read_bytes()).hexdigest()})
(P/'provenance.json').write_text(json.dumps(rows,indent=2),encoding='utf-8')
print(json.dumps(rows,indent=2))
