#---------------------------------------------------------------
# Project         : Linux-Mandrake
# Module          : Borges
# File            : Makefile
# Author          : Camille Bgnis, Fabian Mandelbaum
# Created On      : 2001-05-29
# Last Modif.     : 2002-12-21
# Copyright       : MandrakeSoft under the GPL license
#---------------------------------------------------------------
# dispatch "Makefile"s from "Makefile.in"s to
# required places and setup variables according to conf/

# Root directory where is installed the repository
ROOTDIR=$(shell pwd)
# Place where Borges is installed
DISTDIR=/usr/share/Borges
# Place where backend is available
BACKEND=$(DISTDIR)/backend
# most bins
PATH=$(shell echo $$PATH):$(DISTDIR)/bin
export PATH DISTDIR

# Main configuration file
CONF=conf/repository.xml
# Publisher configuration file
PUBCONF=conf/publish.xml
# Contributors list
AUTHORSCONF=conf/authors.xml

# now get parameters from conf files
# a function that looks for param $(1) in file $(2)
GET=$(shell [ -e $(2) ] && $(DISTDIR)/bin/confparse.pl $(1) $(2))
# a function that adds element $(1) with value $(2) file $(3)
PUT=[ -e $(3) ] && $(DISTDIR)/bin/confadd.pl --xpath=$(1) --value=$(2) $(3)
# Function responsible for transferring dir $(2) to $(3) (by $(1): local or ssh)
transfer=$(if $(filter ssh,$(1)),rsync -rvzl -e $(1) --exclude \.cvsignore --exclude CVS --exclude Makefile --delete $(2) $(3),$(if $(filter local,$(1)),mkdir -p $(3); cp -rf $(2) $(3)))

# Functiun returning the directories that are not in CVS
notincvs=$(strip $(foreach d,$(1),$(if $(wildcard $(d)/CVS),,$(d))))
# A fonction that cvs adds the dirs that are not already in CVS
cvsdiradd=$(if $(call notincvs,$(1)),cvs -z2 add $(call notincvs,$(1)))

#Where are manuals
MANUALSDIR:=$(call GET,configuration/repository/paths/manuals,$(CONF))
#Where are modules
MODULESDIR:=$(call GET,configuration/repository/paths/modules,$(CONF))
#URI of DTD used thoughout project
DTDURI:=$(call GET,configuration/repository/dtd,$(CONF))
DTDTYPE:=$(call GET,configuration/repository/doctype,$(CONF))
# manuals defined
MANUALS:=$(call GET,configuration/repository/manuals/manual,$(CONF))
# manuals being actively maintained
ACTIVEMANUALS:=$(call GET,configuration/repository/manuals/manual[not\(@status=\"inactive\"\)],$(CONF))
# external modules imported
IMPORTS:=$(call GET,configuration/repository/imports/import,$(CONF))
# langs supported
LANGS:=$(call GET,configuration/repository/languages/lang,$(CONF))
# langs being actively maintained
ACTIVELANGS:=$(call GET,configuration/repository/languages/lang[not\(@status=\"inactive\"\)],$(CONF))
# authors referenced
ALLAUTHORS:=$(call GET,authorgroup/*/@id,$(AUTHORSCONF))
# Default lang
DEF_LANG:=$(call GET,configuration/repository/languages/lang[not\(@status=\"inactive\"\)][1],$(CONF))
# Makefiles for compiling manuals
OUTPUTS:=$(call GET,configuration/repository/outputs/makefile,$(CONF))
# Dir to store all outputs
OUTPUTSDIR=Outputs
# XSLT Processors
XSLT:=$(call GET,configuration/backend/XSLT,$(CONF))
XSLTD:=$(call GET,configuration/backend/XSLTD,$(CONF))
# FO Processor
FO2PDF:=$(call GET,configuration/backend/FO2PDF,$(CONF))

# Current project release #
REL=$(strip $(shell cat VERSION))
export REL

# Are we inside a pool limited repository?
ifeq ($(POOL),)
POOL:=$(call GET,configuration/repository/@pool,$(CONF))
endif

# Publishing env vars
# Do not override environment
ifeq ($(CVS_RSH),)
CVS_RSH=$(call GET,configuration/envvar[@id=\"CVS_RSH\"],$(PUBCONF))
endif
ifeq ($(CVSROOT),)
CVSROOT=$(call GET,configuration/envvar[@id=\"CVSROOT\"],$(PUBCONF))
endif
reports-proto=$(call GET,configuration/envvar[@id=\"reports-proto\"],$(PUBCONF))
reportspath=$(call GET,configuration/envvar[@id=\"reportspath\"],$(PUBCONF))
outputs-proto=$(call GET,configuration/envvar[@id=\"outputs-proto\"],$(PUBCONF))
outputspath=$(call GET,configuration/envvar[@id=\"outputspath\"],$(PUBCONF))

# default revision types and associated responsibles:
TYPES:=$(call GET,configuration/repository/revisions/type/name,$(CONF))
AUTHORS:=$(call GET,configuration/repository/revisions/type/author,$(CONF))
# Dirs where compilation may occur
SUBS=$(foreach man,$(MANUALS) module,$(MANUALSDIR)/$(man) $(foreach lang,$(LANGS),$(MANUALSDIR)/$(man)/$(lang))) $(patsubst %,$(MODULESDIR)/%,$(LANGS)) $(patsubst %,$(MODULESDIR)/%,$(IMPORTS)) $(patsubst %,$(MANUALSDIR)/images/%,$(LANGS)) $(patsubst %,entities/%,$(LANGS)) reports

INCLUDE=$(ROOTDIR)/Makefile.include

# Want full output messages?
ifneq ($(DEBUG),yes)
.SILENT:
endif

# use CVS commands or not?
CVS:=$(if $(wildcard $(ROOTDIR)/CVS/Root),yes,no)

XSL_DOCS_INDEX=$(DISTDIR)/XSL/docs_index.xsl
# Name of manuals conf file
conf=conf.xml
# Look for all available sub-docs
SUBDOCS=$(foreach man,$(ACTIVEMANUALS),$(foreach sub,$(call GET,//@id,$(ROOTDIR)/$(MANUALSDIR)/$(man)/$(conf)),$(man)/$(sub)))

# Compile some available sub-documents $(SUBDOCS) (or all if not specified)
# in all available languages and formats as defined in conf.xml
all: $(OUTPUTSDIR)
$(OUTPUTSDIR): $(CONF) $(wildcard conf/* $(MODULESDIR)/*/* images/* images/*/* $(MANUALSDIR)/*/* $(MANUALSDIR)/*/*/*)
	rm -rf $(OUTPUTSDIR)
	mkdir -p $(OUTPUTSDIR)
ifeq ($(POOL),)
	for s in $(SUBDOCS); do \
		doc=$$(echo $$s | sed 's,/.*, ,'); \
		$(MAKE) -C $(MANUALSDIR)/$$(echo $$s | sed 's,/, ,').all; \
		cp -Ruf $(MANUALSDIR)/$$(echo $$s | sed 's,/.*,,')/$(OUTPUTSDIR)/* $(OUTPUTSDIR); \
	done
	xsltproc --stringparam rootdir $(ROOTDIR) --stringparam subdocs "$(SUBDOCS)" \
                $(XSL_DOCS_INDEX) $< > $(OUTPUTSDIR)/index.html
	touch $(OUTPUTSDIR)
else
	mkdir -p $(OUTPUTSDIR)/$(POOL)
	for doc in $$(confparse.pl "//pool[@id=\"$(POOL)\"]/document/@id" $(CONF)); do \
		man=$$(echo $$doc | sed -e "s,/.*,,"); \
		sub=$$(echo $$doc | sed -e "s,.*/,,"); \
		for lang in $$(confparse.pl "//pool[@id=\"$(POOL)\"]/document[@id=\"$$doc\"]/language/@lang" $(CONF)); do \
			for format in $$(confparse.pl "//pool[@id=\"$(POOL)\"]/document[@id=\"$$doc\"]/language[@lang=\"$$lang\"]/style/@format" $(CONF)); do \
				$(MAKE) -C $(MANUALSDIR)/$$man $$sub.$$format LANG=$$lang; \
				mkdir -p $(OUTPUTSDIR)/$(POOL)/$$lang; \
				cp -a $(MANUALSDIR)/$$man/$$sub.$$format $(OUTPUTSDIR)/$(POOL)/$$lang; \
			done; \
		done; \
	done
	@echo "******* Compiled all outputs for \"$(POOL)\" pool in $(OUTPUTSDIR)/$(POOL)/"
endif

.PHONY: configure

# Create all directories according to information in conf/repository
# and fill'em with appropriate Makefile and .cvsignore
# Also creates appropriate Makefile(s), .cvsignore and params.ent for single
# module compilation
configure:
	@echo "****** Updating backend and propagating configuration in whole repository..."
# If the installed Borges version is older than the one needed by the repository, abort
# If the installed Borges version is newer than the one needed by the repository, apply possible patches
	needed_borges_show=$(call GET,configuration/repository/borges,$(CONF)); \
	needed_borges=$$(echo $$needed_borges_show | perl -pe "s,(\d+\.\d+)\..*,\$$1,; s,\.(\d)$$,\.0\$$1,; s,\.(\d\d)$$,\.0\$$1," ); \
	installed_borges_show=$$(cat $(DISTDIR)/VERSION ); \
	installed_borges=$$(echo $$installed_borges_show | perl -pe "s,(\d+\.\d+)\..*,\$$1,; s,\.(\d)$$,\.0\$$1,; s,\.(\d\d)$$,\.0\$$1," ); \
	if [ "$$needed_borges" != "$$installed_borges" ]; then \
		echo "***********************************************"; \
		echo "        Current Borges version is: $$installed_borges"; \
		echo "        Required Borges version is:  $$needed_borges"; \
		echo "***********************************************"; \
		$(MAKE) checkout RECONF=no; \
		needed_borges_show=$(call GET,configuration/repository/borges,$(CONF)); \
		needed_borges=$$(echo $$needed_borges_show | perl -pe "s,(\d+\.\d+)\..*,\$$1,; s,\.(\d)$$,\.0\$$1,; s,\.(\d\d)$$,\.0\$$1," ); \
	fi; \
	if [[ $$needed_borges > $$installed_borges ]]; then \
		echo "***********************************************"; \
		echo "WARNING: Current Borges version is: $$installed_borges_show"; \
		echo "         Required Borges version is:  $$needed_borges_show"; \
		echo "Please update your Borges Installation ($(DISTDIR)) before continuing."; \
		echo "***********************************************"; \
		exit 1; \
	fi; \
	if [[ $$needed_borges < $$installed_borges ]]; then \
		if [ -e $(DISTDIR)/utils/migrate-$$needed_borges-$$installed_borges.sh ]; then \
			echo "***********************************************"; \
			echo "WARNING: Upgrading repository from Borges $$needed_borges_show"; \
			echo "         to Borges $$installed_borges_show"; \
			echo "***********************************************"; \
			$(DISTDIR)/utils/migrate-$$needed_borges-$$installed_borges.sh && \
			perl -pi -e "s,<borges>.*</borges>,<borges>$$installed_borges</borges>," $(CONF) && \
			echo "***********************************************"; \
			echo "Upgraded repository from Borges $$needed_borges_show to $$installed_borges_show"; \
			if [ "$(CVS)" == "yes" ]; then \
				echo "Don't forget to commit changes to the CVS repository."; \
			fi; \
			echo "***********************************************"; \
		elif [ -e $(DISTDIR)/utils/migrate-$$needed_borges-*.sh ]; then \
			upgrade=$$(echo $(DISTDIR)/utils/migrate-$$needed_borges-*.sh | sed -e "s/.*-\(.*\).sh/\1/"); \
			echo "***********************************************"; \
			echo "WARNING: You tried to Upgrading repository from Borges $$needed_borges_show"; \
			echo "         But your repository is too old."; \
			echo "         try to upgrade from Borges $$upgrade first."; \
			echo "***********************************************"; \
			exit 1; \
		elif [ -e $(DISTDIR)/utils/migrate-*-$$installed_borges.sh ]; then \
			upgrade=$$(echo $(DISTDIR)/utils/migrate-*-$$installed_borges.sh | sed -e "s/.*-\(.*\)-.*.sh/\1/"); \
			echo "***********************************************"; \
			echo "WARNING: You tried to Upgrading repository to Borges $$installed_borges_show"; \
			echo "         But your repository is too old."; \
			echo "         try to upgrade to Borges $$upgrade first."; \
			echo "***********************************************"; \
			exit 1; \
		else \
			perl -pi -e "s,<borges>.*</borges>,<borges>$$installed_borges_show</borges>," $(CONF) && \
			echo "***********************************************"; \
			echo "Upgraded repository from Borges $$needed_borges_show to $$installed_borges_show"; \
			if [ "$(CVS)" == "yes" ]; then \
				echo "Don't forget to commit changes to the CVS repository."; \
			fi; \
			echo "***********************************************"; \
		fi; \
	fi
	$(MAKE) -C conf author.xml
	$(MAKE) Makefile.include
	for i in $(LANGS); do \
		mkdir -p images/$$i; \
		mkdir -p $(MANUALSDIR)/images/$$i; \
		mkdir -p $(MANUALSDIR)/module/$$i; \
		mkdir -p entities/$$i; \
		mkdir -p $(MODULESDIR)/$$i; \
		perl -p -e "s,\@INCLUDE\@,$(INCLUDE),g" \
			$(BACKEND)/Makefile.images > $(MANUALSDIR)/images/$$i/Makefile; \
		perl -p -e "s,\@INCLUDE\@,$(INCLUDE),g" \
			$(BACKEND)/Makefile.src.images > images/$$i/Makefile; \
		cp -f $(BACKEND)/cvsignore.images $(MANUALSDIR)/images/$$i/.cvsignore; \
		cp -f $(BACKEND)/cvsignore.images images/$$i/.cvsignore; \
		perl -p -e "s,\@INCLUDE\@,$(INCLUDE),g" \
			$(BACKEND)/Makefile.entities > entities/$$i/Makefile; \
		cp -f $(BACKEND)/cvsignore.entities entities/$$i/.cvsignore; \
		perl -p -e "s,\@INCLUDE\@,$(INCLUDE),g" \
			$(BACKEND)/Makefile.module > $(MODULESDIR)/$$i/Makefile; \
		cp -f $(BACKEND)/cvsignore.module $(MODULESDIR)/$$i/.cvsignore; \
		for j in $(MANUALS); do \
			mkdir -p $(MANUALSDIR)/$$j/$$i; \
			perl -p -e "s,\@INCLUDE\@,$(INCLUDE),g" \
				$(BACKEND)/Makefile.entities > $(MANUALSDIR)/$$j/$$i/Makefile; \
				cp -f $(BACKEND)/cvsignore.entities $(MANUALSDIR)/$$j/$$i/.cvsignore; \
		done; \
		perl -p -e "s,\@INCLUDE@,$(INCLUDE),g" \
			$(BACKEND)/Makefile.entities > $(MANUALSDIR)/module/$$i/Makefile; \
		cp -f $(BACKEND)/cvsignore.entities $(MANUALSDIR)/module/$$i/.cvsignore; \
	done
	for j in $(MANUALS); do \
		perl -p -e "s,\@INCLUDE\@,$(INCLUDE),g" \
			$(BACKEND)/Makefile.manual > $(MANUALSDIR)/$$j/Makefile; \
		cp -f $(BACKEND)/cvsignore.manual $(MANUALSDIR)/$$j/.cvsignore; \
		if [ ! -e $(MANUALSDIR)/$$j/Makefile.include ]; then \
			cp -u $(BACKEND)/Makefile.manual.include $(MANUALSDIR)/$$j/Makefile.include; fi; \
		export target=$(MANUALSDIR)/$$j/conf.xml; \
		if [ ! -e $$target ]; then cp -u $(DISTDIR)/template/conf/manual-default.xml $$target; fi;\
		$(MAKE) -C $(MANUALSDIR)/$$j entities;\
	done
	cp -f $(BACKEND)/cvsignore.root .cvsignore
	cp -f $(BACKEND)/cvsignore.images images/.cvsignore
	perl -p -e "s,\@INCLUDE\@,$(INCLUDE),g" $(BACKEND)/Makefile.src.images > images/Makefile
	cp -f $(BACKEND)/cvsignore.manuals $(MANUALSDIR)/.cvsignore;
	cp -f $(BACKEND)/cvsignore.onemodule $(MANUALSDIR)/module/.cvsignore;
	perl -p -e "s,\@INCLUDE\@,$(INCLUDE),g" $(BACKEND)/Makefile.manual > $(MANUALSDIR)/module/Makefile
	cp -f $(BACKEND)/Makefile.onemodule.include $(MANUALSDIR)/module/Makefile.include;
	if [ ! -e $(MANUALSDIR)/module/conf.xml ]; then \
		cp -u $(DISTDIR)/template/conf/manual-default.xml $(MANUALSDIR)/module/conf.xml; fi
	mkdir -p reports
	cp -u $(BACKEND)/Makefile.reports reports/Makefile;
	cp -u $(BACKEND)/cvsignore.report reports/.cvsignore;
	cp -u $(BACKEND)/cvsignore.conf conf/.cvsignore;
	if [ ! -e conf/DocBook.xml ]; then \
		cp -u $(DISTDIR)/template/conf/DocBook.xml conf; fi
	$(foreach  addon,$(wildcard $(DISTDIR)/template/conf/addon-files.*), \
		if [ ! -e $(notdir $(addon)) ]; then \
			cp -u $(addon) conf; fi; )
	cp -u $(DISTDIR)/template/conf/Makefile conf/Makefile;
# Generate PSGML master documents
ifneq ($(PSGMLTOP),no)
	$(MAKE) -C $(MANUALSDIR)/module/ psgml-tops
endif

# In case I am curerntly adding a new document (adddoc), get modules used in it
ifneq ($(master),)
modules=$(addsuffix .xml,$(call GET,//\*[@role=\"module\"]/@id,$(master)))
endif

# File with variables and functions to be included in all other Makefiles
Makefile.include: $(BACKEND)/Makefile.include.in $(wildcard conf/*.xml) Makefile.variables
	perl -p -e "s,\@ROOTDIR\@,$(ROOTDIR),g;" $(BACKEND)/Makefile.include.in > $@

# this file gathers all project related variables used troughout Makefiles
Makefile.variables: $(wildcard $(ROOTDIR)/conf/*) $(wildcard $(foreach man,$(MANUALS),$(ROOTDIR)/$(MANUALSDIR)/$(man)/master.top.xml)) VERSION
	echo "#### This file is generated automatically, do not edit ####" > $@
# Global vars:
	echo LANGS=$(LANGS) >> $@
	echo ACTIVELANGS=$(ACTIVELANGS) >> $@
	echo ROOTDIR=$(ROOTDIR) >> $@
	echo DISTDIR=$(DISTDIR) >> $@
	echo AUTHORS=$(AUTHORS) >> $@
	echo TYPES=$(TYPES) >> $@
	echo MODULESDIR=$(MODULESDIR) >> $@
	echo MANUALSDIR=$(MANUALSDIR) >> $@
	echo DTDURI=$(DTDURI) >> $@
	echo DTDTYPE=$(DTDTYPE) >> $@
	echo MANUALS=$(MANUALS) >> $@
	echo ACTIVEMANUALS=$(ACTIVEMANUALS) >> $@
	echo IMPORTS=$(IMPORTS) >> $@
	echo OUTPUTS=$(OUTPUTS) >> $@
	echo XSLT='$(XSLT)' >> $@
	echo XSLTD='$(XSLTD)' >> $@
	echo FO2PDF='$(FO2PDF)' >> $@
# default language:
	echo "LANG=$(call GET,//lang,conf/author.xml)" >> $@
# Default author initials
	echo "AUTHOR=$(call GET,//initials,conf/author.xml)" >> $@
# What's the encoding used for current language?
	echo ENCOD=$(call GET,'configuration/repository/languages/lang[text()="$(LANG)"]/@encoding',$(ROOTDIR)/conf/repository.xml) >> $@
	echo 'ENCODING:=$$(if $(ENCOD),$(ENCOD),UTF-8)' >> $@
# each manual's modules
	$(foreach man,$(MANUALS),echo "$(man)-modules=$(sort $(addsuffix .xml,$(shell $(DISTDIR)/bin/confparse.pl '//*[@role="module"]/@id' $(ROOTDIR)/$(MANUALSDIR)/$(man)/master.top.xml 2> /dev/null )))" >> $@; )
# all modules used in all active documents
	echo 'modules:=$$(sort $(foreach man,$(MANUALS),$$($(man)-modules) ))' >> $@
	echo 'allmodules:=$$(foreach lang,$$(LANGS),$$(addprefix $$(ROOTDIR)/$$(MODULESDIR)/$$(lang)/,$$(modules)))' >> $@
# project name and release
	echo proj_name=$(call GET,'configuration/repository/title',$(ROOTDIR)/conf/repository.xml) >> $@
	echo REL=$(REL) >> $@
	echo ALLAUTHORS=$(ALLAUTHORS) >> $@
# Where to publish frontend
	echo frontend-proto=$(call GET,configuration/envvar[@id=\"frontend-proto\"],$(PUBCONF)) >> $@
	echo frontendpath=$(call GET,configuration/envvar[@id=\"frontendpath\"],$(PUBCONF)) >> $@
	echo cgibinpath=$(call GET,configuration/envvar[@id=\"cgibinpath\"],$(PUBCONF)) >> $@
	echo frontendurl=$(call GET,configuration/envvar[@id=\"frontendurl\"],$(PUBCONF)) >> $@
	echo cgibinurl=$(call GET,configuration/envvar[@id=\"cgibinurl\"],$(PUBCONF)) >> $@
# publish conf file
	echo PUBCONF=$(ROOTDIR)/$(PUBCONF) >> $@
	echo AUTHORSCONF=$(ROOTDIR)/$(AUTHORSCONF) >> $@
	echo CONF=$(ROOTDIR)/$(CONF) >> $@
	echo OUTPUTSDIR=$(OUTPUTSDIR) >> $@

test: 
	echo OUTPUTS: $(OUTPUTS)
	echo initials: $(AUTHOR)
	echo langs: $(LANGS)
	echo manualsathlangs: $(MANUALSDIR)
	echo default lang: $(DEF_LANG)
	echo manuals: $(MANUALS)

Makefile: $(DISTDIR)/template/Makefile Makefile.include $(DISTDIR)/VERSION VERSION
	cp -f $< $@
	$(MAKE) configure


# update the CVS repository and imported modules
checkout:
	if [ "$(CVS)" == 'yes' ]; then cvs -z2 update -d -P; fi
ifneq ($(IMPORTS),)
	cd $(MODULESDIR); \
	for i in $(IMPORTS); do \
		cvs -z2 co $$i; \
	done;
endif
ifneq ($(RECONF),no)
	$(MAKE) configure
endif

# Add a new language to conf file, create all resulting directories.
# Copy all entities files from default lang dir to new lang dir.
# Create correponding module templates.
# And finally add all that to CVS repository if available.
LOGCVSCO="New files for $(doc) $(NEWLANG)"
addlang:
ifneq ($(NEWLANG),)
ifeq ($(findstring $(NEWLANG),$(LANGS)),)
	@echo "****** Adding new $(NEWLANG) language in whole repository..."
	$(call PUT,/configuration/repository/languages/lang,$(NEWLANG),$(CONF))
	perl -pi -e "s,</language>,</language>\n    <language lang=\"$(NEWLANG)\"/>," conf/manual-default.xml
	$(MAKE) configure
	$(foreach man,$(MANUALS),cp $(MANUALSDIR)/$(man)/$(DEF_LANG)/*.ent \
		 $(MANUALSDIR)/$(man)/$(NEWLANG); )
ifneq ($(wildcard entities/$(DEF_LANG)/*.ent),)
	cp entities/$(DEF_LANG)/*.ent entities/$(NEWLANG)
endif
ifeq ($(CVS),yes)
	$(call cvsdiradd,$(MODULESDIR)/$(NEWLANG))
endif
# Copy entities from main language
	$(foreach man,$(MANUALS),if ls $(MANUALSDIR)/$(man)/$(DEF_LANG)/*.ent 2>/dev/null; then cp $(MANUALSDIR)/$(man)/$(DEF_LANG)/*.ent $(MANUALSDIR)/$(man)/$(NEWLANG); fi ; )
	if ls entities/$(DEF_LANG)/*.ent 2>/dev/null; then cp entities/$(DEF_LANG)/*.ent entities/$(NEWLANG); fi
# Add a new revisions list in repository.conf so that we can define default authors there
	add_default_authors_list.pl $(CONF) $(NEWLANG) $(NEWAUTHORS)
# generate the templates for that language
	$(foreach man,$(MANUALS),$(MAKE) -C $(MANUALSDIR)/$(man) templates LANG=$(NEWLANG) NOTEMPLATES=yes;)
# we consider that if we add a new language now it is meant to be a translation
	$(foreach man,$(MANUALS),$(MAKE) -C $(MANUALSDIR)/$(man) releases CVSLOG=$(LOGCVSCO) NEWCYCLE=translation NEWIDS=no LANG=$(NEWLANG) NEWREL=$(REL);)
ifeq ($(CVS),yes)
	$(call cvsdiradd,$(foreach dir,entities images $(MANUALSDIR)/module,$(dir)/$(NEWLANG))\
		$(foreach man,$(MANUALS),$(MANUALSDIR)/$(man)/$(NEWLANG)))
	$(foreach com,add commit,cvs -z2 $(com) -m $(LOGCVSCO)\
		$(foreach dir,entities images $(MANUALSDIR)/module $(MODULESDIR),$(dir)/$(NEWLANG)/.cvsignore)\
		$(if $(MANUALS), \
		$(foreach man,$(MANUALS),$(MANUALSDIR)/$(man)/$(NEWLANG)/.cvsignore)\
		$$(ls $(foreach man,$(MANUALS),$(MANUALSDIR)/$(man)/$(NEWLANG)/*.ent)\
		$(foreach man,$(MANUALS),entities/$(NEWLANG)/*.ent)) ); )
	cvs -z2 commit -m "Add language $(NEWLANG)" $(CONF) conf/manual-default.xml
endif
else
	@echo "This language ($(NEWLANG)) is already defined..."
	@echo "Usage: make addlang NEWLANG=xx"
endif
else
	@echo "********  ERROR: What's the New language to add ?"
	@echo "Usage: make addlang NEWLANG=xx"
endif

# Add a new document to conf file, create all resulting directories.
# Take as an argument the master.top.xml file for that document
# Create correponding module templates.
# And finally add all that to CVS repository if available.
adddoc:
ifdef doc
ifeq ($(filter $(doc),$(MANUALS)),)
ifneq ($(wildcard /$(master)),)
	@echo "****** Adding new document $(doc) in repository..."
# check doc is in UTF-8 
	if ! head $(master) | grep '<?xml' | grep 'UTF-8' > /dev/null; then \
		echo "********** ERROR: master must be encoded in UTF-8!"; \
		exit 1; fi
# check doc validity 
	if ! xmllint --noout --valid $(master); then \
		echo "********** ERROR: master not valid!"; \
		exit 1; fi
# insert new doc
	$(call PUT,/configuration/repository/manuals/manual,$(doc),conf/repository.xml)
# reconfigure repository
	$(MAKE) configure PSGMLTOP=no
# Copy master and Make new entities templates
	$(MAKE) -C $(MANUALSDIR)/$(doc) newentities TOPSRC=$(master)
	cp -f conf/manual-default.xml $(MANUALSDIR)/$(doc)/conf.xml
	perl -pi -e "s/ id=\"\"/ id=\"$(doc)\"/" $(MANUALSDIR)/$(doc)/conf.xml
# if scrollkeeper is available compute a seriesid for this new document
	seriesid=$(shell if which scrollkeeper-gen-seriesid > /dev/null 2>&1; then scrollkeeper-gen-seriesid; fi); \
	perl -pi -e "s/omf.seriesid=\"\"/omf.seriesid=\"$$seriesid\"/" $(MANUALSDIR)/$(doc)/conf.xml
# add related files to CVS if available
ifeq ($(CVS),yes)
	$(call cvsdiradd,$(MANUALSDIR) $(MODULESDIR) $(MANUALSDIR)/$(doc) \
		$(foreach lang,$(LANGS),$(MANUALSDIR)/$(doc)/$(lang)) $(foreach lang,$(LANGS),$(MODULESDIR)/$(lang)))
	cvs -z2 add $(MANUALSDIR)/$(doc)/{conf.xml,master.top.xml}
endif
# add revision
	$(MAKE) -C $(MANUALSDIR)/$(doc) master.release NEWREL=$(REL) LOGCVSCO=$(LOGCVSCO)
	perl -pi -e "s/id=\"\"/id=\"$(doc)\"/" $(MANUALSDIR)/$(doc)/conf.xml
# Make modules templates and put them in CVS if available
	$(MAKE) -C $(MANUALSDIR)/$(doc) alltemplates
# Copy add-on files if any
	for list in $(wildcard conf/addon-files.*); do \
		for source in $$($(DISTDIR)/bin/confparse.pl "//addon/source" $$list ); do \
			for target in $$($(DISTDIR)/bin/confparse.pl "//addon[source=\"$$source\"]/target" $$list); do \
				cp -f $$source $(MANUALSDIR)/$(doc)/$$target; \
				if [ "$(CVS)" == "yes" ]; then \
					cvs -z2 add $(MANUALSDIR)/$(doc)/$$target; \
					cvs -z2 commit -m $(LOGCVSCO) $(MANUALSDIR)/$(doc)/$$target; \
				fi; \
			done; \
		done; \
	done
# add related files to CVS if available
ifeq ($(CVS),yes)
	cvs -z2 add \
		$(foreach lang,$(LANGS),$(MANUALSDIR)/$(doc)/$(lang)/.cvsignore)\
		$(foreach lang,$(LANGS),$(MANUALSDIR)/$(doc)/$(lang)/*.ent)\
		$(MANUALSDIR)/$(doc)/{.cvsignore,Makefile.include}
	cvs -z2 commit -m $(LOGCVSCO) $(MANUALSDIR)/$(doc) \
		$(foreach lang,$(LANGS),$(MANUALSDIR)/$(doc)/$(lang)) \
		$(foreach lang,$(LANGS),$(MANUALSDIR)/$(doc)/$(lang)/.cvsignore)\
		$(foreach lang,$(LANGS),$(MANUALSDIR)/$(doc)/$(lang)/*.ent)\
		$(MANUALSDIR)/$(doc)/{.cvsignore,Makefile.include,conf.xml,master.top.xml}\
		$(CONF)
endif
else
	@echo "I can't find the master document (/$(doc))..."
	@echo "Usage: make adddoc doc=My_New_Manual master=/path/to/master.top.xml"
endif
else
	@echo "This Document ($(doc)) is already defined..."
	@echo "Usage: make adddoc doc=My_New_Manual master=/path/to/master.top.xml"
endif	
else
	@echo "What's the name of the document?"
	@echo "Usage: make adddoc doc=My_New_Manual master=/path/to/master.top.xml"
endif	

# Start up a new release cycle for whole project (manuals and modules)
ifeq ($(NEWREL),)
NEWREL=$(shell a=$(REL); b=$${a\#\#*.}; c=$${a%%$$b}; echo $$c$$[$$b+1])
endif	
release: run.once
ifeq ($(REL),$(NEWREL))
	@echo "***** Current release is already $(REL) !"
else
ifeq ($(CVS),yes)
# Tag current files with old release number
# Create a branch with old release in case one need it
	export CVSREL=$$(echo $(REL) | sed -e "s,\.,_,g"); \
	cvs tag -c B_REL-$$CVSREL; \
	cvs tag -b B_BRANCH-$$CVSREL
endif
	$(foreach man,$(MANUALS),$(MAKE) -C $(MANUALSDIR)/$(man) release NEWREL=$(NEWREL) REL=$(NEWREL) CVSLOG="New release $(NEWREL)" LOGCVSCO="New release $(NEWREL)"; )
	echo -n $(NEWREL) > VERSION
ifeq ($(CVS),yes)
	cvs -z2 commit -m "New release $(NEWREL)" VERSION
endif
endif

# add the repository skeleton to CVS
cvsinit: cleaner
ifeq ($(CVS),yes)
	echo "Hmmmm, looks like this is already a CVS repository..."
else
ifeq ($(CVSROOT),)
	echo "***********************************************"
	echo "You need to set CVSROOT in order to use CVS...."
	echo "***********************************************"
else
ifeq ($(PROJECT),)
	echo "You need to provide the project's name!"
	echo "Usage: make cvsinit PROJECT=MyNewProject"
else
	cvs -z2 import -m "New Borges Repository" $(PROJECT) Borges Start
endif
endif
endif

# reconfigure author parameters
reconfigure:
	@echo "Backing up conf/author.xml to conf/author.old.xml"
	mv conf/author.xml conf/author.old.xml
	$(MAKE) -C conf author.xml

# time in seconds a compilation should not run more
maxrun=18000
.PHONY: run.once publish
# Use this file to ensure automatic compilations do not run twice at a time
# Compil should not run more than 5 hours...?
run.once:
	if [ -e run.lock ]; then  \
		touch  -d '$(maxrun) seconds ago' run.lock.test; \
		if [ run.lock -nt run.lock.test ]; then \
			echo "******* Repository in use *********"; \
			rm -f run.lock.test; \
			exit 1; \
		else \
			$(MAKE) clean; \
			rm -f run.lock.tes run.lock; \
		fi; \
	else touch run.lock; \
	fi

PUBTYPE=report output
# publish reports and/or outputs according to conf/publish.xml
publish: run.once
	$(MAKE) checkout
ifneq ($(filter report,$(PUBTYPE)),)
	$(MAKE) -C reports all
	$(call transfer,$(reports-proto),reports,$(reportspath))
endif
ifneq ($(filter output,$(PUBTYPE)),)
	$(MAKE) all
	$(call transfer,$(outputs-proto),$(OUTPUTSDIR),$(outputspath))
endif
ifneq ($(filter frontend,$(PUBTYPE)),)
	$(MAKE) -C reports publish-frontend
endif
ifneq ($(filter xmlvalidity,$(PUBTYPE)),)
	$(MAKE) -C reports xmlvalidity
	$(call transfer,$(reports-proto),reports/xmlvalidity,$(reportspath)/reports)
endif
	rm -f run.lock

# create an archive with everything but specific CVS files
# WARNING: cleans everything!
archive: superclean
ifeq ($(POOL),)
	@echo "****** Backing up the whole repository in archive.tar.bz2 ..."
	rm -f archive.tar.bz2
	tar jcvf archive.tar.bz2 --exclude CVS --exclude .cvsignore *
else
	@echo "****** Backing up pool $(POOL) in $(POOL)-$(REL).tar.bz2 ..."
	rm -f $(POOL).tar.bz2
	configure
	mkdir -p Borges-tmp/$(POOL)-$(REL)/$(MANUALSDIR)
	mkdir -p Borges-tmp/$(POOL)-$(REL)/images
	langs=
	for doc in $$(confparse.pl "//pool[@id=\"$(POOL)\"]/document/@id" $(CONF)); do \
		man=$$(echo $$doc | sed -e "s,/.*,,"); \
		sub=$$(echo $$doc | sed -e "s,.*/,,"); \
		cp -a $(MANUALSDIR)/$$man Borges-tmp/$(POOL)-$(REL)/$(MANUALSDIR); \
		for lang in $$(confparse.pl "//pool[@id=\"$(POOL)\"]/document[@id=\"$$doc\"]//@lang" $(CONF)); do \
			$(MAKE) -C $(MANUALSDIR)/$$man $$sub.img.dependencies LANG=$$lang; \
			cat $(MANUALSDIR)/$$man/$$sub.img.dependencies | sed -e "s,.*/,,; s,\..*,," >> Borges-tmp/img.dependencies.$$lang.tmp; \
		done; \
		$(MAKE) -C $(MANUALSDIR)/$$man $$sub.mod.dependencies; \
		cat $(MANUALSDIR)/$$man/$$sub.mod.dependencies >> Borges-tmp/mod.dependencies.tmp; \
	done
	sort -u Borges-tmp/mod.dependencies.tmp > Borges-tmp/mod.dependencies
	cp -a conf drivers entities configure VERSION Borges-tmp/$(POOL)-$(REL)
	cp -a images/missing.jpg VERSION Borges-tmp/$(POOL)-$(REL)/images
# State in configuratio that this is only a limited repository
	perl -pi -e "s/<repository>/<repository pool=\"$(POOL)\">/" Borges-tmp/$(POOL)-$(REL)/$(CONF)
	for lang in $$(confparse.pl "//pool[@id=\"$(POOL)\"]//@lang" $(CONF)); do \
		mkdir -p Borges-tmp/$(POOL)-$(REL)/modules/$$lang; \
		mkdir -p Borges-tmp/$(POOL)-$(REL)/images/$$lang; \
		for m in $$(cat Borges-tmp/mod.dependencies); do \
			cp $(MODULESDIR)/$$lang/$$m Borges-tmp/$(POOL)-$(REL)/modules/$$lang; \
			cp $(MODULESDIR)/$$lang/.$$(basename $$m .xml).revhistory Borges-tmp/$(POOL)-$(REL)/modules/$$lang; \
		done; \
		sort -u Borges-tmp/img.dependencies.$$lang.tmp > Borges-tmp/img.dependencies.$$lang; \
		for i in $$(cat Borges-tmp/img.dependencies.$$lang); do \
			if ls images/$$lang/$$i.* > /dev/null 2>&1; then cp images/$$lang/$$i.* Borges-tmp/$(POOL)-$(REL)/images/$$lang; fi; \
			if [ -e images/$$lang/$$i.fig ]; then \
				cp images/$$lang/$$i.fig ~/tmp/$$i.fig; \
				for include in $$(cd ~/tmp; fig2dev -L eps $$i.fig /dev/null 2<&1 | grep "No such picture file" | sed -e "s/.*: //"); do \
					cp images/$$lang/$$include Borges-tmp/$(POOL)-$(REL)/images/$$lang; \
				done; \
				rm -f ~/tmp/$$i.fig; \
			fi; \
		done; \
	done
# The .fig script part above and below tries to guess what images are used in xfig diagrams...
	for i in $$(cat Borges-tmp/img.dependencies.* | sort -u); do \
		if ls images/$$i.* > /dev/null 2>&1; then cp images/$$i.* Borges-tmp/$(POOL)-$(REL)/images/$$lang; fi; \
		if [ -e images/$$lang/$$i.fig ]; then \
			cp images/$$lang/$$i.fig ~/tmp/$$i.fig; \
			for include in $$(cd ~/tmp; fig2dev -L eps $$i.fig /dev/null 2<&1 | grep "No such picture file" | sed -e "s/.*: //"); do \
				cp images/$$lang/$$include Borges-tmp/$(POOL)-$(REL)/images/$$lang; \
			done; \
			rm -f ~/tmp/$$i.fig; \
		fi; \
	done
# Clean archive before packing
	cd Borges-tmp/$(POOL)-$(REL); ./configure
	$(MAKE) -C Borges-tmp/$(POOL)-$(REL) superclean
	@echo "******* Generating archive for \"$(POOL)\" pool in $(POOL)-$(REL).tar.bz2"
	cd Borges-tmp ; \
	tar jcf ../$(POOL)-$(REL).tar.bz2 --exclude CVS --exclude .cvsignore $(POOL)-$(REL)
	rm -rf Borges-tmp
endif

# make clean in all subdirs
clean: configure
	@echo "****** Cleaning repository..."
	for i in $(SUBS); do $(MAKE) -C $$i clean; done
	for i in `cat .cvsignore | grep -v Makefile`; do rm -rf $$i; done
	$(MAKE) configure

# removes also compiled images
cleaner: clean
	rm -rf $(MANUALSDIR)/images
	$(MAKE) configure

# remove all files ignored by CVS and dirs too
superclean: configure
	@echo "****** Cleaning repository in depth..."
	$(foreach d,$(SUBS),if [ -e $(d) ]; then $(MAKE) -C $(d) superclean; fi; )
	for i in $$(find . -name .cvsignore | sed "s,\(.*\)/.*,\1,"); do \
		if [ -e $(ROOTDIR)/$$i ]; then \
			cd $(ROOTDIR)/$$i; \
			rm -rf `cat .cvsignore` *~; \
		fi; \
	done


# Used for debugging purposes
debug:
	$(run)


# Local variables:
# mode: makefile
# End:
