# Make(1) rules for FleXML XML processor generator system.
# Copyright  1999 Kristoffer Rose.  All rights reserved.
#
# This file is part of the FleXML XML processor generator system.
# Copyright  1999 Kristoffer Rose.  All rights reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc., 59
# Temple Place, Suite 330, Boston, MA 02111-1307 USA.

# $Id: Makefile,v 1.24 1999/12/09 16:48:35 krisrose Exp krisrose $

# FILES.

STUFF =	GPL Makefile flexml.pl FleXML.html
BINS =	flexml
LIBS =	flexml-act
DATA =	skel
MANS =	flexml.1
DOCS =	README flexml-act.dtd EnlargeFlex.patch
HTMLS =	index.html flexml.html paper.html
SAMPS =	my.dtd my-show.act my-joke.xml my-joke2.xml my-joke3.xml \
	tricky.dtd tricky.act tricky.xml \
	test.html

SRC =	$(STUFF) $(DATA) $(DOCS) $(HTMLS) $(SAMPS)
ALL =	$(PROGS) $(LIBS) $(DATA) $(MANS) $(DOCS) $(HTMLS) $(SAMPS)

# SETUP.

# Installation paths
USR = /usr
BINDIR = $(USR)/bin
LIBDIR = $(USR)/lib
SHARE = /usr/share
MAN1DIR = $(SHARE)/man/man1
DOCDIR = $(SHARE)/doc
DATADIR = $(SHARE)/flexml
TMPDIR = /var/tmp

# Permanent program locations
PERL = /usr/bin/perl
FLEX = /usr/bin/flex
FLEXML = $(BINDIR)/flexml
ACT = $(LIBDIR)/flexml-act
SKEL = $(DATADIR)/skel

# Build compilation setup.
CC = gcc -Wall -ansi -pedantic
CFLAGS = -O2 -g
#FLEXDEBUG = -d

# Web location.

WEBHOME = info:public_html
FTPHOME = info:public_html/ftp/FleXML

# PRIMARY TARGETS.

.PHONY:		all install dist test clean

all:		$(ALL)

install:	$(ALL)
	mkdir -p $(DESTDIR)$(BINDIR) $(DESTDIR)$(LIBDIR) \
		$(DESTDIR)$(MAN1DIR) $(DESTDIR)$(DOCDIR) \
		$(DESTDIR)$(DATADIR) \
		$(DESTDIR)$(DOCDIR)/flexml/html \
		$(DESTDIR)$(DOCDIR)/flexml/examples
	install -m555 $(BINS) $(DESTDIR)$(BINDIR)/
	install -m555 $(LIBS) $(DESTDIR)$(LIBDIR)/
	install -m444 $(DATA) $(DESTDIR)$(DATADIR)/
	install -m444 $(MANS) $(DESTDIR)$(MAN1DIR)/
	install -m444 $(DOCS) $(DESTDIR)$(DOCDIR)/flexml/
	install -m444 $(HTMLS) $(DESTDIR)$(DOCDIR)/flexml/html/
	install -m444 $(SAMPS) $(DESTDIR)$(DOCDIR)/flexml/examples/

dist:		clean
	rsync -v FleXML.html $(WEBHOME)/FleXML.html
	rsync -va --cvs-exclude --delete-excluded ./ $(FTPHOME)/

clean::; @echo "Cleaning..."
	$(RM) *.[olh1] *-dummy.? lex.* *~ ./#*
test::		all
	@echo "Testing..."

# DEFAULT RULES.

# Generate C source from flex scanner.
%.c: 		%.l
	$(FLEX) -Bsv $(FLEXDEBUG) -o$@ $<

# Direct generation of stand-alone XML processor+application.
# Note: The dependency must be of the form "appl.l: appl.act proc.dtd".
%.l:		%.act
	./flexml.pl $(FLEXDEBUG) -vA -a $^

# Generate XML processor to link with application.
%.l %.h:	%.dtd
	./flexml.pl $(FLEXDEBUG) -v -s skel $<

# Generate XML application C source to compile and link with processor.
# Note: The dependency must be of the form "appl.c: appl.act proc.dtd".
%.c:		%.act
	./flexml.pl $(FLEXDEBUG) -vD -a $^


# MAIN PROGRAM.

clean::; $(RM) flexml flexml-act flexml-act.c

flexml:		flexml.pl
	sed -e "s;^[#][!].*perl;#!$(PERL);" \
	    -e "s;[.][/]flexml-act;$(ACT);g" \
	    -e "s;[.][/]skel;$(SKEL);g" \
	    -e "s;/var/tmp;$(TMPDIR);g" \
	    -e "s;/usr/share/doc/;$(DOCDIR)/;g" flexml.pl >flexml
	chmod +x flexml

# Action language...
flexml-act.l:		flexml-act.dtd
	./flexml.pl $(FLEXDEBUG) -Lv -ractions -s skel $<

flexml-act.c:		flexml-act.l
	$(FLEX) -Bsv -oflexml-act.c flexml-act.l

flexml-act.o:		flexml-act.c flexml-act.h
flexml-act-bootstrap.o:	flexml-act-bootstrap.c flexml-act.h
flexml-act:		flexml-act.o flexml-act-bootstrap.o

clean::; $(RM) TAGS
TAGS: 	$(SRC);	etags $(SRC)
ci:	$(SRC);	ci -u $(SRC)

# DOCUMENTS.

clean::; $(RM) flexml.html index.html pod2html-*

flexml.1:	flexml
	pod2man flexml >flexml.1

flexml.html:	flexml
	pod2html <flexml >flexml.html

index.html:	FleXML.html
	sed 's.ftp/FleXML/..g' FleXML.html >index.html

# TESTS.

# Example: LINK processor with application:

my.l my.h:	my.dtd
my.c:		my.l
my.o:		my.c my.h

my-show.c:	my-show.act my.dtd
my-show.o:	my-show.c my.h
my-show:	my-show.o my.o

test::		my-show
	./my-show <my-joke.xml && echo OK
	./my-show <my-joke2.xml && echo OK
	./my-show <my-joke3.xml || echo OK

clean::; $(RM) my.c my-show my-show.c my-dummy my-dummy.c

# Example: COMPILE processor into application

tricky.l:	tricky.act tricky.dtd
tricky.c:	tricky.l
tricky.o:	tricky.c
tricky:		tricky.o

test::		tricky
	./tricky <tricky.xml

clean::; $(RM) tricky.[lco] tricky

# Complex example: application to print XHTML <a href=...> values.
# XHTML is big, so...we must use a patched flex (included in Debian).


xhtml1-transitional.dtd:
	wget 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'

xhtml1-transitional.l xhtml1-transitional.h: xhtml1-transitional.dtd
	./flexml.pl $(FLEXDEBUG) -sskel -rhtml \
		-p "-//W3C//DTD XHTML 1.0 Transitional//EN" \
		-u "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" \
		xhtml1-transitional.dtd

xhtml1-transitional.c:	xhtml1-transitional.l
	$(FLEX) -Bsv -Ca -oxhtml1-transitional.c xhtml1-transitional.l

xhtml1-transitional.o:	xhtml1-transitional.c xhtml1-transitional.h

xhtml-href.c:	xhtml-href.act xhtml1-transitional.dtd
xhtml-href.o:	xhtml-href.c xhtml1-transitional.h
xhtml-href:	xhtml-href.o xhtml1-transitional.o

test::		xhtml-href
	./xhtml-href <test.html
	./xhtml-href <FleXML.html

clean::; $(RM) xhtml1-*.dtd xhtml1-*.[lhco] xhtml-href.[co] xhtml-href

# END.

clean::; @echo "Done cleaning."
test::;	@echo "Done testing."
