WMLOPT  = \
          -I $(WMLBASE)/include \
          -D DOCROOT=$(WMLBASE) \
          -D IMGROOT=$(WMLBASE)/images \
          -D TORSVNSTABLE=$(TORSVNSTABLE) \
          -D TORSVNHEAD=$(TORSVNHEAD)


LANGS=de en es it fi fr ja nl no pl pt ru se zh-cn
WMLFILES=$(wildcard $(patsubst %, %/*.wml, $(LANGS)))
WMIFILES=$(wildcard $(patsubst %, %/*.wmi, $(LANGS)) $(WMLBASE)/include/*.wmi )

HTMLFILES = $(shell perl -le 's,  (.*)/(.*).wml  ,        $$2.html.$$1    ,x, print $$_ for @ARGV' $(WMLFILES))
DEPFILES  = $(shell perl -le 's,  (.*)/(.*).wml  ,  .deps/$$2.html.$$1.d  ,x, print $$_ for @ARGV' $(WMLFILES))


all: $(HTMLFILES)

%.html.en: en/%.wml
	lang=`dirname $<` && wml $(WMLOPT) -I $$lang -I $(WMLBASE)/$$lang -D LANG=$$lang $< -o $@

%.html.es: es/%.wml en/%.wml
	lang=`dirname $<` && wml $(WMLOPT) -I $$lang -I $(WMLBASE)/$$lang -D LANG=$$lang $< -o $@

%.html.de: de/%.wml en/%.wml
	lang=`dirname $<` && wml $(WMLOPT) -I $$lang -I $(WMLBASE)/$$lang -D LANG=$$lang $< -o $@

%.html.it: it/%.wml en/%.wml
	lang=`dirname $<` && wml $(WMLOPT) -I $$lang -I $(WMLBASE)/$$lang -D LANG=$$lang $< -o $@

%.html.fi: fi/%.wml en/%.wml
	lang=`dirname $<` && wml $(WMLOPT) -I $$lang -I $(WMLBASE)/$$lang -D LANG=$$lang $< -o $@

%.html.fr: fr/%.wml en/%.wml
	lang=`dirname $<` && wml $(WMLOPT) -I $$lang -I $(WMLBASE)/$$lang -D LANG=$$lang $< -o $@

%.html.ja: ja/%.wml en/%.wml
	lang=`dirname $<` && wml $(WMLOPT) -I $$lang -I $(WMLBASE)/$$lang -D LANG=$$lang $< -o $@

%.html.nl: nl/%.wml en/%.wml
	lang=`dirname $<` && wml $(WMLOPT) -I $$lang -I $(WMLBASE)/$$lang -D LANG=$$lang $< -o $@

%.html.no: no/%.wml en/%.wml
	lang=`dirname $<` && wml $(WMLOPT) -I $$lang -I $(WMLBASE)/$$lang -D LANG=$$lang $< -o $@

%.html.pl: pl/%.wml en/%.wml
	lang=`dirname $<` && wml $(WMLOPT) -I $$lang -I $(WMLBASE)/$$lang -D LANG=$$lang $< -o $@

%.html.pt: pt/%.wml en/%.wml
	lang=`dirname $<` && wml $(WMLOPT) -I $$lang -I $(WMLBASE)/$$lang -D LANG=$$lang $< -o $@

%.html.ru: ru/%.wml en/%.wml
	lang=`dirname $<` && wml $(WMLOPT) -I $$lang -I $(WMLBASE)/$$lang -D LANG=$$lang $< -o $@

%.html.se: se/%.wml en/%.wml
	lang=`dirname $<` && wml $(WMLOPT) -I $$lang -I $(WMLBASE)/$$lang -D LANG=$$lang $< -o $@

%.html.zh-cn: zh-cn/%.wml en/%.wml
	lang=`dirname $<` && wml $(WMLOPT) -I $$lang -I $(WMLBASE)/$$lang -D LANG=$$lang $< -o $@

.deps/%.html.en.d: en/%.wml .deps/.stamp
	tmpfile=`tempfile` \
	lang=`dirname $<` && \
	OUT=`echo $@ | sed -e 's,\.deps/\(.*\)\.d$$,\1,'` && \
	wml $(WMLOPT) -I $$lang -I $(WMLBASE)/$$lang -D LANG=$$lang $< -o $$OUT --depend | tee $$tmpfile > $@ && \
	sed -e s',\(^[^ ]*\):,.deps/\1.d:,' < $$tmpfile >> $@ && \
	rm -f $$tmpfile
.deps/%.html.es.d: es/%.wml .deps/.stamp
	tmpfile=`tempfile` \
	lang=`dirname $<` && \
	OUT=`echo $@ | sed -e 's,\.deps/\(.*\)\.d$$,\1,'` && \
	wml $(WMLOPT) -I $$lang -I $(WMLBASE)/$$lang -D LANG=$$lang $< -o $$OUT --depend | tee $$tmpfile > $@ && \
	sed -e s',\(^[^ ]*\):,.deps/\1.d:,' < $$tmpfile >> $@ && \
	rm -f $$tmpfile
.deps/%.html.de.d: de/%.wml .deps/.stamp
	tmpfile=`tempfile` \
	lang=`dirname $<` && \
	OUT=`echo $@ | sed -e 's,\.deps/\(.*\)\.d$$,\1,'` && \
	wml $(WMLOPT) -I $$lang -I $(WMLBASE)/$$lang -D LANG=$$lang $< -o $$OUT --depend | tee $$tmpfile > $@ && \
	sed -e s',\(^[^ ]*\):,.deps/\1.d:,' < $$tmpfile >> $@ && \
	rm -f $$tmpfile
.deps/%.html.it.d: it/%.wml .deps/.stamp
	tmpfile=`tempfile` \
	lang=`dirname $<` && \
	OUT=`echo $@ | sed -e 's,\.deps/\(.*\)\.d$$,\1,'` && \
	wml $(WMLOPT) -I $$lang -I $(WMLBASE)/$$lang -D LANG=$$lang $< -o $$OUT --depend | tee $$tmpfile > $@ && \
	sed -e s',\(^[^ ]*\):,.deps/\1.d:,' < $$tmpfile >> $@ && \
	rm -f $$tmpfile
.deps/%.html.fi.d: fi/%.wml .deps/.stamp
	tmpfile=`tempfile` \
	lang=`dirname $<` && \
	OUT=`echo $@ | sed -e 's,\.deps/\(.*\)\.d$$,\1,'` && \
	wml $(WMLOPT) -I $$lang -I $(WMLBASE)/$$lang -D LANG=$$lang $< -o $$OUT --depend | tee $$tmpfile > $@ && \
	sed -e s',\(^[^ ]*\):,.deps/\1.d:,' < $$tmpfile >> $@ && \
	rm -f $$tmpfile
.deps/%.html.fr.d: fr/%.wml .deps/.stamp
	tmpfile=`tempfile` \
	lang=`dirname $<` && \
	OUT=`echo $@ | sed -e 's,\.deps/\(.*\)\.d$$,\1,'` && \
	wml $(WMLOPT) -I $$lang -I $(WMLBASE)/$$lang -D LANG=$$lang $< -o $$OUT --depend | tee $$tmpfile > $@ && \
	sed -e s',\(^[^ ]*\):,.deps/\1.d:,' < $$tmpfile >> $@ && \
	rm -f $$tmpfile
.deps/%.html.ja.d: ja/%.wml .deps/.stamp
	tmpfile=`tempfile` \
	lang=`dirname $<` && \
	OUT=`echo $@ | sed -e 's,\.deps/\(.*\)\.d$$,\1,'` && \
	wml $(WMLOPT) -I $$lang -I $(WMLBASE)/$$lang -D LANG=$$lang $< -o $$OUT --depend | tee $$tmpfile > $@ && \
	sed -e s',\(^[^ ]*\):,.deps/\1.d:,' < $$tmpfile >> $@ && \
	rm -f $$tmpfile
.deps/%.html.nl.d: nl/%.wml .deps/.stamp
	tmpfile=`tempfile` \
	lang=`dirname $<` && \
	OUT=`echo $@ | sed -e 's,\.deps/\(.*\)\.d$$,\1,'` && \
	wml $(WMLOPT) -I $$lang -I $(WMLBASE)/$$lang -D LANG=$$lang $< -o $$OUT --depend | tee $$tmpfile > $@ && \
	sed -e s',\(^[^ ]*\):,.deps/\1.d:,' < $$tmpfile >> $@ && \
	rm -f $$tmpfile
.deps/%.html.no.d: no/%.wml .deps/.stamp
	tmpfile=`tempfile` \
	lang=`dirname $<` && \
	OUT=`echo $@ | sed -e 's,\.deps/\(.*\)\.d$$,\1,'` && \
	wml $(WMLOPT) -I $$lang -I $(WMLBASE)/$$lang -D LANG=$$lang $< -o $$OUT --depend | tee $$tmpfile > $@ && \
	sed -e s',\(^[^ ]*\):,.deps/\1.d:,' < $$tmpfile >> $@ && \
	rm -f $$tmpfile
.deps/%.html.pl.d: pl/%.wml .deps/.stamp
	tmpfile=`tempfile` \
	lang=`dirname $<` && \
	OUT=`echo $@ | sed -e 's,\.deps/\(.*\)\.d$$,\1,'` && \
	wml $(WMLOPT) -I $$lang -I $(WMLBASE)/$$lang -D LANG=$$lang $< -o $$OUT --depend | tee $$tmpfile > $@ && \
	sed -e s',\(^[^ ]*\):,.deps/\1.d:,' < $$tmpfile >> $@ && \
	rm -f $$tmpfile
.deps/%.html.pt.d: pt/%.wml .deps/.stamp
	tmpfile=`tempfile` \
	lang=`dirname $<` && \
	OUT=`echo $@ | sed -e 's,\.deps/\(.*\)\.d$$,\1,'` && \
	wml $(WMLOPT) -I $$lang -I $(WMLBASE)/$$lang -D LANG=$$lang $< -o $$OUT --depend | tee $$tmpfile > $@ && \
	sed -e s',\(^[^ ]*\):,.deps/\1.d:,' < $$tmpfile >> $@ && \
	rm -f $$tmpfile
.deps/%.html.ru.d: ru/%.wml .deps/.stamp
	tmpfile=`tempfile` \
	lang=`dirname $<` && \
	OUT=`echo $@ | sed -e 's,\.deps/\(.*\)\.d$$,\1,'` && \
	wml $(WMLOPT) -I $$lang -I $(WMLBASE)/$$lang -D LANG=$$lang $< -o $$OUT --depend | tee $$tmpfile > $@ && \
	sed -e s',\(^[^ ]*\):,.deps/\1.d:,' < $$tmpfile >> $@ && \
	rm -f $$tmpfile
.deps/%.html.se.d: se/%.wml .deps/.stamp
	tmpfile=`tempfile` \
	lang=`dirname $<` && \
	OUT=`echo $@ | sed -e 's,\.deps/\(.*\)\.d$$,\1,'` && \
	wml $(WMLOPT) -I $$lang -I $(WMLBASE)/$$lang -D LANG=$$lang $< -o $$OUT --depend | tee $$tmpfile > $@ && \
	sed -e s',\(^[^ ]*\):,.deps/\1.d:,' < $$tmpfile >> $@ && \
	rm -f $$tmpfile
.deps/%.html.zh-cn.d: zh-cn/%.wml .deps/.stamp
	tmpfile=`tempfile` \
	lang=`dirname $<` && \
	OUT=`echo $@ | sed -e 's,\.deps/\(.*\)\.d$$,\1,'` && \
	wml $(WMLOPT) -I $$lang -I $(WMLBASE)/$$lang -D LANG=$$lang $< -o $$OUT --depend | tee $$tmpfile > $@ && \
	sed -e s',\(^[^ ]*\):,.deps/\1.d:,' < $$tmpfile >> $@ && \
	rm -f $$tmpfile
.deps/.stamp:
	[ -d .deps ] || mkdir .deps
	touch "$@"

dep: $(DEPFILES)
clean:
	rm -f $(HTMLFILES) $(DEPFILES)
	for sub in $(SUBDIRS); do \
		$(MAKE) -C "$$sub" WMLBASE=../$(WMLBASE) clean; \
	done

include $(DEPFILES)