#---------------------------------------------------------------
# Project         : Mandrake Linux
# Module          : Borges
# File            : Makefile.module
# 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 module specific 
# tasks, specifically creating the reports for revision checking

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

XSL_EXTRACT_ID=$(DISTDIR)/XSL/extract_ids.xsl
XSL_EXTRACT_REVISIONS=$(DISTDIR)/XSL/extract_revisions.xsl
XSLFILTER=$(DISTDIR)/XSL/filter.xsl

xml_head='<?xml version="1.0" encoding="ISO-8859-1" ?><revisions>'
xml_foot='</revisions>'

deps:= $(foreach ext,.ids.xml .xmla .id.xml .src-ids.xml,$(addsuffix $(ext),$(basename $(wildcard *.xml))))

# Wich lang is this modules repository
LOC_LANG:=$(shell pwd | sed -e "s,.*/,,")

# modules in current language
modules:=$(filter $(ROOTDIR)/$(MODULESDIR)/$(LOC_LANG)/%,$(allmodules))

# the .*.revhistory files also hold information on modules
# namely whether they are valid XML
# <revhistory conformance="1">
# This function maintains this

# a function that switches a module $(1) variable $(2) to another value $(3) in module revhistory
SWITCH=$$([ -e .$(1).revhistory ] && perl -pi -e "s,$(2)=\"[^\"]\",$(2)=\"$(3)\"," .$(1).revhistory)


.PRECIOUS: $(deps)

test:
	echo $(modules)

# index all useful modules for inclusion from masters.
# I cannot use $(modules) just below because it is redefined by makefile.onemodule
modules.ent: $(wildcard *.xml$(filter $(ROOTDIR)/$(MODULESDIR)/$(LOC_LANG)/%,$(allmodules)))
	@echo "****** Catalogging modules in $(PWD)....."
	rm -f $@
ifeq ($(strip $(wildcard $(modules))),)
	touch $@
else
	for f in $$(ls *.xml | sed -e "s/\..*\././" | sort -u); do \
		i=`basename $$f | sed -e "s/\.db\./\./"`; \
		echo -n "<!ENTITY ">> $@; \
		echo -n $${i%.xml} >> $@; \
		echo -n " SYSTEM \"">> $@; \
		echo -n $$f >> $@; \
		echo "\">" >> $@; \
	done
endif

# Mark a module status in modules.ent
%.status:
	@echo "**** changing $(*F) $(VAR) to $(VAL)"
	-$(call SWITCH,$(*F),$(VAR),$(VAL))

# Check module's validity
%.validate: %.xml
	@echo
	@echo " ************* Validating $<....."
	perl -p -e "s/&[^;]*;//g" $< > $*.xmlb
	-xmllint --noout --dtdvalid $(DTDURI) $*.xmlb > $@ 2>&1;
	if grep '.xmlb:' $@ | grep -v IDREF > /dev/null 2>&1; then \
		echo "******* NOT Valid !!!!!! ********"; \
		$(MAKE) $(*F).status VAR=conformance VAL=0; \
	else \
		echo "******* Validation OK ********"; \
		$(MAKE) $(*F).status VAR=conformance VAL=1; \
		rm -f $@; touch $@; \
	fi
	rm -f $*.xmlb

# This parameter is used to know whether we are switching to a
# "translation" or whether we are adding a true new release
NEWCYCLE=original
# Change release number
%.release: %.xmla .%.revhistory
# increment Module's current release number by adding a new revisions cycle
ifeq ($(NEWREL),)
	@echo "******* I need a release Number!"
else
	@echo "******* Adding release $(NEWREL) in $*.xml in ~$(LOC_LANG)~"
# Add new release cycle only if not already there or if we start a translation
# If the translation is not synchronized with original, do not assign IDs, just delete them
# if this is the initial module release, do not attribute IDs
	currel=$$($(DISTDIR)/bin/confparse.pl \(//revnumber\)[1] .$*.revhistory); \
	if [ "$$currel" != $(NEWREL) ] || [ $(NEWCYCLE) == translation ]; then \
		if [ "$(NEWIDS)" != no ]; then \
			if ! grep 2translate .$*.revhistory > /dev/null && \
					[ $$(confparse.pl //revision[revremark/text\(\)=\"synch\"][1]/date .$*.revhistory) == YYYY-MM-DD ]; \
			then \
				$(MAKE) $*.delid; \
			else \
				$(MAKE) $*.newid; \
			fi; \
		fi; \
		revisions_manager.pl add $(NEWCYCLE) $(NEWREL) $(totranslate) $(*F); \
		mv -f .$*.revhistory.tmp .$*.revhistory; \
		if which tidy > /dev/null 2>&1; then \
			tidy -q -i -xml -m .$*.revhistory; \
		fi; \
	fi
	$(MAKE) $*.commit
endif

# Removes possible automatic Ids and assign them.
%.delid: %.xml
	@echo "****** Removing Automatic IDs from $* in ~$(LOC_LANG)~..."
	perl -pi -e "s/\sid=\"BId[^\"]*\"//g" $<

# Removes possible automatic Ids and reassign them.
%.newid: %.delid
	$(MAKE) $*.id

# Assign automatic Ids.
%.id: %.xmla
	@echo "****** Assigning element IDs $* in ~$(LOC_LANG)~..."
	scatter_ids.pl $< $* > $*.xml
	perl -pi -e "s/&amp;/&/g" $*.xml

# Check there is no newer revision
.%.revhistory: force
ifeq ($(CVS),yes)
	$(if $(@D),,cvs -z2 update $@)
endif

# extract the ID/revision information from a module
# and its original (src-) module in case we deal with a translation 
%.ids.xml %.src-ids.xml: %.xmla $(foreach lang,$(LANGS),$(ROOTDIR)/$(MODULESDIR)/$(lang)/%.xml $(ROOTDIR)/$(MODULESDIR)/$(lang)/.%.revhistory)
	-$(MAKE) $(*F).validate
# Start gathering revision histories for that module so that we can know which language is the original
	$(MAKE) -C $(ROOTDIR)/$(MANUALSDIR)/module/ $*.revisions-todo.xml
# Compute local lang IDs list
	$(XSLT) -o $*.ids.xml --param module '"$(*F)"' --param manualpath '"$(ROOTDIR)/$(MANUALSDIR)"' --param modulepath '"$(ROOTDIR)/$(MODULESDIR)"' --param lang '"$(LOC_LANG)"' $(XSL_EXTRACT_ID) $<
# if it is a translation, process the original
	if [ -e .lang.master ]; then \
		MLANG=`cat .lang.master`; \
		if [ $(LOC_LANG) != $$MLANG ]; then \
			export path=$(ROOTDIR)/$(MODULESDIR)/$$MLANG; \
			cd $$path; \
			make $*.xmla; \
			$(XSLT) -o $*.ids.xml --param manual '"$(manual)"' --param module '"$(*F)"' --param manualpath '"$(ROOTDIR)/$(MANUALSDIR)"' --param modulepath '"$(ROOTDIR)/$(MODULESDIR)"' --stringparam lang $$MLANG $(XSL_EXTRACT_ID) $(*F).xmla; \
			cd $(ROOTDIR)/$(MODULESDIR)/$(LOC_LANG); \
			mv $$path/$(*F).ids.xml ./$(*F).src-ids.xml; \
		else \
			cp $*.ids.xml $*.src-ids.xml; \
		fi; \
	else \
		cp $*.ids.xml $*.src-ids.xml; \
	fi
# this "else" was to avoid unnecessary recompilation next time?
	rm -f .lang.master

# creates the HTML report of ID/revision changes
# between a translation and the original
# quite ugly...
%.ids.html: %.ids.xml %.src-ids.xml
	@echo "****** Computing ID changes for $* in $(LOC_LANG)..."
# if modules.ent do not exists, make it
	if [ ! -e modules.ent ]; then $(MAKE) modules.ent; fi
	-diff2html --only-changes $*.ids.xml $*.src-ids.xml > $@
	if ( grep 'id="' $@ > /dev/null ); then \
		echo "******** Changes"; \
		$(MAKE) $(*F).passrevision TYPE=synch.todo AUTHOR=unchanged; \
		touch $@; \
		$(MAKE) $(*F).changes.html; \
	else \
		rm -f $@; touch $@; \
		echo "******** No changes"; \
		$(MAKE) $(*F).revision TYPE=synch AUTHOR=unchanged; \
		touch $(*F).changes.html; \
	fi

# creates the XML report of changes
# between a translation and the original
%.changes.xml %.src-changes.xml %.ids.html.xml: %.ids.html
	for l in $(LANGS); do $(MAKE) -rC $(ROOTDIR)/$(MODULESDIR)/$$l/ $*.xmla; done
	echo $(xml_head) > $<.xml
	grep 'class="modified"' $< | grep "revision" |\
		perl -p -e "s,&(nbsp|lt|gt);,,g" >> $<.xml
	echo $(xml_foot)  >> $<.xml
	$(XSLT) --param file1 '"$(ROOTDIR)/$(MODULESDIR)/$(LOC_LANG)/$*.changes.xml"' \
		--param file2 '"$(ROOTDIR)/$(MODULESDIR)/$(LOC_LANG)/$*.src-changes.xml"' \
		--param path '"$(ROOTDIR)/$(MODULESDIR)/"' $(XSL_EXTRACT_REVISIONS) $<.xml

# creates the XML report of changes from above files
%.changes.html: %.changes.xml %.src-changes.xml
	@echo "****** Computing content changes for $* in $(LOC_LANG)..."
	diff2html $^ > $@
	perl -pi -e "s/&amp;/&/g" $@

# special modules states triggering special actions
# First write that changes other langues to translations
writetask=$(call GET,\(//revisions/original/type[@role=\"1time\"]/name\)[1],$(rootconf))
# state that triggers translations
totranslate=$(call GET,\(//revisions/original/type[@role=\"2translate\"]/name\)[1],$(rootconf))
# state that tells a translation is done
translatetask=$(call GET,\(//revisions/translation/type[@role=\"1time\"]/name\)[1],$(rootconf))
# tasks for which cost is calculated based on the changes made
proportionaltasks=$(sort $(call GET,\(//revisions/*/type[weight[@role=\"proportional\"]]/name\),$(rootconf)))

%.revision: .%.revhistory
# First check Author is known
ifneq ($(filter $(AUTHOR),$(ALLAUTHORS) tbn unchanged),)
# Compute module revisions file
	$(MAKE) -C $(ROOTDIR)/$(MANUALSDIR)/module/ $*.revisions-todo.xml
# get the active revision in it
# and check we pass active task
	active=$$($(DISTDIR)/bin/confparse.pl \(//revhistory[@lang=\"$(LOC_LANG)\"]/revision[@status=\"active\"]/revremark\) $(ROOTDIR)/$(MANUALSDIR)/module/$*.revisions-todo.xml); \
	ask=$(TYPE); \
	if [ "$$active" == "" ] && [ "$(STRICT)" != no ]  && [ "$${ask%.todo}" == "$$ask" ]; then \
		if [ "$(AUTHOR)" != "unchanged" ]; then \
			echo "********* WARNING:  There is no active task on this module currently..."; \
		fi; \
	elif [ "$${ask%.todo}" == "$(TYPE)" -a "$(TYPE)" != "$$active" -a "$(STRICT)" != no ] ; then \
		if [ "$(AUTHOR)" != "unchanged" ]; then \
			echo "********* WARNING:  You can only pass current active task: $$active"; \
		fi; \
	else \
		$(MAKE) $*.passrevision; \
	fi
else
	@echo "********* ERROR: Author ($(AUTHOR)) is unknown. Known authors are: $(ALLAUTHORS)."
	exit 1
endif

LOGCVSCO="Automatic Borges Commit"
# add a revision entry/date into a module info header
%.passrevision: %.xmla %.validate
# Ensure module is valid first
	if grep 'conformance="0"' $(ROOTDIR)/$(MODULESDIR)/$(LOC_LANG)/.$*.revhistory > /dev/null 2>&1; then \
		echo "************************************************************************"; \
		cat $$(for m in $(modules); do echo $$(basename $$m .xml).validate; done); \
		echo "********* Found errors in modules: see above *********"; \
		exit 1; \
	fi
# ensure first that there is a revhistory
	grep revhistory .$*.revhistory > /dev/null || \
		(echo "**** There is no revision history for $*; Generate the module templates first! ****"; \
		exit 1)
ifndef TYPE
	@echo
	@echo "********* ERROR:  What is the type of revision you want to publish?"
	@echo "Usage: make <LCA1-1.1.1>.revision TYPE=[write|translate|tproof|pproof|lproof|ispell] [AUTHOR=<your_initials>]"
else
ifeq ($(TYPE),$(writetask))
# This is no more a template
	perl -pi -e 's/ role="template"//' $<
# use the translation workflow for other languages and commit them to cvs
	$(foreach lang,$(filter-out $(LOC_LANG),$(LANGS)),echo "******* Switching to translation workflow in $*.xml in ~$(lang)~"; \
		$(MAKE) -C $(ROOTDIR)/$(MODULESDIR)/$(lang) $*.release $*.commit NEWCYCLE=translation NEWREL=$(REL) NEWIDS=no CVSLOG="This language is a translation"; )
endif
ifeq ($(TYPE),$(totranslate))
# Assign automatic IDs if needed
	$(MAKE) $*.id
# copy original to translations if needed
# .xmla is needed by translation_template.pl
	$(foreach lang,$(filter-out $(LOC_LANG),$(LANGS)), \
		cd ../$(lang); \
		$(MAKE) $*.xmla; \
		translation_template.pl $(LOC_LANG) $*.xml; \
		$(MAKE) -C $(ROOTDIR)/$(MODULESDIR)/$(lang) $*.commit CVSLOG="Original to be translated"; )
endif
# ifeq ($(TYPE),$(translatetask))
#	$(MAKE) -r $*.id
# endif
	@echo "********* Changing \"$*\" module \"$(TYPE)\" task to \"$(AUTHOR)\""
ifneq ($(filter $(TYPE),$(proportionaltasks)),)
# Assign automatic IDs if needed for costs calculation
	$(MAKE) $*.id
# Compute module revisions file if not already there
	$(MAKE) -C $(ROOTDIR)/$(MANUALSDIR)/module/ $*.revisions-todo.xml
endif
# actually change revision and possibly calculate changes ratio
	$(MAKE) $*.xmla
	revisions_manager.pl change $(TYPE) "$(AUTHOR)" "$(proportionaltasks) " $(ROOTDIR)/$(MANUALSDIR)/module/$*.revisions-todo.xml $*
# replace file only if it was actually modified otherwise make falls in endless loop while compiling reports
	if diff .$*.revhistory .$*.revhistory.tmp > /dev/null; then \
		rm -f .$*.revhistory.tmp; \
	else \
		mv -f .$*.revhistory.tmp .$*.revhistory; \
		touch .$*.revhistory; \
		$(MAKE) $*.commit CVSLOG="Passing task $(TYPE) by $(AUTHOR)"; \
	fi
endif

# Check module validity before allowing to commit it on CVS server
%.commit:
	make commit modules=$*

# Commit many modules at a time, checking validity
commit: $(foreach mod,$(basename $(modules)),$(mod).validate)
	if grep 'conformance="0"' $(foreach mod,$(notdir $(basename $(modules))),.$(mod).revhistory) > /dev/null 2>&1; then \
		echo "************************************************************************"; \
		cat $$(for m in $(modules); do echo $$(basename $$m .xml).validate; done); \
		echo "********* Found errors in modules: see above *********"; \
		exit 1; \
	elif [ "$(CVS)" == 'yes' ]; then \
		$(call cvsadd,$(foreach mod,$(basename $(notdir $(modules))),$(mod).xml .$(mod).revhistory)) \
		cvs -z2 commit $(if $(CVSLOG),-m "$(CVSLOG)") $(foreach mod,$(basename $(notdir $(modules))),$(mod).xml .$(mod).revhistory); \
	fi



# Local variables:
# mode: makefile
# End: