## ----------------------------------------------------------------------
## Makefile : makefile for docbook-xml
## ----------------------------------------------------------------------

## ----------------------------------------------------------------------
## Document definitions
doc_lang	:= en
# doc_lang	:= en fr
doc_name	:= debian-blends
doc_xml	:= $(shell for l in $(doc_lang); do echo $(doc_name).$$l.xml; done)
doc_xml_source := $(shell find $$l -name "*.xml")
#po_files := $(shell ls *.po)
#mo_files := $(patsubst %.po, %/LC_MESSAGES/blends.mo, $(po_files))
translated := $(shell for l in $$(echo $(doc_lang) | sed 's/en//'); do for f in en/*.xml; do echo $$l/$$(basename $$f); done; done)

sources := $(shell find . -name "*.xml")
doc_pdf	:= $(shell for l in $(doc_lang); do echo $$l/pdf/$(doc_name).$$l.pdf; done)
doc_ps	:= $(shell for l in $(doc_lang); do echo $$l/ps/$(doc_name).$$l.ps; done)
doc_dvi	:= $(shell for l in $(doc_lang); do echo $$l/dvi/$(doc_name).$$l.dvi; done)

doc_txt	:= $(shell for l in $(doc_lang); do echo $$l/txt/$(doc_name).$$l.txt; done)
#this needed cause xmlto txt does not include section numbering by default
doc_txt_params := --stringparam section.autolabel=1 --stringparam section.label.includes.component.label=1

doc_html := $(shell for l in $(doc_lang); do echo $$l/html/index.html; done)
pkg      := blends-doc

doc_html_en := en/html
doc_pdf_en  := en/pdf/debian-blends.en.pdf
doc_html_fr := fr/html
doc_pdf_fr  := fr/pdf/debian-blends.en.pdf

## ----------------------------------------------------------------------
## General definitions
LN		:= /bin/ln -sf
RMR		:= /bin/rm -fr
LOCALE          := unset LC_ALL; unset LANG; unset LANGUAGE; 

## ----------------------------------------------------------------------
# this can and will be overridden by a higher level makefile
PUBLISHHOST ?= dillon.debian.org
# PUBLISHDIR := alioth.debian.org:/srv/alioth.debian.org/chroot/home/groups/blends/htdocs/blends
PUBLISHDIR := $(PUBLISHHOST):/srv/blends.debian.org/www/blends

# There is no difference between letter and a4, but a2 for instance works
PAPERSIZE  := a4

## ----------------------------------------------------------------------
## Targets
all:		html

validate: $(doc_xml)
	for f in $(doc_xml); do \
	  xmllint --valid --noout $$f; \
	done

# update_po: $(po_files)

blends.pot: $(doc_xml_source)
	../devtools/blend-doc-makepot en > $@

$(po_files): blends.pot
	@for l in $(doc_lang); do \
	  [ $$l != "en" ] && echo -n "Updating $$l.po " && \
	  mkdir -p $$l/LC_MESSAGES && \
	  msgmerge -U $$l.po blends.pot; \
	done

update_mo: $(mo_files)

$(mo_files): $(po_files)
	@for l in $$(echo $(doc_lang)| sed 's/en//'); do \
	  echo "Updating $$l/LC_MESSAGES/blends.mo" && \
	  msgfmt -o $$l/LC_MESSAGES/blends.mo $$l.po; \
	done

translate: $(mo_files)
	@for l in $(doc_lang); do \
	  [ $$l != "en" ] && ../devtools/blend-doc-translate $$l; \
	done

html: $(doc_html)

$(doc_html) $(doc_html_en): $(doc_xml) $(sources)
	for l in $(doc_lang); do \
	  f=$(doc_name).$$l.xml; d=$$l/html; \
	  mkdir -p $$d; \
	  $(LOCALE) xmlto --skip-validation -o $$d -x $(doc_name).xsl html $$f; \
	done

txt $(doc_txt): $(doc_xml)
	for l in $(doc_lang); do \
	  f=$(doc_name).$$l.xml; d=$$l/txt; \
	  mkdir -p $$d; \
	  $(LOCALE) xmlto --skip-validation $(doc_txt_params) -o $$d txt $$f; \
	done

ps $(doc_ps): $(doc_xml)
	for l in $(doc_lang); do \
	  f=$(doc_name).$$l.xml; d=$$l/ps; \
	  mkdir -p $$d; \
	  $(LOCALE) xmlto -o $$d --with-dblatex ps $$f; \
	done


pdf $(doc_pdf): $(doc_xml)
	for l in $(doc_lang); do \
	  f=$(doc_name).$$l.xml; d=$$l/pdf; \
	  mkdir -p $$d; \
	  $(LOCALE) xmlto --skip-validation -o $$d --with-dblatex pdf $$f; \
	done


dvi $(doc_dvi): $(doc_xml)
	for l in $(doc_lang); do \
	  f=$(doc_name).$$l.xml; d=$$l/dvi; \
	  mkdir -p $$d; \
	  $(LOCALE) xmlto -o $$d --with-dblatex dvi $$f; \
	done


publish: $(doc_html_en) $(doc_pdf_en)
	rsync -azult --delete $(doc_html_en)/*.html $(PUBLISHDIR)
	rsync -azult $(doc_pdf_en) $(PUBLISHDIR)
	# sync to mirrors
	ssh $(PUBLISHHOST) "/usr/local/bin/static-update-component blends.debian.org"

clean:
	for l in $(doc_lang); do \
	  for d in html pdf txt ps dvi info; do \
	    $(RMR) $$l/$$d; \
	  done; \
	done
	find . -name "*~" -o -name "*.mo" -exec $(RMR) {} \;
	rm -f blends.pot

distclean:
		make clean

.PHONY: all publish clean distclean validate # translateu update_mo update_po
