# Copyright 2001, 2002 (C) Metacosm Development Team
#                          Loic Dachary <loic@gnu.org>
#                          Jaime Villate <villate@gnu.org>
#
# Based on FSF Europe CVS code.
#
# XML/XSLT processor (validator)
# -------------------------
#
# sablotron (sabcmd)
# apt-get install sablotron
#
# libxslt + libxml2 (xsltproc)
# http://www.xmlsoft.org/
#
# XML validator
# -------------
# apt-get install rxp
# or
# ftp://ftp.cogsci.ed.ac.uk/pub/richard/rxp-1.2.3.tar.gz
#

XSLTPROC = sabcmd

#TODO METACOSM = http://metacosm.org
METACOSM = http://www.nongnu.org/metacosm

ECHO	  = echo 

XSLTOPTS = '$$metacosm=$(METACOSM)' 

ENPAGES = $(shell find * -regex '.*\.en\.xhtml' -print | sed "s/xhtml$$/html/")

FRPAGES = $(shell find * -regex '.*\.fr\.xhtml' -print | sed "s/xhtml$$/html/")

LANGFILES = $(shell find * -regex '.*\.lang' -print)

all: doc $(ENPAGES) $(FRPAGES)

$(ENPAGES): %.en.html: %.en.xhtml metacosm.xsl navigation.en.xsl %.lang
	@$(ECHO) "Building $@ ..."; \
	path=$< ; \
	base=`expr $$path : '\(.*\).en.xhtml'` ; \
	filebase=`basename $$base` ; \
	dir=`dirname $$path` ; \
	root=`dirname $$path | perl -pe 'chop; s:([^/]+):..:g if($$_ ne ".")'` ; \
	langlinks="`./tools/translate.sh $$base $(METACOSM) en`" ; \
	$(XSLTPROC) metacosm.xsl $$path $(XSLTOPTS) '$$fsfeurope='$$root '$$filebase='$$filebase.en '$$path='$$path '$$langlinks='"$$langlinks" > $$base.en.html-temp && (cat $$base.en.html-temp | perl -p -e '$$| = 1; (s/Date://, s/Author:/by/, s/\$$//g) if(/\$$''Date:/); s/mode: xml \*\*\*/mode: html \*\*\*/' > $$base.en.html) ; \
	rm -f $$base.en.html-temp

$(FRPAGES): %.fr.html: %.fr.xhtml metacosm.xsl navigation.fr.xsl %.lang
	@$(ECHO) "Building $@ ..."; \
	path=$< ; \
	base=`expr $$path : '\(.*\).fr.xhtml'` ; \
	filebase=`basename $$base` ; \
	dir=`dirname $$path` ; \
	root=`dirname $$path | perl -pe 'chop; s:([^/]+):..:g if($$_ ne ".")'` ; \
	langlinks="`./tools/translate.sh $$base $(METACOSM) fr`" ; \
	$(XSLTPROC) metacosm.xsl $$path $(XSLTOPTS) '$$fsfeurope='$$root '$$filebase='$$filebase.fr '$$path='$$path '$$langlinks='"$$langlinks" > $$base.fr.html-temp && (cat $$base.fr.html-temp | perl -p -e '$$| = 1; (s/Date://, s/Author:/par/, s/\$$//g) if(/\$$''Date:/); s/mode: xml \*\*\*/mode: html \*\*\*/' > $$base.fr.html) ; \
	rm -f $$base.fr.html-temp

%lang: 
	@$(ECHO) "Building $@ ..."; \
	path=$@ ; \
	base=`expr $$path : '\(.*\).lang'` ; \
	./tools/translate.sh $$base $(METACOSM) > /dev/null

doc:
	./tools/doc.sh fr > doc.fr.xhtml
	./tools/doc.sh en > doc.en.xhtml

# remove html files for which an xhtml version exists
clean:
	rm -f $(ENPAGES) $(FRPAGES) $(LANGFILES)
