DECLS=decls

clean:
	@-rm _type/*.convert2xml.xsl _mode/eval_mode_*_type_*.xsl
	$(MAKE) all

LEVELS=$(wildcard _type/*.blocks-levels)
MODE=$(wildcard _mode/eval_mode_*.xsl)

all: $(DECLS) $(patsubst %.blocks-levels,%.convert2xml.xsl,$(LEVELS)) \
	$(MODE)

decls:
	$(MAKE) -C ../.. decls

no-decls:

%.raw.xml:
	@echo '<?xml version="1.0" encoding="utf-8"?>' > $@ ; \
	echo '<lego:page xmlns:lego="https://lego.yandex-team.ru" name="eval">' >> $@ ; \
	echo '<global>' >> $@; \
	echo '<id>lego</id>' >> $@; \
	$(if $(findstring mobile,$*),echo '<mobile/>' >> $@;) \
	$(if $(findstring intranet,$*),echo '<intranet/>' >> $@;) \
	$(if $(findstring mobile,$*),echo '<css-global>noreset</css-global>' >> $@;,echo '<css-global>simple</css-global>' >> $@;) \
	echo '</global>' >> $@; \
	echo '<lego:declaration>' >> $@ ; \
	echo '<lego:i-bem><lego:dom/></lego:i-bem>' >> $@ ; \
	echo '<lego:i-domain-lower/>' >> $@ ; \
	for l in `tr ':' ' ' < $(@:.raw.xml=.blocks-levels)` ; do \
		for i in `ls -X _type/$$l/*/*.decl.xml` ; do \
			xsltproc decl2lego.xsl $$i >> $@ ; \
		done ; \
	done ; \
	echo '</lego:declaration>' >> $@ ; \
	echo '</lego:page>' >> $@ ; \
	../prettyxml $@
	@cd _type && ../../rebuild --paths `cat $(patsubst _type/%.raw.xml,%.blocks-levels,$@)` $(subst _type/,,$@)

%.convert2xml.xsl: %.raw.xml
	@echo $@
	@cd _type && \
	perl -I`pwd`/../.. `pwd`/../../xml2paths --input $(subst _type/,,$<) \
		--paths `cat $(patsubst _type/%.raw.xml,%.blocks-levels,$<)` --mode convert2xml

$(MODE)::
	@echo $@
	@for t in $(patsubst _type/eval%.blocks-levels,%,$(LEVELS)) ; do \
		sed "s+<!-- {{import}} -->+<xsl:import href=\"../_type/eval$$t.xsl\"/>+" $@ > $(patsubst %.xsl,%$$t.xsl,$@); \
	done ; \

.PHONY: all decls no-decls
