DESTDIR=
DESTPATH = /usr/share/doc
LANGUAGES = `find . -name "rosegarden-manual.*.po" | sed -e "s/.\/rosegarden-manual.//" -e "s/.po//"`

all: build

update:
	./get_rosegarden-manual
	./update_translations

status:
	@for f in *.po ; do \
		echo -n "$$f: "; \
		msgfmt --statistics $$f -o /dev/null ; \
	done

build:
	# create localized xml files
	for i in rosegarden-manual.*.po ; do { \
	        po4a-translate -o tags="<listitem> <emphasis>" -M UTF-8 -k 5 -f docbook -m rosegarden-manual.xml -p $$i -l $${i%.po}.xml ; \
	} done

	# Ignore errors because the docbook xml is broken
	#
	# build the english html version
	-openjade -t sgml -i html -E 1000 -d /usr/share/docbook-utils/docbook-utils.dsl\#html -V paper-type=A4 -V nochunks /usr/share/sgml/declaration/xml.dcl rosegarden-manual.xml > rosegarden-manual.en.html
	# Ignore errors because the docbook xml is broken
	#
	# build the other html versions
	-for f in $(LANGUAGES) ; do \
		openjade -t sgml -i html -E 1000 -d /usr/share/docbook-utils/docbook-utils.dsl\#html -V paper-type=A4 -V nochunks /usr/share/sgml/declaration/xml.dcl rosegarden-manual.$$f.xml > rosegarden-manual.$$f.html ; \
	done
	# add utf8 content-type header to html versions
	for f in rosegarden-manual.*.html ; do \
		sed -i "s%><TITLE%><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /><TITLE%" $$f ; \
	done

install: build
	# en needs to be first
	for f in en $(LANGUAGES) ; do \
		export LANGPATH=$(DESTDIR)/debian-edu-doc-$$f/$(DESTPATH)/debian-edu-doc-$$f ; \
		mkdir -p $$LANGPATH ; \
		if [ -e rosegarden-manual.$$f.html ] ; then \
			cp rosegarden-manual.$$f.html $$LANGPATH/rosegarden-manual.html ; \
			if [ "$$f" = "en" ] ; then \
			  mkdir $$LANGPATH/images/ ; \
			  find images -maxdepth 1 -type f -exec cp \{\} $$LANGPATH/images/ \; ; \
			  dblatex -T db2latex -p rosegarden-manual.xsl -o $$LANGPATH/rosegarden-manual.pdf rosegarden-manual.xml ; \
			else \
			  if [ -d images/$$f ] ; then \
			  	mkdir $$LANGPATH/images ; \
			  	cd images ; \
			  	for i in *.* ; do \
					if [ -e $$f/$$i ] ; then \
						cp -v $$f/$$i $$LANGPATH/images/ ; \
					else \
						ln -sf ../../debian-edu-doc-en/images/$$i $$LANGPATH/images/$$i ; \
					fi ; done ; \
			  	cd .. ; \
			  else \
			    ln -sf ../debian-edu-doc-en/images $$LANGPATH/images ; \
			  fi ; \
			  mv images images_tmp_away ; dblatex -I $$LANGPATH/ -I $(DESTDIR)/debian-edu-doc-en/$(DESTPATH)/debian-edu-doc-en/ -T db2latex -p rosegarden-manual.xsl -o $$LANGPATH/rosegarden-manual.pdf rosegarden-manual.$$f.xml ; mv images_tmp_away images ; \
			fi ; \
		fi ; \
	done 		

clean:
	rm -f *.pdf
	rm -f *.html
	rm -f rosegarden-manual.*.xml

dist-clean:	clean
