# Makefile with useful targets

SOURCE_ROOT=$(HOME)/matrix-head
SCRIPT_ROOT=$(SOURCE_ROOT)/../scripts
APACHE_CONF=$(HOME)/apache/conf
CLIENT=
NOSE_ARGS=--exe -v

client_dirs=$(wildcard $(SOURCE_ROOT)/*_org $(SOURCE_ROOT)/*_shared)

.PHONY: docs lint clearcache all unittests checksettings validatesettings

all:
	@echo 'Available targets:'
	@echo
	@echo '  Testing:'
	@echo '    unittests 		Run generic unit tests'
	@echo '    clientunittests	Run client-specific tests'
	@echo
	@echo '  Development:'
	@echo '    checksettings	Check setting files for well-formedness'
	@echo '    docs		Generate docs using Doxygen.'
	@echo '    lint		Run pyflakes over the Python codebase.'
	@echo '    queryb		Generate updated JavaScript for the Query Builder'
	@echo '    tags                Generate a TAGS file for Emacs.'
	@echo '    ctags               Generate *.tags files for vi.'


queryb:
	maxx-python scripts/query-builder/build-js.py \
		<scripts/query-builder/interim-fields.tsv \
		>generic/themes/intranet/js/query_builder_data.js

checksettings:
	echo 'Verifying syntax of setting files...'
	for i in `find . -name '*xml' |grep -v broken_global.xml` ; do xmlwf $$i ; done

# Probably outdated, since the schema doesn't include any of
# the marketing information we added recently.
validatesettings:
	echo 'Validating and verifying syntax of setting files...'
	for i in `find . -regex './[^/]*/settings/[^/]*xml'|grep -v schemas.xml` ; do xmlstarlet val -E $$i ; done

tests: unittests

unittests:
	echo 'Running unit tests'
	cd $(SOURCE_ROOT) ; nosetests $(NOSE_ARGS) \
		$(SOURCE_ROOT)/libs/tests/unittests \
		$(SOURCE_ROOT)/generic/tests/unittests

clientunittests:
	echo 'Running client-specific unit tests'
	cd $(SOURCE_ROOT) ; nosetests $(NOSE_ARGS) $(client_dirs)

dbtests:
	echo 'Running database-related tests'
	echo '(none yet)'

clearcache:
	rm */sites/intranet/cached/*.html

# We want the tags to be found in a certain order, with generic classes
# being used first, before client-specific subclasses.
EXCLUDE_PAT=ckeditor.js
tags: TAGS
	rm TAGS
	for dir in libs generic scripts client *_shared *_org ; do \
		find $$dir -name "*.py" -or -name "*.js" | egrep -v "$(EXCLUDE_PAT)" | etags -a - ; done

ctags: aacrao.tags alagc.tags aopl.tags asf.tags dcww.tags dsa.tags dsa_shared.tags ecfc.tags epsa.tags eric.tags fact.tags fmiam.tags gma.tags impact.tags matrix.tags naes.tags nafc.tags nea.tags nmapc.tags npra.tags pmpa.tags psf.tags rrca.tags sqf.tags ufpa.tags urac.tags wedi.tags wfdsa.tags generic.tags nea_shared.tags

%.tags:
	find $*_org -name '*.py' | ctags -L - -f $@
generic.tags:
	find generic libs -name '*.py' | ctags -L - -f $@
nea_shared.tags:
	find nea_shared -name '*.py' | ctags -L - -f $@
dsa_shared.tags:
	find dsa_shared -name '*.py' | ctags -L - -f $@

lint:
	for file in `find . -name '*.py'` ; do \
		pyflakes $$file ; \
	done

docs:
	doxygen  scripts/development/Doxyfile