# Add any tasks that are not dependent on files to the .PHONY list.
.PHONY: clean dev_env pip_lock_versions \
	lint test \
	tax_reports tax_report_job \
	unit_lint_job

env:
	python3.8 -m venv env
	env/bin/python -m pip install -q -U pip setuptools wheel
	env/bin/python -m pip install -q -r reqs.pip

dev_env: env
	if [ ! -f env/bin/py.test ]; then env/bin/pip install -q -I -r reqs.pip -r reqs-test.pip; fi

lint: dev_env
	env/bin/python -m flake8 \
		./*.py \
		royalties/ \
		tests/

test: dev_env
	env/bin/python -m pytest tests --cov royalties --cov-report term-missing

test_unit: dev_env
	env/bin/python -m pytest tests \
		--cov royalties tests/ \
		--cov-report xml \
		--junitxml=pyunit.xml

clean:
	rm -rf env reports .pytest_cache .coverage reports.zip pyunit.xml
	find . | grep -vE '^\./env' | grep -E '__pycache__|\.cache|\.pyc|\.pyo$\' | xargs rm -rf

pip_lock_versions: clean
	python3.8 -m venv env
	env/bin/python -m pip install -q -U pip setuptools wheel
	env/bin/python -m pip install -q -r requirements-to-freeze.txt
	echo "-i https://pypi.theorchard.io/pypi/" > reqs.pip
	env/bin/pip freeze >> reqs.pip

reports.zip:
	zip -r reports.zip reports

send_reports:
	ZIP_FILE_PATH=reports.zip env/bin/python email_report.py

tax_reports:
	OUTPUT_DIRECTORY=./reports TERRITORY_FILTER=domestic env/bin/python tax_report.py
	OUTPUT_DIRECTORY=./reports TERRITORY_FILTER=foreign env/bin/python tax_report.py
	OUTPUT_DIRECTORY=./reports TERRITORY_FILTER=all env/bin/python tax_report.py

tax_report_job: clean env tax_reports reports.zip send_reports clean

unit_lint_job: clean lint test_unit
