ifneq ($(words $(OUTDIR)), 1)
  $(error Variable OUTDIR must be defined and it must not contain whitespace.)
endif
ifneq ($(words $(APPDIR)), 1)
  $(error Variable APPDIR must be defined and it must not contain whitespace.)
endif

# external commands
RSYNC = rsync
RSYNCOPTS = -rltp --delete --exclude "*.swp"
GENTREE = bin/gentree.py
GENTREEOTPS =
GENPAGE = bin/genpage.py
GENPAGEOTPS =
GENSCHEMA = bin/genschema.py
GENSCHEMAOPTS =
GENQUERIES = bin/genqueries.py
GENQUERIESOPTS =

# directories that might need to be created during the build
DIRECTORIES = $(OUTDIR)/generated $(OUTDIR)/public $(APPDIR)/.cache

# base directories of the framework, the app and all plugins
BASEDIRS_RAD = $(PLUGINS) .
BASEDIRS_APP = $(APPDIR)
BASEDIRS = $(BASEDIRS_APP) $(BASEDIRS_RAD)

# languages and gettext related files
PODIRS_RAD = $(addsuffix /po/, $(PLUGINS) .)
PODIR_APP = $(APPDIR)/po/
LANGS_RAD = $(sort $(basename $(notdir $(wildcard $(addsuffix *.po, $(PODIRS_RAD))))))
LANGS_APP = $(basename $(notdir $(wildcard $(PODIR_APP)*.po)))
MOPATTERN_RAD = $(OUTDIR)/locale/%/LC_MESSAGES/rad.mo
MOPATTERN_APP = $(OUTDIR)/locale/%/LC_MESSAGES/rad-app.mo

# paths related to pages
PAGES_DIRS_RAD = $(wildcard $(addsuffix /pages, $(BASEDIRS_RAD)))
PAGES_DIR_APP = $(wildcard $(addsuffix /pages, $(BASEDIRS_APP)))
PAGES_DIRS = $(PAGES_DIRS_RAD) $(PAGES_DIR_APP)
ifneq ($(words $(PAGES_DIRS)), 0)
  PAGES_SOURCE = $(shell find $(PAGES_DIRS) -type f -name "*.xml")
  MODULES_SOURCE = $(shell find $(PAGES_DIRS) -type f -name "*.php")
else
  PAGES_SOURCE =
  MODULES_SOURCE =
endif
ALL_PAGE_SOURCES = $(PAGES_SOURCE) $(MODULES_SOURCE)

# config files TODO
CFG_ROUTES = $(wildcard $(addsuffix /etc/routes.xml, $(BASEDIRS)))

# other paths
PUBLIC_DIRS = $(wildcard $(addsuffix /public/, $(BASEDIRS)))
SCHEMA_FILES = $(wildcard $(addsuffix /db/schema.sql, $(BASEDIRS)))

.PHONY: all clean
all: src public gettext pages-tree resources schema pages queries data-dir
clean: clean-src clean-public clean-gettext clean-pages-tree clean-resources \
	clean-schema clean-pages clean-queries clean-directories


# generate gettext databases
.PHONY: gettext gettext-rad gettext-app clean-gettext
gettext: gettext-rad gettext-app
gettext-rad:
ifneq ($(words $(LANGS_RAD)), 0)
	python bin/makegettext.py $(addprefix -l, $(LANGS_RAD)) -m $(MOPATTERN_RAD) $(PODIRS_RAD)
endif
gettext-app:
ifneq ($(words $(LANGS_APP)), 0)
	python bin/makegettext.py $(addprefix -l, $(LANGS_APP)) -m $(MOPATTERN_APP) $(PODIR_APP)
endif
clean-gettext:
	rm -rf $(OUTDIR)/locale

# deploy PHP class files and external dependencies
.PHONY: src clean-src
src:
	if [ -d $(APPDIR)/src ]; then \
		$(RSYNC) $(RSYNCOPTS) -- $(APPDIR)/src/ $(OUTDIR)/src; \
	fi
	if [ -d $(APPDIR)/lib ]; then \
		$(RSYNC) $(RSYNCOPTS) -- $(APPDIR)/lib/ $(OUTDIR)/lib; \
	fi
	$(RSYNC) $(RSYNCOPTS) -- $(APPDIR)/vendor/ $(OUTDIR)/vendor
clean-src:
	rm -rf $(OUTDIR)/src $(OUTDIR)/lib
	rm -rf $(OUTDIR)/vendor

# deploy public files
.PHONY: public clean-public
public: | $(OUTDIR)/public
ifneq ($(words $(PUBLIC_DIRS)), 0)
	$(RSYNC) $(RSYNCOPTS) -- $(PUBLIC_DIRS) $(OUTDIR)/public
endif
clean-public:
	rm -rf $(OUTDIR)/public

# deploy resources
.PHONY: resources clean-resources
resources:
	if [ -d $(APPDIR)/resources ]; then \
		$(RSYNC) $(RSYNCOPTS) -- $(APPDIR)/resources/ $(OUTDIR)/resources; \
	fi
clean-resources:
	rm -rf $(OUTDIR)/resources

# generate page tree
.PHONY: pages-tree clean-pages-tree
pages-tree: $(OUTDIR)/generated/pagetree.php
$(OUTDIR)/generated/pagetree.php: $(PAGES_SOURCE) $(CFG_ROUTES) | $(OUTDIR)/generated
	python $(GENTREE) $(GENTREEOPTS) $(addprefix -p, $(PAGES_DIRS)) $(addprefix -r, $(CFG_ROUTES)) $@
clean-pages-tree:
	rm -f $(OUTDIR)/generated/pagetree.php

# build pages
.PHONY: pages pages-rad pages-app clean-pages
pages: pages-rad pages-app
pages-rad: gettext-rad
ifneq ($(words $(PAGES_DIRS_RAD)), 0)
	python $(GENPAGE) $(GENPAGEOPTS) $(addprefix -l, $(LANGS_RAD)) -m $(OUTDIR)/locale -d rad -o $(OUTDIR)/generated/pages $(PAGES_DIRS_RAD)
endif
pages-app: gettext-app
ifneq ($(words $(PAGES_DIR_APP)), 0)
	python $(GENPAGE) $(GENPAGEOPTS) $(addprefix -l, $(LANGS_APP)) -m $(OUTDIR)/locale -d rad-app -o $(OUTDIR)/generated/pages $(PAGES_DIR_APP)
endif
clean-pages:
	rm -rf $(OUTDIR)/generated/pages

# parse queries
.PHONY: queries clean-queries
queries: $(OUTDIR)/generated/queries.json
$(OUTDIR)/generated/queries.json: $(APPDIR)/.cache/schema.dat $(ALL_PAGE_SOURCES) | $(OUTDIR)/generated
	if [ "z$(filter $<, $?)" = "z" ]; then \
		python $(GENQUERIES) $(GENQUERIESOPTS) $< $@ $?; \
	else \
		python $(GENQUERIES) $(GENQUERIESOPTS) $< $@ $(ALL_PAGE_SOURCES); \
	fi
clean-queries:
	rm -f $(OUTDIR)/generated/queries.json

# parse schema
.PHONY: schema clean-schema
schema: $(OUTDIR)/generated/schema.php
$(OUTDIR)/generated/schema.php $(APPDIR)/.cache/schema.dat: $(SCHEMA_FILES) | $(APPDIR)/.cache
	python $(GENSCHEMA) $(GENSCHEMAOPTS) --pickle-out $(APPDIR)/.cache/schema.dat -o$(OUTDIR)/generated/schema.php $^
clean-schema:
	rm -f $(APPDIR)/.cache/schema.dat $(OUTDIR)/generated/schema.php

# create directory structure in /data (for now only /data/files is needed)
.PHONY: data-dir
data-dir: | $(OUTDIR)/data/files
$(OUTDIR)/data/files:
	mkdir -p $@

# create and remove $(DIRECTORIES)
.PHONY: clean-directories
$(DIRECTORIES):
	mkdir -p $@
clean-directories:
	rm -df $(DIRECTORIES)
