#
# Copyright (C) 2003 Stefan Seefeld
# All rights reserved.
# Licensed to the public under the terms of the GNU LGPL (>= 2),
# see the file COPYING for details.
#

SHELL	:= /bin/sh

srcdir	:= @srcdir@
topdir	:= @topdir@

synopsis:= @PYTHON@ synopsis.py
xsltproc:= xsltproc --novalid --path $(shell pwd) --xinclude
fop	:= xmlroff --backend=cairo

# This is a default setting suitable for the sxr-server demo applet
# use 'make sxr sxr=<URL>' to generate the sxr db for another URL
sxr	:= http://localhost:8000/sxr

python_src := $(wildcard $(topdir)/Synopsis/*.py)
python_src += $(shell find $(topdir)/Synopsis/Parsers -name '*.py' -print)
python_src += $(shell find $(topdir)/Synopsis/Processors -name '*.py' -print)
python_src += $(shell find $(topdir)/Synopsis/Formatters -name '*.py' -print)
#python_src += $(shell find $(topdir)/Synopsis/dist -name '*.py' -print)

python_syn := $(patsubst $(topdir)/%.py, %.syn, $(python_src))

texi-help := aux cp cps fn fns ky log pg toc tp tps vr vrs

MODULES	:= Cxx-API IR ASG Processor Processors Parsers 
MODULES += Formatters-HTML Formatters-DocBook Formatters-SXR
TEXI	:= $(patsubst %, %.texi, $(MODULES))
DB	:= $(patsubst %, %.xml, $(MODULES))

vpath %.py $(topdir)
vpath %.cc $(topdir)
vpath %.h $(topdir)
vpath %.hh $(topdir)
vpath Manual.texi $(srcdir)
vpath Manual.xml $(srcdir)

all: html pdf info

html: html/python html/cxx
sxr: html/sxr
pdf: Manual.pdf

html/%: %.syn
	@echo formatting $@
	$(synopsis) html --output=$@ $<
	touch $@

html/sxr: sxr.syn
	@echo formatting $@
	$(synopsis) sxr --url=$(sxr) --output=$@ $<
	touch $@

sxr.syn: python.syn cxx-sxr.syn
	@echo generating $@
	$(synopsis) archive --output=$@ $^

# Compile Python files
$(python_syn): %.syn: %.py
	@echo parsing $^
	mkdir -p $(dir $@)
	$(synopsis) python --output=$@ $^

# Link Python files together
python.syn: $(python_syn)
	@echo linking Python files together
	$(synopsis) link_python --output=$@ $^

IR.syn: python.syn
	@echo linking $@
	$(synopsis) link --scopes='Synopsis.IR,Synopsis.SXR,Synopsis.QualifiedName,Synopsis.SourceFile,Synopsis.DocString' --output=$@ $^

ASG.syn: python.syn
	@echo linking $@
	$(synopsis) strip --scope='Synopsis.ASG' --output=$@ $^

Processor.syn: python.syn
	@echo linking $@
	$(synopsis) link --scopes='Synopsis.Processor,Synopsis.process' --output=$@ $^

Processors.syn: python.syn
	@echo linking $@
	$(synopsis) strip --scope='Synopsis.Processors' --output=$@ $^

Parsers.syn: python.syn
	@echo linking $@
	$(synopsis) strip --scope='Synopsis.Parsers' --output=$@ $^

Formatters-HTML.syn: python.syn
	@echo linking $@
	$(synopsis) strip --scope='Synopsis.Formatters.HTML' --output=$@ $^

Formatters-DocBook.syn: python.syn
	@echo linking $@
	$(synopsis) strip --scope='Synopsis.Formatters.DocBook' --output=$@ $^

Formatters-SXR.syn: python.syn
	@echo linking $@
	$(synopsis) strip --scope='Synopsis.Formatters.SXR' --output=$@ $^

Cxx-API.syn: cxx.syn
	@echo linking $@
	$(synopsis) strip --scope='Synopsis' --output=$@ $^

%.texi: %.syn
	@echo formatting $@
	$(synopsis) texi --output=$@ $^

title:=

%.xml: %.syn
	@echo formatting $@
	$(synopsis) docbook --title=$(title) --output=$@ $^

IR.xml: override title:='The Internal Representation'
ASG.xml: override title:='The Abstract Semantic Graph'
Processor.xml: override title='The Processor Framework'
Processors.xml: override title='ASG Processors'
Parsers.xml: override title='Parsers'
Formatters-HTML.xml: override title='The HTML Formatter'
Formatters-DocBook.xml: override title='The DocBook Formatter'
Formatters-SXR.xml: override title='The SXR Formatter'
Cxx-API.xml: override title='The C++ API'

#Manual.pdf: Manual.texi $(TEXI)
#	@echo generating $@
#	texi2pdf -I $(srcdir) $<

html/db: Manual.xml $(DB)
	@echo generating $@
	cp $(srcdir)/synopsis.css $@
	$(xsltproc) -o $@/ $(srcdir)/html.xsl $<

Manual.pdf: Manual.fo $(DB)
	@echo generating $@
	$(fop) $<

Manual.fo: Manual.xml $(DB)
	@echo generating $@
	$(xsltproc) -o $@ $(srcdir)/fo.xsl $<

info:	Manual.texi $(TEXI)
	@echo generating info files
	makeinfo -I $(srcdir) $<

install: all
	mkdir -p $(datadir)/synopsis
	cp -pr html $(datadir)/synopsis
	mkdir -p $(infodir)
	install -m644 synopsis.info* $(infodir)
	install -m644 Manual.pdf $(datadir)/synopsis

clean:
	rm -rf synopsis sxr *.syn links.toc *.fo images $(DB) $(TEXI) \
     $(addprefix Manual., $(texi-help))

distclean:  clean
	rm -rf html Manual.pdf *.info* 

