# -*-python-*-

import os
import string

Import ('env', 'base_glob')
tely = base_glob ('*.tely')
png = base_glob ('*.png')

# We need lily and mf to build these.
env.Depends ('lilypond.texi', ['#/lily', '#/mf'])
env.Depends ('music-glossary.texi', ['#/lily', '#/mf'])

env.Depends ('lilypond.texi', 'lilypond-internals.texi')

eps = map (env.PNG2EPS, png)
env.Depends ('lilypond.texi', eps)

e = env.Copy (
	LILYPOND_BOOK_FLAGS = '''--process="lilypond-bin -I#/input/test -e '(ly:set-option (quote internal-type-checking) \#t)'"''',
	__verbose = ' --verbose',
	GENERATE_DOCUMENTATION = '$srcdir/ly/generate-documentation',
	)

e.Command ('lilypond-internals.texi', ['#/lily', '#/mf'],
	   'cd ${TARGET.dir} && $LILYPOND_BIN $GENERATE_DOCUMENTATION')

## FIXME: implicit steps from [TE]LY -> PDF
texi = map (env.TEXI, tely)
dvi = map (env.TEXIDVI, tely)
ps = map (env.DVIPS, tely) ###map (lambda x: x + '.dvi', tely))
dvipdf = map (env.DVIPDF, tely)
pdf = map (env.PSPDF, dvipdf)

# FIXME: install
info = map (env.INFO, tely)

def file_subst (file_name, find, subst):
	s = open (file_name).read ()
	t = string.replace (s, find, subst)
	if s != t:
		os.rename (file_name, file_name + '~')
		h = open (file_name, "w")
		h.write (t)
		h.close ()

e['usersrc'] = Dir ('.').srcnode ().abspath
e['userout'] = Dir ('.').abspath
def url_fixup (target, source, env):
	file_subst (str (target[0]),
		    '../lilypond-internals', 'lilypond-internals/')

# fixme: see builder.py
a = ['$MAKEINFO -I$usersrc -I${SOURCE.dir} --html \
     --css-include=$srcdir/Documentation/texinfo.css $__verbose \
     --output=${TARGET.dir} $SOURCE',
     '$MAKEINFO -I$usersrc -I${SOURCE.dir} --html \
     --no-split --no-headers \
     --css-include=$srcdir/Documentation/texinfo.css $__verbose \
     --output=$TARGET $SOURCE',
     url_fixup,
     'ln -f ${SOURCE.dir}/*.png ${SOURCE.dir}/*.ly ${TARGET.dir}/',]

e.Command ('lilypond/lilypond.html', 'lilypond.texi', a)
e.Command ('lilypond-internals/lilypond-internals.html',
	   'lilypond-internals.texi', a)

#Hmm -- why not just mv ./Documentation/{*,*/*} ./doc :-)
env.Alias ('doc', texi)
env.Alias ('doc', dvi)
env.Alias ('doc', ps)
env.Alias ('doc', pdf)

env.Alias ('doc', 'lilypond/lilypond.html')
env.Alias ('doc', 'lilypond-internals/lilypond-internals.html')

# install ('lilypond/*', env['sharedir_doc_package'] + '/html')
# install ('lilypond-user/*', env['sharedir_doc_package'] + '/html')
