CURRENT_PATH := $(shell pwd)
MODULE := $(shell basename "$(CURRENT_PATH)")
VERSION := $(shell python -c "import sys; import $(MODULE); sys.stdout.write($(MODULE).__version__)")
SOURCES := $(shell find $(MODULE) -name '*.py')
DOCUMENTATION := $(shell find . -name '*.rst')
SPHINXOPTS    ?=
SPHINXBUILD   ?= sphinx-build
SPHINXSOURCEDIR = ./docs
SPHINXBUILDDIR  = docs/_build
# TODO: clean up
test: FORCE
	flake8 rna tests
	py.test

coverage:
	# coverage run $(MODULE) test
	py.test --cov=$(MODULE) || true
	# coverage report
	coverage html
	python -m webbrowser htmlcov/index.html

clean:
	coverage erase
	rm -rf htmlcov
	rm -rf docs/_build
	rm -rf docs/source
	pre-commit clean

publish:
	bump2version patch  # possible: major / minor / patch
	git push
	git push --tags

untag:
	# remove last tag. mostly, because publishing failed
	git tag -d v$(VERSION)
	git push origin :refs/tags/v$(VERSION)

requirements: setup.cfg
	# We have all the information in the setup.cfg file. For some reasons (e.g. bug in setuptools or limitations to use setup.cfg in readthedocs) we still need a requirements file
	python -c "import configparser; import os; config = configparser.ConfigParser(); config.read('setup.cfg'); deps = config['metadata']['install_requires'].split('\n'); deps = [x for x in deps if x]; head = '# Autogenerated by Makefile from setup.cfg install_requies section. Remove this line if you want to fix this file.'; path = 'requirements.txt'; f = open(path, 'r') if os.path.exists(path) else None; line = f.readline() if f else ''; quit('User defined requirements already existing.') if f and not line.startswith(head[:20]) else None; f = open('requirements.txt', 'w'); f.write('\n'.join([head] + deps))"

doc: Makefile $(SOURCES) $(DOCUMENTATION) docs/conf.py docs/apidoc-template/*
	# link apidoc to source and build html documentation with sphinx
	python setup.py build_sphinx
	# manual version would be
	# # link apidoc to source
	# sphinx-apidoc -o docs/source/ $(MODULE)
	# # build html documentation with sphinx
	# # @$(SPHINXBUILD) -M html "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(0)
	# sphinx-build -M html docs docs/_build
	#
	# open the html slides
	python -m webbrowser docs/_build/html/index.html

FORCE: ;
