# Copyright (c) 2017 Pietro Albini <pietroalbini@ubuntu-it.org>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# Files in those directories will be converted:
DIRS_JS = js themes
DIRS_CSS = themes fonts
DIRS_FONTS = fonts
DIRS_IMAGES = themes pictograms

DIR_BUILD = build
DIR_ITALY_THEME = repos/italy-theme

JS_MINIFIER = python3 -m rjsmin
CSS_MINIFIER = python3 -m rcssmin
FONT_CONVERTER = fontforge convert-fonts.pe



# Get all the files that needs to be converted

js = $(patsubst %.js,$(DIR_BUILD)/%.min.js,$(shell find $(DIRS_JS) -type f -name "*.js"))
css = $(patsubst %.css,$(DIR_BUILD)/%.min.css,$(shell find $(DIRS_CSS) -type f -name "*.css"))
fonts_ttf = $(patsubst %,$(DIR_BUILD)/%,$(shell find $(DIRS_FONTS) -type f -name "*.ttf"))
fonts_eot = $(patsubst %.ttf,%.eot,$(fonts_ttf))
fonts_woff = $(patsubst %.ttf,%.woff,$(fonts_ttf))
images_png = $(patsubst %,$(DIR_BUILD)/%,$(shell find $(DIRS_IMAGES) -type f -name "*.png"))
images_jpg = $(patsubst %,$(DIR_BUILD)/%,$(shell find $(DIRS_IMAGES) -type f -name "*.jpg"))
images_svg = $(patsubst %,$(DIR_BUILD)/%,$(shell find $(DIRS_IMAGES) -type f -name "*.svg"))



.PHONY: build clean italy-theme build-inner

build: italy-theme
	@$(MAKE) build-inner

build-inner: $(js) $(css) $(fonts_ttf) $(fonts_eot) $(fonts_woff) $(images_png) $(images_jpg) $(images_svg)

clean:
	@rm -rf $(DIR_BUILD)

italy-theme:
	@git submodule update --init --recursive $(DIR_ITALY_THEME)
	@$(MAKE) -C $(DIR_ITALY_THEME)
	@mkdir -p themes/italy
	@cp -r $(DIR_ITALY_THEME)/build/* themes/italy


$(DIR_BUILD)/%.min.js: %.js
	@mkdir -p $(dir $@)
	@$(JS_MINIFIER) < $< > $@

$(DIR_BUILD)/%.min.css: %.css
	@mkdir -p $(dir $@)
	@$(CSS_MINIFIER) < $< > $@

$(DIR_BUILD)/%.ttf: %.ttf
	@mkdir -p $(dir $@)
	@cp $< $@

$(DIR_BUILD)/%.eot: %.ttf
	@mkdir -p $(dir $@)
	@$(FONT_CONVERTER) $< $@

$(DIR_BUILD)/%.woff: %.ttf
	@mkdir -p $(dir $@)
	@$(FONT_CONVERTER) $< $@

$(DIR_BUILD)/%.png: %.png
	@mkdir -p $(dir $@)
	@cp $< $@

$(DIR_BUILD)/%.jpg: %.jpg
	@mkdir -p $(dir $@)
	@cp $< $@

$(DIR_BUILD)/%.svg: %.svg
	@mkdir -p $(dir $@)
	@cp $< $@
