#
# New Makefile for XML source (Osamu Aoki)
#
#  Build multi file html, single file html, and single file text formats.
#  makefile for developer-reference was used as my reference.
#
# ------------------------------------------------------------------- #
#                          WARNING                                    #
#         Use with caution, aimed at Lenny system                     #
# ------------------------------------------------------------------- #

# do not use default suffix rules such as .tex.dvi: (uses tex, not latex!)
.SUFFIXES:
# =================================================================== #
#                 Default configuration part                          #
# =================================================================== #

BIN		:= bin
XSLT		:= xslt
IMAGES		:= images
WIKI_ROOT	:= wiki
HTML_ROOT	:= html
#LANG_ALL	:= en fr it es de pt-br pl zh-tw zh-cn ja
LANG_ALL  	:= en
LANG_EN   	:= en
LANG_PO   	:= 

# programs for creating output
XP=xsltproc --nonet --novalid --xinclude
XL=xmllint --nonet --noout --postvalid --xinclude

# The "--keep 0" should be removed as soon as the translations are ready
TRANSLATE=po4a-translate --format docbook --keep 0

# XSL files and parameters
# note: the URL is used as identifier, no HTTP is used!
DOCBOOK_XSL=http://docbook.sourceforge.net/release/xsl/current


REMOTE	:= people.debian.org:public_html/pub/getwiki/

# =================================================================== #
#                 Build rule part: XML source                         #
# =================================================================== #
# XML source contents files base filenames (static for build package)
MANUAL		:= debian-reference
SOURCE 		:= abstract appendix auth copy dataconversion datamanagement \
		i18nl10n internetapp network package preface program \
		systeminit systemtips test-all test tutorial xwindow $(MANUAL)
HTML_PAGES	:= apa ch01 ch02 ch03 ch04 ch05 ch06 ch07 ch08 ch09 ch10 \
		ch11 ch12 ch13

# XML source include files (static for build package)
ENTS 	:= common.ent pkgsize.ent popcon.ent urls.ent
# XML source all files (static for build package)
SRCS 	:= $(foreach lang, $(LANG_ALL), $(prefix $(lang)/, $(append $(lang).xml, $(SOURCE)))) $(ENTS)
# List of html index files to be built
HTML	:= $(foreach lang,$(LANG_ALL),$(HTML_ROOT)/index.$(lang).html) \
	$(foreach page,$(HTML_PAGES),$(foreach lang,$(LANG_ALL),$(HTML_ROOT)/$(page).$(lang).html)) \
	$(foreach lang,$(LANG_ALL),$(HTML_ROOT)/$(MANUAL).$(lang).html)
# List of txt to be built (single file html)
TXT	:= $(foreach lang,$(LANG_ALL),$(HTML_ROOT)/$(MANUAL).$(lang).txt) $(HTML_ROOT)/test.en.html
# All target
TARGETS	:= $(HTML) $(TXT)
# Blacklist for failing build
BLACKLIST	:=
#BLACKLIST	:= $(MANUAL).ja.pdf

.PHONY:	all
all:    $(filter-out $(BLACKLIST), $(TARGETS))

#.PHONY: validate
#validate:			$(SOURCES)
#	$(XL) index.dbk
#
#%/validate:			$(addprefix %/,$(SOURCES))
#	cd $(@D) && $(XL) index.dbk

.PHONY: update resethtml
# Update English source files from wiki directory
# This needs to be run explicitly
update:
	mkdir -p en
	for x in $(WIKI_ROOT)/*.en.xml; do cp -f $$x en/$$(basename $$x) ; done
	for x in $(WIKI_ROOT)/*.ent;    do cp -f $$x $$(basename $$x) ; done
	# Need to make $(LANG_ALL) (excluding en) from po4a

# Zap all data in $(HTML_ROOT) and reset contents
resethtml:
	-rm -rf $(HTML_ROOT)
	mkdir -p $(HTML_ROOT)/$(IMAGES)
	cp -f $(XSLT)/$(MANUAL).css $(HTML_ROOT)/$(MANUAL).css
	cp -rf $(IMAGES) $(HTML_ROOT)/

$(HTML_ROOT)/index.%.html:
### %/$(MANUAL).%.xml
	$(XP)	--stringparam root.filename index \
		--stringparam html.ext .$*.html \
		--stringparam html.stylesheet $(MANUAL).css \
		-o $(HTML_ROOT)/ $(XSLT)/style-html.xsl $*/$(MANUAL).$*.xml

$(HTML_ROOT)/$(MANUAL).%.html:
### %/$(MANUAL).%.xml
	$(XP)	--stringparam root.filename $(MANUAL) \
		--stringparam html.ext .en.html \
		--stringparam html.stylesheet $(MANUAL).css \
		-o $(HTML_ROOT)/ $(XSLT)/style-onehtml.xsl $*/$(MANUAL).$*.xml

$(HTML_ROOT)/test.en.html: en/test-all.en.xml
	$(XP)	--stringparam root.filename test \
		--stringparam html.ext .en.html \
		--stringparam html.stylesheet $(MANUAL).css \
		-o $(HTML_ROOT)/ $(XSLT)/style-onehtml.xsl en/test-all.en.xml

$(HTML_ROOT)/$(MANUAL).%.txt: $(HTML_ROOT)/$(MANUAL).%.html
	LC_ALL=en_US.UTF-8 w3m -dump -cols 65 -T text/html $(HTML_ROOT)/$(MANUAL).$*.html >$@

# Replicate
.PHONY: rsync
rsync:
	rsync --delete-after -rlpt . $(REMOTE)

nmu: $(foreach page,$(SOURCE),$(page).xml.diff)
	ls -l *.xml.diff >all.xml.diff
	gedit all.xml.diff  *.xml.diff

%.xml.diff: $(WIKI_ROOT)/%.en.xml
	if [ -f "$*.en.xml"]; then \
	diff -u $(WIKI_ROOT)/$*.en.xml $*.en.xml >$@ || true ; \
	else \
	:>$@ ; \
	fi


######################
#### There must be an easier way than recursive make!
###.PRECIOUS:		%.dbk %.ent
###ifndef LINGUA
###%.dbk %.ent: FORCE
###	$(MAKE) $@ LINGUA=`basename $(@D)`
###
###FORCE:
###else
###$(LINGUA)/%.dbk:	%.dbk $(patsubst %.dbk,po4a/$(LINGUA)/%.po,%.dbk)
###	$(TRANSLATE) -m $< -p po4a/$(@:.dbk=.po) -l $@
###
###$(LINGUA)/common.ent:	common.ent
###	cd $(@D) && ln -sf ../$(@F) .
###endif
###
###
###.PHONY: pot
###pot:				$(patsubst %.dbk,po4a/po/%.pot,$(SOURCES))
###po4a/po/%.pot:			%.dbk
###	po4a-gettextize --format docbook --master $< --po $@
###
###ifdef LINGUA
###.PHONY: updatepo
###updatepo:			$(patsubst %.dbk,po4a/$(LINGUA)/%.po,$(SOURCES))
###po4a/$(LINGUA)/%.po:		%.dbk
###	po4a-updatepo --format docbook --master $< --po $@
###endif
###
###%/version.ent:
###	echo '<!ENTITY version "$(VERSION)">' >  $@
###	echo '<!ENTITY pubdate "$(PUBDATE)">' >> $@
###
# Clean XML build environment 
.PHONY: clean wikiclean distclean targetclean
clean:
	$(MAKE) -C $(WIKI_ROOT) clean
	-rm -f *.xml.diff
	-rm -f `find . -name "*~" -o -name "*.bak" -o -name "*.rej" -o -name "*.orig" -o -name ".#*"`
	-rm -f core
wikiclean: clean
	$(MAKE) -C $(WIKI_ROOT) wikiclean
	
targetclean:
	-rm  -f $(TARGETS)

distclean: clean targetclean
	-rm -f $(SRCS)
	$(MAKE) -C $(WIKI_ROOT) distclean
###	rm -f version.ent
# if rule bomb out, delete the target
.DELETE_ON_ERROR:
