# use markdown template for po4a, or some custom one?
HTMLTEMPLATE = text

# 1:source 2:locales
define SOURCE_template
$(1): init $$($(1)_MODULES) $(dummy_MODULES)

.PHONY: $(1)
endef
$(foreach source,$(SOURCES),$(eval $(call SOURCE_template,$(source),$($(source)_LOCALES))))



# Override po4a with local Markdown-supportive text module
export PERL5LIB = $(CURDIR)/perl

# 1:moduledir 2:locale 3:mastermodule 4:source
define POT_template
fileformat = $(if $(filter templates,$(3)),$(HTMLTEMPLATE),text)
filetype = $(if $(filter templates,$(3)),tmpl,mdwn)
ALL_POTFILES += $(podir)/$(3).pot
$(3)_L10NFILES = $$(patsubst $(1)/%,%,$$(shell find $(1) -type f -name '*.$$(filetype)' | LANG=C sort))
MASTER_L10NFILES += $$(patsubst %,$(1)/%,$$($(3)_L10NFILES))

$(podir)/$(3).pot: $(1)
	$(MAKE) -f "$$(CURDIR)/make/po4a.mk" -C "$(1)" package="$(4)" module="$(3)" version="$(5)" locale="LOCALE" fileformat="$$(fileformat)" l10nfiles="$$($(3)_L10NFILES)" potfile="$(CURDIR)/$$@" mkpot; \

.PHONY: $(1)
endef
$(foreach source,$(SOURCES),$(foreach module,$($(source)_POMODULES),$(eval $(call POT_template,$(masterdir),,$(module),$(source),$($(source)_VERSION)))))



# 1:moduledir 2:locale 3:mastermodule 4:source 5:originmodule
define INITPO_template
fileformat = $(if $(filter templates,$(3)),$(HTMLTEMPLATE),text)
filetype = $(if $(filter templates,$(3)),tmpl,mdwn)
ALL_POFILES += $(podir)/$(3).$(2).po
LOCALE_L10NFILES += $$(patsubst %,$(1)/%,$$($(3)_L10NFILES))

$(4)_POMODULES := $(filter-out $(3),$$($(4)_POMODULES))
$(4)_POLOCALES := $(filter-out $(2),$$($(4)_POLOCALES))

$(podir)/$(3).$(2).po: $(3) $(5)
	$(MAKE) -f "$$(CURDIR)/make/po4a.mk" -C "$(3)" fileformat="$$(fileformat)" l10nfiles="$$($(3)_L10NFILES)" masterdir="$$(CURDIR)/$(1)" pofile="$(CURDIR)/$$@" mkpo

# TODO: Implement the below for automated tests, after unfuzzing the above like this:
#	grep -v '^#, fuzzy$' | sed 's/^#, fuzzy,/#,/'
#
#$$(patsubst %,$(1)/%,$$($(3)_L10NFILES)): $(podir)/$(3).$(2).po
#	mkdir -p $(1)
#	$(MAKE) -f "$$(CURDIR)/make/po4a.mk" -C "$(1)" fileformat="$$(fileformat)" l10nfiles="$$($(3)_L10NFILES)" masterdir="$$(CURDIR)/$(3)" pofile="$(CURDIR)/$(podir)/$(3).$(locale).po" threshold=0 "$(CURDIR)/$$@"
endef
$(foreach source,$(SOURCES),$(foreach module,$(po-init-modules),$(foreach locale,$(po-init-locales),$(eval $(call INITPO_template,$(l10ndir),$(locale),$(module),$(source),$(if $(po-init-fromlocale),$(module)_l10n/$(po-init-fromlocale),$(module)))))))



# 1:moduledir 2:locale 3:mastermodule 4:source
define PO_template
fileformat = $(if $(filter templates,$(3)),$(HTMLTEMPLATE),text)
filetype = $(if $(filter templates,$(3)),tmpl,mdwn)
ALL_POFILES += $(podir)/$(3).$(2).po
LOCALE_L10NFILES += $$(patsubst %,$(1)/%,$$($(3)_L10NFILES))

$(podir)/$(3).$(2).po: $(3)
	$(MAKE) -f "$$(CURDIR)/make/po4a.mk" -C "$(3)" fileformat="$$(fileformat)" l10nfiles="$$($(3)_L10NFILES)" pofile="$(CURDIR)/$$@" updatepo

$$(patsubst %,$(1)/%,$$($(3)_L10NFILES)): $(podir)/$(3).$(2).po
	mkdir -p $(1)
	$(MAKE) -f "$$(CURDIR)/make/po4a.mk" -C "$(1)" fileformat="$$(fileformat)" l10nfiles="$$($(3)_L10NFILES)" masterdir="$$(CURDIR)/$(3)" pofile="$(CURDIR)/$(podir)/$(3).$(locale).po" "$$(patsubst $(1)/%,%,$$@)"
endef
# Only update if not initializing - can't do both at once
ifeq ($(po-init-modules),)
$(foreach source,$(SOURCES),$(foreach module,$($(source)_POMODULES),$(foreach locale,$($(source)_POLOCALES),$(eval $(call PO_template,$(l10ndir),$(locale),$(module),$(source))))))
endif



# 1:dummydir
define DUMMYMODULE_template
$(1):
	mkdir -p $$@

# WARNING: this may wipe unrelated files too!
ultraclean::
	rm -rf "$(1)"
endef
$(foreach module,$(dummy_MODULES),$(eval $(call DUMMYMODULE_template,$(masterdir))))



# 1:configfile
define CONFIG_template
$(1):
	mkdir -p $$@

# WARNING: this may wipe unrelated files too!
install::
	ikiwiki --setup $(1) --rebuild
endef
$(foreach config,$(CONFIGS),$(eval $(call CONFIG_template,$(config))))