# -*-python-*-

import os
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-braces-[a-z].mf',
		     'feta-din*[0-9].mf',
		     'feta-nummer*[0-9].mf',
		     'parmesan[0-9]*.mf',)))

# .pfa rules want an encoding file; ecb10.enc
#sauter = ['ecb10']
sauter = []
fonts = feta + sauter
env['feta'] = string.join (feta)
env['sauter'] = ''

t = map (env.TFM, fonts)
a = map (env.AFM, feta)
p = map (env.PFA, fonts)

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

mf_essential = ['feta16', 'feta20', 'parmesan16', ]
pfa_essential = map (env.PFA, mf_essential)
env.Alias ('mf-essential', pfa_essential)

env.Command ('cmr.enc', 'cmr.enc.in', 'cp $SOURCE $TARGET')
env.Alias ('mf', 'cmr.enc')

env.Command ('lilypond.map', p,
	     ['for i in $feta; do echo $$i $$(fgrep FontName ${TARGET.dir}/$$i.afm | sed -e "s/FontName *//") "<"$$i.pfa; done > $TARGET',
	      'for i in $sauter; do echo "$$i $$i <$$i.pfa"; done >> $TARGET'])
env.Alias ('mf', 'lilypond.map')

env.Command ('fonts.scale', p,
	     'cd ${TARGET.dir} && echo *.pfa *.pfb | $PYTHON $srcdir/buildscripts/make-font-dir.py  > $TARGET.file')
env.Alias ('mf', 'fonts.scale')

env.Command ('Fontmap', p,
	     ["echo '%!' > $TARGET",
	      "echo '% Override default GS Fontmap' >> $TARGET",
	      "echo '% To let gs load fonts from builddir, do:' >> $TARGET",
	      "echo '% export GS_LIB=$$(pwd)/mf/out:' >> $TARGET",
	      "echo '% See Fontmap.GS for the syntax of real Fontmap files.' >> $TARGET",
	      "echo '(Fontmap.GS) .runlibfile' >> $TARGET",
	      "echo '(Fontmap.lily) .runlibfile' >> $TARGET"])

env.Command ('Fontmap.lily', p + ['Fontmap'],
	     ['echo "%!" > $TARGET',
	      '''for i in $feta; do echo "/$$(fgrep FontName ${TARGET.dir}/$$i.afm | sed -e 's/FontName *//') ($$i.pfa);"; done >> $TARGET''',
	      '''for i in $sauter; do echo "$$i ($$i.pfa);"; done >> $TARGET'''])
env.Alias ('mf', 'Fontmap.lily')

# build essential stuff first, that's friendlier
env.Alias ('mf', pfa_essential + p)

install (t, env['sharedir_package_version'] + '/fonts/tfm')
install (a, env['sharedir_package_version'] + '/afm')
install (p, env['sharedir_package_version'] + '/fonts/type1')
