ifndef FWDIR
  FWDIR = ..
  XGETTEXTOPTS += -k_r
endif

PAGES_DIR = $(wildcard ../pages/)
SRC_DIR = $(wildcard ../src/)
RESOURCES_DIR = $(wildcard ../resources/)

# files that are scanned for gettext strings
SCHEMA = $(wildcard ../db/schema.sql)
ifneq ($(words $(PAGES_DIR)), 0)
  PAGES = $(shell find ../pages -type f -name "*.xml")
  MODULES = $(shell find ../pages -type f -name "*.php")
else
  PAGES =
  MODULES =
endif
ifneq ($(words $(SRC_DIR)), 0)
  SOURCES = $(shell find ../src -type f -name "*.php")
else
  SOURCES =
endif
ifneq ($(words $(RESOURCES)), 0)
  RESOURCES = $(shell find ../resources -type f -name "*.phtml")
else
  RESOURCES =
endif

.PHONY: all
all: $(wildcard *.po)

%.po: rad.pot
	msgmerge --backup=none --no-fuzzy-matching --update -s $@ $<

rad.pot: $(PAGES) $(MODULES) $(SCHEMA) $(SOURCES) $(RESOURCES)
# TODO: automatically remove unused entries from rad.pot
	if [ $(words $(filter %.xml, $?)) -gt 0 ]; then \
		$(FWDIR)/bin/pagegettext.py -o $@ $(filter %.xml, $?); \
	fi
	touch $@
	if [ $(words $(filter %.php %.phtml, $?)) -gt 0 ]; then \
		xgettext $(XGETTEXTOPTS) --from-code=utf-8 -L php -sj -o $@ $(filter %.php %.phtml, $?); \
	fi
	if [ $(words $(filter %.sql, $?)) -gt 0 ]; then \
		$(FWDIR)/bin/schemagettext.py -o $@ $(filter %.sql, $?); \
	fi
