PAGES := index.html 404.html javascript.html contact.html downloads.html
SHARED_FILES := stallman.webp stallman_2024.webp favicon.ico \
				poster_cz.webp poster_en.webp poster_cz.jpg poster_en.jpg \
				word_cloud_cz.png word_cloud_en.png background.jpg \
				moon.svg sun.svg narrow_light.svg narrow_dark.svg wide_light.svg wide_dark.svg lang_cz.svg lang_en.svg \
				styles.css icons.css \
				countdown.js theme.js lang.js
STATIC_ASSETS := jetbrains_mono.woff2 poster_cz.png poster_en.png
SEARCH_REPLACE := lib/search_and_replace/target/release/search_and_replace
COMPONENTS := head.html header.html nav.html footer.html

.PHONY: build
build: $(PAGES:%=dst/cz/%) $(PAGES:%=dst/en/%) $(SHARED_FILES:%=dst/%) $(STATIC_ASSETS:%=dst/%)

.PHONY: pack
pack: dst/libre_liberec.tar.gz

.PHONY: clean
clean:
	rm -rf dst

$(SEARCH_REPLACE): $(shell find lib/search_and_replace/src)
	cd lib/search_and_replace && cargo build --release

define make_page
	cat $(1) | \
		$(SEARCH_REPLACE) \
			'<!--#include virtual="components/head.html" -->' "$$(cat components/$(3)/head.html)" \
			'<!--#include virtual="components/header.html" -->' "$$(cat components/$(3)/header.html)" \
			'<!--#include virtual="components/nav.html" -->' "$$(cat components/$(3)/nav.html)" \
			'<!--#include virtual="components/footer.html" -->' "$$(cat components/$(3)/footer.html)" \
	> $(2)
endef

dst/cz/%.html: pages/cz/%.html $(SEARCH_REPLACE) $(COMPONENTS:%=components/cz/%)
	@mkdir -p $(@D)
	$(call make_page,$<,$@,cz)

dst/en/%.html: pages/en/%.html $(SEARCH_REPLACE) $(COMPONENTS:%=components/en/%)
	@mkdir -p $(@D)
	$(call make_page,$<,$@,en)

dst/%.css: styles/%.css
	@mkdir -p $(@D)
	ln -f $< $@

dst/%.js: scripts/%.js
	@mkdir -p $(@D)
	ln -f $< $@

dst/poster_%.webp: images/poster_%.png
	@mkdir -p $(@D)
	magick $< -resize 2000x2000 $@

dst/poster_%.jpg: images/poster_%.png
	@mkdir -p $(@D)
	magick $< -resize 2000x2000 $@

dst/word_cloud_%.png: images/word_cloud_%.png
	@mkdir -p $(@D)
	magick $< -resize 2000x2000 $@

dst/background.jpg: images/background.jpg
	@mkdir -p $(@D)
	magick $< -quality 80 $@

dst/%.jpg: images/%.jpg
	@mkdir -p $(@D)
	ln -f $< $@

dst/%.webp: images/%.jpg
	@mkdir -p $(@D)
	magick $< $@

dst/%.png: images/%.png
	@mkdir -p $(@D)
	ln -f $< $@

dst/%.svg: icons/%.svg
	@mkdir -p $(@D)
	ln -f $< $@

dst/favicon.ico: images/favicon.ico
	@mkdir -p $(@D)
	ln -f $< $@

dst/%.woff2: fonts/%.woff2
	@mkdir -p $(@D)
	ln -f $< $@

dst/libre_liberec.tar.gz: $(PAGES:%=dst/cz/%) $(PAGES:%=dst/en/%) $(SHARED_FILES:%=dst/%)
	tar --transform='s,^dst/,libre_liberec/,' -czvf $@ $^ --owner=0 --group=0
