# 2012.10.09 - Giuseppe Sucameli
# This makefile can be used with each pyQGIS plugin. 
#
# It searches all the .ui and .qrc files recursively in the current directory 
# tree, then it compiles the files it has found according to the following 
# schema:
# <srcdir>/<relpath>/<filename>.ui -> <outdir>/<relpath>/<filename>_ui.py
# <srcdir>/<relpath>/<filename>.qrc -> <outdir>/<relpath>/<filename>_rc.py
#
# Use SRCDIR and OUTDIR to override source and/or destination directories.

# macros to generate the output filename from the input filename
ui_in2outname=$(patsubst %.ui,%_ui.py,$(1))
rc_in2outname=$(patsubst %.qrc,%_rc.py,$(1))

# source directory: it's current dir if not specified
SRCDIR?=$(CURDIR)
SRC_DIR:=$(abspath $(SRCDIR))

# the source dir must exist (use realpath to check it)
ifeq ($(realpath $(SRCDIR)),)
abort:
	@echo ">>> ERROR: The source dir \"$(SRC_DIR)\" doesn't exist."
endif

# source folder name, used to create the plugin package
PLUGIN_NAME?=$(notdir $(SRC_DIR))

# output directory: it's current dir if not specified
OUTDIR?=$(CURDIR)
OUT_DIR:=$(abspath $(OUTDIR))

# macro to make a recursive wildcard search:
# $1 is the directory tree the search starts from, $2 is the pattern
rwildcard=$(foreach d,$(wildcard $(1)*),$(call rwildcard,$(d)/,$(2))$(filter $(subst *,%,$(2)),$(d)))

# macro to replace $(SRC_DIR) with $(OUT_DIR) in the argument $1
in2outdir=$(patsubst $(SRC_DIR)%,$(OUT_DIR)%,$(1))

# macro to convert the .ui file paths $(1) to the generated .py file paths
ui_in2out=$(foreach f,$(1),$(call in2outdir,$(dir $(f)))$(call ui_in2outname,$(notdir $(f))))

# macro to convert the .qrc file paths $(1) to the generated .py file paths
rc_in2out=$(foreach f,$(1),$(call in2outdir,$(dir $(f)))$(call rc_in2outname,$(notdir $(f))))

# path to the .py files generated by pyuic4 from the .ui files
UI_SOURCES:=$(call rwildcard,$(SRC_DIR),*.ui)
UI_FILES:=$(call ui_in2out,$(UI_SOURCES))

# path to the .py files generated by pyrcc4 from the .qrc files
RC_SOURCES:=$(call rwildcard,$(SRC_DIR),*.qrc)
RC_FILES:=$(call rc_in2out,$(RC_SOURCES))

# rule template to compile .ui files
define UI_template
$(call ui_in2out,$(1)): $(1)
	python2-pyuic4 -o $$@ $$<
endef
# for each directory containing .ui files create a rule to compile them
UI_DIRS:=$(sort $(dir $(UI_SOURCES)))
$(foreach d,$(UI_DIRS),$(eval $(call UI_template,$(d)%.ui)))

# rule template to compile .qrc files
define RC_template
$(call rc_in2out,$(1)): $(1)
	pyrcc4 -o $$@ $$<
endef
# for each directory containing .qrc files create a rule to compile them
RC_DIRS:=$(sort $(dir $(RC_SOURCES)))
$(foreach d,$(RC_DIRS),$(eval $(call RC_template,$(d)%.qrc)))


all: copyfiles ui rc

ui: $(UI_FILES)
rc: $(RC_FILES)

copyfiles:
ifneq ($(SRC_DIR),$(OUT_DIR))
	mkdir -p $(OUT_DIR)
	cp -rf $(SRC_DIR)/* $(OUT_DIR)
endif

clean:
	rm -rf $(UI_FILES) $(RC_FILES)


# rule to generate a zip package containing all the stuff in the output directory
package: all
	@cd $(OUT_DIR)/../ && \
	rm -f $(PLUGIN_NAME).zip && \
	zip -qr $(PLUGIN_NAME).zip $(PLUGIN_NAME) -x \*.svn* -x \*.pyc -x \*~ -x \*entries\* -x \*.git\* -x \*.skip\* -x \*.settings\* -x \*.project\*
	@echo ">>> DONE: Package \"$(abspath $(OUT_DIR)/../)/$(PLUGIN_NAME).zip\" created."
	@cd $(CURDIR)

