# -*-python-*-

import os
import re
import string

Import ('env', 'base_glob', 'install')
feta = reduce (lambda x, y: x + y,
	       map (lambda x: base_glob (x),
		    ('feta[0-9]*.mf',
		     'feta-alphabet*[0-9].mf',
		     'feta-braces-[a-z]*.mf',
		     'parmesan[0-9]*.mf',)))
feta = base_glob ('feta[0-9][0-9]*.mf')
feta_alphabet = base_glob ('feta-alphabet[0-9][0-9]*.mf')
feta_braces = base_glob ('feta-braces-[a-z].mf')
parmesan = base_glob ('parmesan[0-9][0-9]*.mf')

fonts = feta + feta_alphabet + feta_braces + parmesan

feta_sizes = map (lambda x: re.sub ('feta([0-9]+)', '\\1', x), feta)
otfs = map (lambda x: 'emmentaler-' + x, feta_sizes) + ['aybabtu']

t = map (env.TFM, fonts)
g = map (env.GTABLE, fonts)
p = map (env.PFA, fonts)
e = map (lambda x: x + '.enc', fonts)
s = map (lambda x: x + '.svg', fonts)
o = map (env.OTF, otfs)

# Emmentaler
a = '''cat ${SOURCE} \
$$(echo ${SOURCE} | grep -v brace | sed s/feta/parmesan/) \
$$(echo ${SOURCE} | grep -v brace | sed s/feta/feta-alphabet/) \
> ${TARGET}'''
otf_table = Builder (action = a, suffix = '.otf-table',
		     # barf
		     src_suffix = '.lisp')
env.Append (BUILDERS = {'OTF_TABLE': otf_table})
f = map (env.OTF_TABLE, feta)
g = map (env.OTF_TABLE, feta_braces)

map (lambda x: env.Depends ('feta' + x + '.otf-table',
			    ['parmesan' + x + '.lisp',
			    'feta-alphabet' + x + '.lisp']), feta_sizes)

map (lambda x: env.Depends ('emmentaler-' + x + '.otf',
			    'feta' + x + '.otf-table'),
     feta_sizes)

map (lambda x: env.Depends ('emmentaler-' + x + '.otf',
			    ['feta' + x + '.pfa',
			     'parmesan' + x + '.pfa',
			     'feta-alphabet' + x + '.pfa']), feta_sizes)

for i in feta_sizes:
	env.Command ('emmentaler-%(i)s.pe' % locals (),
		     '$srcdir/buildscripts/gen-emmentaler-scripts.py',
		     '$PYTHON $srcdir/buildscripts/gen-emmentaler-scripts.py --dir=${TARGET.dir}')

map (lambda x: env.Depends (x + '.pfa', x + '.enc'), feta)


# Aybabtu

feta_braces_pfa = map (lambda x: x + '.pfa', feta_braces)

env.AT_COPY ('aybabtu.pe.in')
env.Command ('aybabtu.fontname', '', 'echo -n aybabtu > $TARGET')
env.Command ('aybabtu.subfonts',
	     map (lambda x: x + '.mf', feta_braces),
	     'echo ${SOURCES.filebase} > $TARGET')

env.Command ('aybabtu.otf-table',
	     map (lambda x: x + '.otf-table', feta_braces),
	     'cd ${TARGET.dir} && cat ${SOURCES.file} > ${TARGET.file}')

env.Command ('aybabtu.otf-gtable',
	     map (lambda x: x + '.otf-gtable', feta_braces),
	     'echo "(design_size . 20)" > $TARGET')

env.Depends ('aybabtu.otf',
	     feta_braces_pfa
	     + ['aybabtu.subfonts',
		'aybabtu.fontname',
		'aybabtu.otf-table',
		'aybabtu.otf-gtable'])

## FIXME: building only a few fonts does not seem to work anymore.
## what is essential these days, aybabtu/emmentaler are needed always?
mf_essential = ['feta16', 'feta20', 'parmesan16', ]
pfa_essential = map (env.PFA, mf_essential) + ['emmentaler-20.otf']
env.Alias ('mf-essential', pfa_essential)
env.Alias ('mf-essential', 'fonts.cache-1')

env['fonts'] = string.join (fonts)
env['feta_sizes'] = string.join (feta_sizes)

env.Alias ('mf', pfa_essential + p + map (lambda x: x[0], o))
env.Alias ('mf', s)
env.Alias ('mf', 'fonts.cache-1')

install (t, env['sharedir_package_version'] + '/fonts/tfm')
install (p, env['sharedir_package_version'] + '/fonts/type1')
install (o, env['sharedir_package_version'] + '/fonts/otf')
install (e, env['sharedir_package_version'] + '/ps')
install (s, env['sharedir_package_version'] + '/fonts/svg')

