#!/usr/bin/make

ifndef db
db=maildb
endif

# dsn='dbname=$(db) user=tester password=testpass'
# dba_dsn='dbname=$(db) $(dba_user_dsn_part)'
dsn='dbname=$(db)'

ifndef debug
debug=no
endif

cron_integration_behave_params=--tags ~@blocked

ifdef junit-dir
pymdb_behave_params=--junit --junit-directory=$(junit-dir)/mdb
cron_integration_behave_params+=--junit --junit-directory=$(junit-dir)/cron
pymdb_pytest_params=--junit-xml=$(junit-dir)/TESTS-pymdb.xml
cron_pytest_params=--junit-xml=$(junit-dir)/TESTS-cron.xml
endif

ifdef coverage-file
pymdb_pytest_params+=--cov-report= --cov=pymdb --cov-append
cron_pytest_params+=--cov-report= --cov=maildb_cron --cov-append
endif

# '__main__' cause normal usage works only 2.7+
behave=behave --format=progress2

all: install check migration-check


install:
	python scripts/make_db_users.py $(dsn)
	python scripts/build.py $(dsn) constants mail.sql filters.sql settings.sql stats.sql contacts.sql impl code util grants

clean-pyc:
	rm -f `find . -name '*.pyc' | xargs` 2>/dev/null || echo 'no pycs'

pymdb-check:
	py.test python/pymdb $(pymdb_pytest_params)

cron-check:
	py.test python/cron $(cron_pytest_params)

check: pymdb-check cron-check

behave-check:
	cd tests; DSN=$(dsn) DBA_DSN=$(dsn) $(behave) $(pymdb_behave_params) -D debug=$(debug) --stop

migration-check:
	./migration-check.sh $(db)

# migration-check runs behave tests on build-by-migrations-db on its own
# integration-check: behave-check migration-check
integration-check: migration-check

ifdef coverage-file
# coverage-report: .coverage ora2pg/.coverage
coverage-report: .coverage
	# coverage combine .coverage ora2pg/.coverage
	coverage xml -o $(coverage-file)
	rm .coverage
endif

.tags: mail.sql filters.sql settings.sql contacts.sql impl code migrations util pymdb
	ctags -f .tags -R mail.sql filters.sql settings.sql contacts.sql impl code migrations util pymdb --exclude=test_*

.PHONY: all install check migration-check code coverage-report .coverage
