
#---------------------------------------------------------------
# Project         : Mandrake Linux
# Module          : Borges
# File            : Makefile
# Author          : Camille Bgnis
# Created On      : Tue Nov 27 2001 16:25:21 2001
# Copyright       : Camille Bgnis camille@mandrakesoft.com,
#                    MandrakeSoft under the GPL license
#---------------------------------------------------------------

include ../Makefile.include

XSLSUMMARY=$(DISTDIR)/XSL/report_summary.xsl
XSLMAILS=$(DISTDIR)/XSL/report2mail.xsl
XSLCONTRIBS=$(DISTDIR)/XSL/summarize_tasks.xsl
XSLREVISIONS=$(DISTDIR)/XSL/gather_revhistories.xsl
XSLTODO=$(DISTDIR)/XSL/todo_tasks.xsl
XSLREVISIONS-TODO=$(DISTDIR)/XSL/bigbrother-todos.xsl
XSLMODULES-LIST=$(DISTDIR)/XSL/list_modules.xsl

reports=$(addsuffix /master-report.html,$(ACTIVEMANUALS))

mail_header="Hi,\n\nI am R2D2, and these are the tasks I have found assigned to you for manuals:\n\n"
mail_footer="\n\nDo not forget to update the revisions in each module file so that this report is always up-to date.\n\nThanks, R2D2."

# Additional dirs to be cleaned
toclean=$(MANUALS) images

# Current release number
REL=$(strip $(shell cat $(ROOTDIR)/VERSION))

# this target compiles all manuals in PDF and HTML in all languages
# plus the report.
# all is then stored in reports/
# then reports/ is suited for Web publication
all: index.html allguidelines

allguidelines:
	-for m in $(ACTIVEMANUALS); do \
		$(MAKE) -C $(ROOTDIR)/$(MANUALSDIR)/$$m/ master.top.{pdf,flat.html}; \
		mkdir -p $$m/; \
		cp -Ru $(ROOTDIR)/$(MANUALSDIR)/$$m/{master.top.{pdf,flat.html},images} $$m/; \
	done

%/master-report.html: force
	$(MAKE) -rC ../$(MANUALSDIR)/$*/ master-report.html
	rm -rf $*
	mkdir -p $*
	cp -a ../$(MANUALSDIR)/$*/master-report.html $*
	[ -e ../$(MANUALSDIR)/$*/reports ] && cp -a ../$(MANUALSDIR)/$*/reports $*

index.html: $(reports) $(XSLSUMMARY)
	@echo "****** Generating report index page..."
	$(XSLT) --param manualdir '"$(ROOTDIR)/$(MANUALSDIR)"'  --param release '"$(REL)"' \
		-o $@.tmp $(XSLSUMMARY) $(CONF)
# Some timezones as set in Linux are not recognized by Date::Manip
# That's why I force it to GMT
	TZ=GMT ETA.pl $@.tmp > $@
	rm -f $@.tmp

# URLs to be scattered in mail footer
frontendurl-perso=$(call GET,configuration/envvar[@id=\"frontendserver\"],$(PUBCONF))/~$(call GET,configuration/envvar[@id=\"frontendurl\"],$(PUBCONF))
outputsurl=$(call GET,configuration/envvar[@id=\"outputsurl\"],$(PUBCONF))/$(OUTPUTSDIR)
reportsurl=$(call GET,configuration/envvar[@id=\"reportsurl\"],$(PUBCONF))/reports
CVS_RSH=$(call GET,configuration/envvar[@id=\"CVS_RSH\"],$(PUBCONF))
CVSROOT=$(call GET,configuration/envvar[@id=\"CVSROOT\"],$(PUBCONF))
# tasks mails for authors
mails: frontend
	rm -f *@*
	@echo "****** Preparing mails..."
	if ls frontend/*.mail >/dev/null 2>&1; then \
		mv frontend/*.mail .; \
		for t in $$(ls *.mail); do \
			mail=`basename $$t .mail`; \
			echo "Preparing mail for $$mail"; \
			printf $(mail_header) > $$mail; \
			cat $$t >> $$mail; \
			printf $(mail_footer) >> $$mail; \
			if [ -e ../conf/mailfooter.txt ]; then \
				cat ../conf/mailfooter.txt >> $$mail; \
				a=$$(confparse.pl "//author[affiliation/address/email[text()=\"$$mail\"]]/@id" $(AUTHORSCONF)); \
				perl -pi -e "s,\@frontendurl-perso\@,$(frontendurl-perso)/$$a.html," $$mail; \
			fi; \
			perl -pi -e "$(foreach var,outputsurl reportsurl,s%\@$(var)\@%$($(var))%; )" $$mail; \
			rm -f $$t; \
		done; \
	fi

.PHONY: sendmails
sendmails: mails
	for f in `find . -name \*@\* -exec basename {} \;`; \
		do cat $$f | mail $$f -s "Tasks list for $(proj_name) documentation"; done
	rm -f *@*

# This file gathers all revision histories relevant for a particular release
revhistories.xml: $(foreach mod,$(allmodules),$(dir $(mod))$(addprefix .,$(notdir $(addsuffix .revhistory,$(basename $(mod)))))) $(CONF) $(AUTHORSCONF)
# Create a dummy master document that gathers all used modules
	@echo '<?xml version="1.0" encoding="ISO-8859-1"?>' > master.xml
	@echo "<modules>" >> master.xml
	$(foreach mod,$(sort $(foreach m,$(ACTIVEMANUALS),$(call GET,//\*[@role=\"module\"]/@id,$(ROOTDIR)/$(MANUALSDIR)/$(m)/master.top.xml ))), \
		echo "<module role=\"module\" id=\"$(mod)\"/>" >> master.xml; )
# This is used to know which module is used in which document
	$(foreach man,$(ACTIVEMANUALS), \
		echo "<document id=\"$(man)\">" >> master.xml; \
		$(foreach mod,$(sort $(call GET,//\*[@role=\"module\"]/@id,$(ROOTDIR)/$(MANUALSDIR)/$(man)/master.top.xml )), \
			echo "<module id=\"$(mod)\"/>" >> master.xml; ) \
		echo "</document>" >> master.xml; )
	@echo "</modules>" >> master.xml
# Make sure all escaped modules are available
	$(foreach m,$(ACTIVEMANUALS),$(MAKE) -C $(ROOTDIR)/$(MANUALSDIR)/$(m) .escape-modules; )
# gather revhistories
	$(XSLT) -o $@ --param conf-file "'$(CONF)'" \
		--param authors-file "'$(AUTHORSCONF)'" \
		--param modules-path "'$(ROOTDIR)/$(MODULESDIR)'" \
		--param release "'$(REL)'" $(XSLREVISIONS) master.xml

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

# Lists all existing modules 
modules-list.html: revhistories-todo.xml $(XSLMODULES-LIST)
	@echo "****** listing all modules....."
	echo "<allmodules>" > modules-list.xml
	for m in $$(ls $(ROOTDIR)/$(MODULESDIR)/$(LANG)/.*.revhistory | sed -e "s/.*\/\.\(.*\).revhistory/\1/"); do \
		echo "<module id=\"$$m\"/>" >> modules-list.xml; \
		$(MAKE) -C $(ROOTDIR)/$(MODULESDIR)/$(LANG) $$m.xmla; \
	done 
	echo "</allmodules>" >> modules-list.xml
	$(XSLT) -o $@ --stringparam revhistories $< $(XSLMODULES-LIST) modules-list.xml
#	rm -f modules-list.xml

contributions.html: revhistories.xml $(XSLCONTRIBS)
ifeq ($(REL),)
	@echo "****** I meed a release number! *********"
	@echo "Usage: make contributions.html REL=0.8"

	@echo "       0.8 being the release number of the documents you want the contributions"
else
	@echo "****** Calculating contributions costs in $@..."
	$(XSLT) --stringparam release $(REL) $(XSLCONTRIBS) $< > $@
endif

%/costs.html: $(allmodules) $(ROOTDIR)/$(MANUALSDIR)/%/master.top.xml $(CONF)
	$(MAKE) -rC $(ROOTDIR)/$(MANUALSDIR)/$*/ $(@F); \
	mkdir -p $*; \
	cp -f $(ROOTDIR)/$(MANUALSDIR)/$*/$(@F) $*; \

accounting.html: $(foreach man,$(ACTIVEMANUALS),$(man)/costs.html) $(CONF)
	@echo "****** Generating accounting report in $@..."
	costs.pl $(CONF) costs.html > $@

.PHONY: frontend $(XSLTODO)
frontend: revhistories-todo.xml
	@echo "****** Generating Web Frontend HTML files..."
	mkdir -p $@
	export project=$$(echo -n $(frontendurl) | sed -e  "s,/$$,,; s,.*/,,"); \
	xsltproc -o $@/index.html --stringparam cgi $(cgibinurl)/web-frontend-$$project-$(REL).cgi \
		 --stringparam release $(REL) --stringparam url $(frontendurl)/repository/modules/ \
		 $(XSLTODO) $<

publish-frontend: frontend
	@echo "****** Publishing Web Frontend HTML files..."
	mkdir -p $</cgi-bin/ ~$(frontendpath) ~/public_html/cgi-bin/
# make sure permissions are compatible with apache2-mod_suexec
	chmod 755 ~/public_html/cgi-bin/
# Get available modules output formats and inform CGI about various params
	export formats="$(sort $(call GET,//format,$(ROOTDIR)/$(MANUALSDIR)/module/conf.xml))"; \
	export repository=$$(echo ~$(frontendpath)/repository); \
	export project=$$(echo $(frontendurl) | sed -e  "s,/$$,,; s,.*/,,"); \
	perl -p -e "s,script=\"web-frontend.cgi\",script=\"web-frontend-$$project-$(REL).cgi\",; s,url=\"borges-frontend/\",url=\"~$(frontendurl)\",; s,localpath=\"/repository\",localpath=\"$$repository\",; s,formats=\"html pdf\",formats=\"$$formats\"," \
		$(DISTDIR)/bin/web-frontend.cgi > $</web-frontend-$$project-$(REL).cgi
	chmod 644 $</*.html
	chmod 755 $</web-frontend*
	rm -f $</repository
	ln -sf $(ROOTDIR) $</repository
	$(call transfer,$(frontend-proto),$</web-frontend*,~$(cgibinpath))
	rm -f $</web-frontend*
	$(call transfer,$(frontend-proto),$</*,~$(frontendpath))

# Local variables:
# mode: makefile
# End: