DESIGNSTANDDIR := /usr/share/yandex/maps/wiki/designstand/layers
DESIGNSTANDPOD := $(shell sky list Y@maps-core-nmaps-designstand-unstable | head -1)

LAYERS := mpskl
MAP_FILES := $(foreach layer,$(LAYERS),$(layer)/map.xml)

test:: validate

validate: $(MAP_FILES)
	for file in $^; do xmllint $$file 1>/dev/null || exit; done

define UPDATE
update_$(1):
	rsync -Lq $$(DESIGNSTANDPOD):$$(DESIGNSTANDDIR)/$(1)/map.xml $(1)/
	rsync -Lqr $$(DESIGNSTANDPOD):$$(DESIGNSTANDDIR)/$(1)/resources $(1)/
	@echo
	@echo Hints to next steps:
	@echo 1. Please make sure you are on clean trunk and add files to VCS
	@echo SVN: svn add $(1)/resources --force
	@echo Arc: arc checkout -b NMAPS-XXXX-new-design
	@echo "     arc add -A $(1)/resources $(1)/map.xml"
	@echo
	@echo 2. Create PR
	@echo SVN: svn ci -m '"New design NMAPS-XXXX SKIP_REVIEW"'
	@echo Arc: arc commit -m '"New design NMAPS-XXXX"'
	@echo "     arc pr c --push --no-edit"
endef

update_style2_map:
	rsync -Lq $(DESIGNSTANDPOD):$(DESIGNSTANDDIR)/v2/design.*.json ./v2/
	rsync -Lq $(DESIGNSTANDPOD):$(DESIGNSTANDDIR)/v2/icons.tar ./v2/
	rsync -Lq $(DESIGNSTANDPOD):$(DESIGNSTANDDIR)/v2/revision.json ./v2/
	@echo
	@echo Hints to next steps:
	@echo 1. Please make sure you are on clean trunk and add files to VCS
	@echo SVN: svn add design.json icons.tar revision.json --force
	@echo Arc: arc checkout -b NMAPS-XXXX-new-design
	@echo "     arc add -A v2"
	@echo
	@echo 2. Create PR
	@echo SVN: svn ci -m '"New style2 design NMAPS-XXXX SKIP_REVIEW"'
	@echo Arc: arc commit -m '"New style2 design NMAPS-XXXX"'
	@echo "     arc pr c --push --no-edit"

.PHONY: maxid
maxid:
	@$(foreach layer, $(LAYERS), echo $(layer) `grep -o -P '(?<=id=").\d+(?=")' $(layer)/map.xml |sort -n -r | head -1`)

$(foreach layer, $(LAYERS), $(eval $(call UPDATE,$(layer))))

