#---------------------------------------------------------------
# Project         : Mandrake Linux
# Module          : Borges
# File            : Makefile.manual
# Author          : Camille Bgnis
# Created On      : Mon May  1 16:25:21 2001
# Copyright       : Camille Bgnis camille@mandrakesoft.com,
#                    MandrakeSoft under the GPL license
#---------------------------------------------------------------
# This Makefile is responsible for performing all manual specific 
# tasks, specifically creating the master document (master.xml)
# from the specifications (master.top.xml); and creating the reports

include /home/stew/RPM/BUILD/Borges-0.12.2/doc/Makefile.include
conf=conf.xml

# Are there Makefiles specific for this manual?
LOCALOUTPUTS:=$(call GET,//makefile,$(conf))
ifneq ($(wildcard $(LOCALOUTPUTS)),)
OUTPUTS=$(wildcard $(LOCALOUTPUTS))
endif
include $(OUTPUTS)

XSLTEMPLATE=$(DISTDIR)/XSL/master2templates.xsl
XSLREPORT=$(DISTDIR)/XSL/bigbrother.xsl
XSLREVISIONS=$(DISTDIR)/XSL/gather_revhistories.xsl
XSLREVISIONS-TODO=$(DISTDIR)/XSL/bigbrother-todos.xsl
XSLFILTER=$(DISTDIR)/XSL/filter.xsl
XSLOMF=$(DISTDIR)/XSL/db2omf.xsl

IS_V58=$(shell perl -v | grep v5.8)

document=$(notdir $(PWD))
# Get module filenames for that manual
modules=$($(document)-modules)


# now get parameters from conf files

# formats for compiling all outputs
MANUALS:=$(call GET,//document/@id,$(conf))
# Compute needed local langs in the order given by conf/repository.xml
ALLLOCALLANGS:=$(call GET,//@lang,$(conf))
LOCALLANGS:=$(foreach l,$(ACTIVELANGS),$(findstring $(l),$(sort $(ALLLOCALLANGS))))

# the meta entities file pointing to other entities files
# plus the &lang; entity
entities="<!ENTITY % src SYSTEM '$(ROOTDIR)/$(MODULESDIR)/$(LANG)/modules.ent'> <!ENTITY % local SYSTEM '$(LANG)/catalog'> <!ENTITY % global-entities SYSTEM '$(ROOTDIR)/entities/$(LANG)/catalog'> %src; %local; %global-entities; <!ENTITY lang '$(LANG)'> <!ENTITY index SYSTEM 'index.xml'>"

# Possible targets to be cleaned
toclean=$(foreach man,$(MANUALS),$(wildcard $(man)*) ) 


.PHONY: all release

# make all available manuals according to conf.xml, plus OMF files
all:
	$(foreach lang,$(LOCALLANGS),$(MAKE) $(OUTPUTSDIR)/$(lang) LANG=$(lang); )

# make all available formats and all languages for a single subdoc, according to conf.xml, plus OMF file
%.all: $(conf)
# If we deal about a multilingual document only one language is needed
	if [ "$(call GET,//document[@id=\"$*\"]/@multilang,$<)" != "" ]; then \
		langs=$(call GET,//document[@id=\"$*\"]/language[1]/@lang,$<); \
	else \
		langs="$(foreach l,$(ACTIVELANGS),$(findstring $(l),$(sort $(call GET,//document[@id=\"$*\"]//@lang,$<))))"; \
	fi; \
	for lang in $$langs; do \
		$(foreach ext,$(call GET,//document[@id=\"$*\"]/style/@format,$(conf)), \
			$(MAKE) $*.$(ext) LANG=$$lang; \
			mkdir -p  $(OUTPUTSDIR)/$$lang; \
			rm -rf  $(OUTPUTSDIR)/$$lang/$*.$(ext); \
			mv $*.$(ext) $(if $(findstring flat,$(ext)),images) $(OUTPUTSDIR)/$$lang; ) \
	done
	$(MAKE) $*.omf
	mv $*.omf $(OUTPUTSDIR)

# make all available documents for a single language, according to conf.xml, plus OMF files
$(OUTPUTSDIR)/$(LANG): $(sources)
	@echo "****** Making $@ ....."
	rm -rf $(OUTPUTSDIR)/$(LANG)
	mkdir -p $(OUTPUTSDIR)/$(LANG)
	$(foreach doc,$(call GET,//document[//@lang=\"$(LANG)\"]/@id,$(conf)), \
		$(foreach ext,$(call GET,//document[@id=\"$(doc)\"]/style/@format,$(conf)), \
			$(MAKE) $(doc).$(ext) $(doc).omf && \
			rm -rf  $(OUTPUTSDIR)/$(LANG)/$(doc).$(ext); \
			mv -f $(doc).$(ext) $(if $(findstring flat,$(ext)),images) $(OUTPUTSDIR)/$(LANG) && \
			if [ ! -e $(wildcard $(OUTPUTSDIR)/$(doc).omf) ]; then \
				$(MAKE) $(doc).$(ext) $(doc).omf && mv -f $(doc).omf $(OUTPUTSDIR); fi; ))

# scan entities files 
entities: i18n $(wildcard $(ROOTDIR)/entities/$(LANG)/*.ent) $(wildcard $(foreach mod,$(modules),$(ROOTDIR)/$(MODULESDIR)/$(LANG)/$(mod))) $(if $(IMPORTS),$(wildcard $(ROOTDIR)/$(MODULESDIR)/$(IMPORTS)/$(LANG)/*)) $(wildcard $(LANG)/*.ent) 
ifneq ($(IMPORTS),)
	cd $(ROOTDIR)/$(MODULESDIR); \
	for i in $(IMPORTS); do \
		if [ -e $$i/$(LANG) ]; then $(MAKE) -rC $$i all LANGS=$(LANG); fi; \
	done;
endif
	$(MAKE) -rC $(ROOTDIR)/$(MODULESDIR)/$(LANG) modules.ent
	$(MAKE) -rC $(ROOTDIR)/entities/$(LANG) catalog
	$(MAKE) -rC $(LANG) catalog
	echo $(entities) > entities
# We discovered some random results due to identical timestamps.
# The following line solves the problem. Why? I hope you'll tell me...
	touch -d '1 second ago' $@


ifeq ($(TOPSRC),)
TOPSRC=$(TOP)
endif
NEWENTS=strings.ent
# create entity files templates for each language with entities found in new master
newentities: $(TOPSRC)
	@echo "****** Creating template entities in $(NEWENTS)....."
	extract_entities.pl $< $(TOP) > $(NEWENTS).tmp
	$(foreach lang,$(LANGS), \
		if [ ! -e $(lang)/$(NEWENTS) ]; then \
			echo "<?xml version=\"1.0\" encoding=\"$(ENCODING)\"?>" > $(lang)/$(NEWENTS); \
			echo >> $(lang)/$(NEWENTS); \
		fi; \
		cat $(NEWENTS).tmp >> $(lang)/$(NEWENTS); \
		rm -f $(lang)/catalog; \
		$(MAKE) -rC $(LANG) catalog; )
	rm -f $(NEWENTS).tmp

# this is a trick to keep track of the lang used for last compilation
i18n: i18n.$(LANG)
	touch $@

i18n.%:
	@echo "****** Setting current language to $* ....."
	rm -f i18n.*
	touch $@

# creates the master from specifications 
master.xml master.filtered.xml: $(TOP) i18n entities
	@echo "****** Filtering out excluded elements....."
# perl version 5.8 has better utf-8 support
ifneq ($(IS_V58),)
	master2master.pl $< >$@
else
	master2master.pl $< | iconv -f UTF-8 -t ISO-8859-1> $@
endif
ifdef exclude
# the &nbsp; &lt; &amp; entities must not be transformed!
	perl -pi -e "s/&(nbsp|lt|amp);/&amp;$$1;/" $@
	xsltproc -o master.filtered.xml --stringparam exclude $(exclude) $(XSLFILTER) $@
	DOCTYPE='$(shell $(DISTDIR)/bin/extract_doctype.pl $(TOP))'; \
	perl -p -e "s#(<\?xml.*)#\1\n$$DOCTYPE#" master.filtered.xml > $@
	rm -f master.filtered.xml
endif

# master guidelines source
master.top.flat.xml: $(conf) i18n entities $(TOP)
	cp -f master.top.xml $@

# Catch *.xml targets and make a specialized manual out of it 
# following conf.xml excludes
# also we must verify all depending modules exists with DTD specific %.mod.deps target
%.flat.xml: $(conf) i18n entities %.index.deps %.mod.deps $(TOP) $(if $(IMPORTS),$(wildcard $(ROOTDIR)/$(MODULESDIR)/$(IMPORTS)/$(LANG)/*))
# If this is a multi-languages document there is a special treatment
# We need also to make images for all languages.
	if [ "$(call GET,//document[@id=\"$*\"]/@multilang,$<)" != "" ]; then \
		echo "******   Processing a Multilingual document  ******"; \
		$(foreach lang,$(call GET,//document[@id=\"$*\"]/language/@lang,$<), \
			$(MAKE) master.xml exclude="\"$(call GET,//document[@id=\"$*\"]/exclude,$<) $(EXCLUDE)\"" LANG=$(lang); \
			mv master.xml $*.$(lang).flat.xml; ) \
		tie_ml_doc.pl $* master.xml; \
		rm -f $*.*.flat.xml; \
	else \
		$(MAKE) master.xml exclude="\"$(call GET,//document[@id=\"$*\"]/exclude,$<) $(EXCLUDE)\""; \
	fi
	mv -f master.xml $@
# get back to original language
	$(MAKE) entities

# the same but without index for making the index...
%.noindex.xml: $(conf) i18n entities %.mod.deps $(TOP) $(if $(IMPORTS),$(wildcard $(ROOTDIR)/$(MODULESDIR)/$(IMPORTS)/$(LANG)/*))
	touch index.xml
	$(MAKE) master.xml exclude="\"$(call GET,//document[@id=\"$*\"]/exclude,$<)\""
	mv master.xml $@

# Following three targets are used during output compilation to make sure all needed dependencies are available.
# For each, the .dependencies target is provided by the $(OUTPUTS) Makefile.
%.mod.deps: %.mod.dependencies
	@echo "******   Making module dependencies for document $* in $(LANG) ..."
	$(MAKE) -r -C $(ROOTDIR)/$(MODULESDIR)/$(LANG)/ $(foreach dep,$(shell cat $< | sort -u),$(notdir $(dep)) )

%.index.deps: %.index.dependencies
	@echo "******   Making index dependencies for document $* in $(LANG) ..."
	rm -f *.index.xml
	$(foreach dep,$(shell cat $< | sort -u),$(MAKE) -r -C $(dir $(dep)) $(notdir $(dep));)

%.img.deps %.missing.$(LANG).img: %.img.dependencies
	@echo "******   Making images  dependencies for document $* in $(LANG) ..."
	rm -f $(ROOTDIR)/$(MANUALSDIR)/images/$(LANG)/missing.img
# If this is a multi-languages document we need to make images for all languages.
	if [ "$(call GET,//document[@id=\"$*\"]/@multilang,$(conf))" != "" ]; then \
		$(foreach lang,$(call GET,//document[@id=\"$*\"]/language/@lang,$(conf)), \
			$(MAKE) -r -C $(ROOTDIR)/$(MANUALSDIR)/images/$(lang)/ $(foreach dep,$(shell cat $<),$(notdir $(dep)) ) LANG=$(lang); ) \
			echo ; \
	else \
		$(MAKE) -r -C $(ROOTDIR)/$(MANUALSDIR)/images/$(LANG)/ $(foreach dep,$(shell cat $<),$(notdir $(dep)) ); \
	fi
	rm -rf images
	ln -s $(ROOTDIR)/$(MANUALSDIR)/images/$(LANG) images
	if [ -e $(ROOTDIR)/$(MANUALSDIR)/images/$(LANG)/missing.img ]; then \
		sort -u $(ROOTDIR)/$(MANUALSDIR)/images/$(LANG)/missing.img > $*.missing.$(LANG).img; \
		rm -f $(ROOTDIR)/$(MANUALSDIR)/images/$(LANG)/missing.img; \
	else rm -f $*.missing.$(LANG).img; touch $*.missing.$(LANG).img; \
	fi
	touch images

# Make sure xml file (master) validates against DTD.
%.validate: %.xml $(wildcard $(sources)) entities
	echo
	echo "************* Validating $* document....."
	xmllint --noout --valid $< > $@ 2>&1
	if [ -s $@ ]; then \
		echo "******************************************************************"; \
		echo "******* $* Not Valid: Consult $@ ********"; \
		echo "******************************************************************"; \
		exit 1; \
	else \
		echo "******* Validation OK ********"; \
		rm -f $@; \
	fi
ifndef NOTEMPLATES
	for l in $(LANGS); do for m in $(modules); do \
		ls ../../$(MODULESDIR)/$$l/$$m 1> /dev/null 2>> $@; done; done; \
	if [ -e $@ ] && grep 'ls: ../../modules/' $@ > /dev/null; then \
		echo "******************************************************************"; \
		echo "******* Missing module templates: Consult $@ ********"; \
		echo "******* You should run 'make alltemplates' ********"; \
		echo "******************************************************************"; \
		echo "***************** You should run 'make alltemplates' *************" >> $@; \
		exit 1; \
	fi
endif

master.top.commit: master.top.validate
	if [ -s master.top.validate ]; then \
		echo "******************************************************************"; \
		echo "******* $(TOP) Not Valid: Consult $< ********"; \
		echo "******************************************************************"; \
	elif [ "$(CVS)" == 'yes' ]; then \
		$(call cvsadd,$(TOP)) \
		cvs -z2 commit $(if $(LOGCVSCO),-m "$(LOGCVSCO)") $(TOP); \
	fi

# Create module templates according to master document
# put revision histories where needed
alltemplates:
	rm -f master.top.validate
	$(foreach lang,$(LANGS),$(MAKE) templates LANG=$(lang); )
	$(MAKE) master.top.commit

templates:
	$(MAKE) .templates.$(LANG) NOTEMPLATES=yes

# REL holds the current release of document
# NEWREL holds the release passed on the command line
# MODREL holds release to be used when generating module templates
ifeq ($(REL)$(NEWREL),)
REL=1.0
endif
ifeq ($(NEWREL),)
MODREL=$(REL)
else
MODREL=$(NEWREL)
endif

.templates.$(LANG): master.top.validate i18n entities $(wildcard $(addprefix $(ROOTDIR)/$(MODULESDIR)/$(LANG)/,$(modules)))
	if [ ! -s master.top.validate ]; then \
		echo "******* Generating module templates in $(LANG)"; \
		gen-templates.pl $(ROOTDIR)/$(MODULESDIR)/$(LANG) $(ENCODING); \
		$(MAKE) -C $(ROOTDIR)/$(MODULESDIR)/$(LANG) modules.ent; \
		$(foreach mod,$(basename $(modules)),$(MAKE) -C $(ROOTDIR)/$(MODULESDIR)/$(LANG) $(mod).release \
			CVSLOG="New module template to be filled." NEWREL=$(MODREL) NEWIDS=no; ) \
		touch  $@; \
	else \
		exit 1; \
	fi
# Make sure all escaped modules are available
.escape-modules: $(foreach lang,$(LOCALLANGS),$(addprefix $(ROOTDIR)/$(MODULESDIR)/$(lang)/,$(modules)))
	$(foreach  l,$(LANGS),$(MAKE) -rC $(ROOTDIR)/$(MODULESDIR)/$(l) $(foreach m,$(modules),$(m)a); )
	touch .escape-modules

# create an intermediate file containing all revision related info
# of all modules used in this document
master-revisions.xml: $(TOP) $(ROOTDIR)/conf/repository.xml $(foreach lang,$(LOCALLANGS),$(addprefix $(ROOTDIR)/$(MODULESDIR)/$(lang)/,$(modules))) $(foreach lang,$(LOCALLANGS),$(addprefix $(ROOTDIR)/$(MODULESDIR)/$(lang)/.,$(addsuffix .revhistory,$(basename $(modules))))) i18n entities $(XSLREVISIONS) $(conf)
	@echo "****** Gathering revision information from all modules....."
# Make sure all escaped modules are available
	$(foreach  l,$(LANGS),$(MAKE) -rC $(ROOTDIR)/$(MODULESDIR)/$(l) $(foreach m,$(modules),$(m)a); )
# gather current revhistories for computing diffs
	$(XSLT) -o $@ --param conf-file "'$(CONF)'" \
		--param authors-file "'$(AUTHORSCONF)'" \
		--param modules-path "'$(ROOTDIR)/$(MODULESDIR)'" \
		--param release "'$(REL)'" $(XSLREVISIONS) $<
ifeq ($(ISMODULE),)
# Compute diffs and update flags in *.revhistory
	for l in $(LOCALLANGS); do \
		mkdir -p reports/$$l; \
		for s in $(modules); do \
			m=`basename $$s .xml`; \
			echo "********************************* $$m ***************************************"; \
			$(MAKE) -rC $(ROOTDIR)/$(MODULESDIR)/$$l $$m.ids.html LANG=$$l manual=$(document); \
			cp $(ROOTDIR)/$(MODULESDIR)/$$l/{$$m.ids.html,$$m.changes.html} reports/$$l; \
		done; \
	done
# gather revhistories
	$(XSLT) -o $@ --param conf-file "'$(CONF)'" \
		--param authors-file "'$(AUTHORSCONF)'" \
		--param modules-path "'$(ROOTDIR)/$(MODULESDIR)'" \
		--param release "'$(REL)'" $(XSLREVISIONS) $<
endif
# insert LOCALLANGS in result
	for l in $(LOCALLANGS); do \
		$(PUT) --xpath=/modules/languages/lang --value="$$l" $@; \
	done

# Identify todo tasks in the revisions pool
master-revisions-todo.xml: master-revisions.xml
	@echo "****** Identifying ToDo tasks....."
	$(XSLT) -o $@ $(XSLREVISIONS-TODO) $<

# creates the global reports for that document
master-report.html costs.html: master-revisions-todo.xml $(XSLREPORT) i18n entities
	@echo "****** Computing Global report in $@ ....."
	$(XSLT) -o master-report.html --param reports-path "'reports'" $(XSLREPORT) $<
	@echo "****** Computing costs in costs.html ..."
	accounting.pl $< > costs.html

# add a new revision to revision history
release: $(TOP) entities master.release
# Add new revision cycle in all depending modules
	$(foreach lang,$(LANGS),$(MAKE) releases LANG=$(lang) manual=$(document) NEWIDS=no; )

# add a new revision in master
# if entities are not available XML::LibXML suppresses them from file...
master.release: $(conf)
	echo "***** Changing release to $(NEWREL)";
# Declare release in conf file
	$(foreach lang,$(LANGS), \
		$(foreach doc,$(MANUALS), \
			$(ADD) --xpath=/configuration/document[@id=\"$(doc)\"]/language[@lang=\"$(lang)\"]/revision --value='' $<; \
			$(ADD) --xpath=/configuration/document[@id=\"$(doc)\"]/language[@lang=\"$(lang)\"]/revision[1]/date --value="YYYY-MM-DD" $<; \
			$(ADD) --xpath=/configuration/document[@id=\"$(doc)\"]/language[@lang=\"$(lang)\"]/revision[1]/revnumber --value="$(NEWREL)" $<; ))
	if [ "$(CVS)" == 'yes' ]; then \
		echo "******* Committing conf changes to CVS ********"; \
		cvs -z2 commit $(if $(LOGCVSCO),-m "$(LOGCVSCO)") $< $(TOP); \
	fi

# add a new release cycle for all modules of that document
releases: $(TOP)
	$(MAKE) -C $(ROOTDIR)/$(MODULESDIR)/$(LANG) $(patsubst %.xml,%.release,$(modules))

# Set the start work date for progress reports
startwork: $(conf)
	today=$(if $(DATE),$(DATE),$$(date --iso-8601)); \
	echo "****** Setting start date for document $(document) in $(LANG) to $$today"; \
	$(foreach doc,$(MANUALS), \
		$(ADD) --xpath=/configuration/document[@id=\"$(doc)\"]/language[@lang=\"$(LANG)\"]/revision[1]/date --position=self --value="$$today" $<; )
	if [ "$(CVS)" == 'yes' ]; then \
		echo "******* Committing conf changes to CVS ********"; \
		cvs -z2 commit -m "Start work in $(LANG)" $<; \
	fi


# assign tasks for all modules of that document
revisions: $(TOP)
	$(MAKE) -C $(ROOTDIR)/$(MODULESDIR)/$(LANG) $(patsubst %.xml,%.revision,$(modules))

# Build the OMF file for scrollkeeper
%.omf: conf.xml $(TOP) entities
	@echo "****** Generating OMF data for $* ..."
	xsltproc -o $@ --stringparam doc $* $(XSLOMF) $<

# custom rules come in here:
include Makefile.include

# Local variables:
# mode: makefile
# End: