.ONESHELL:
ENV_PREFIX=$(shell python -c "if __import__('pathlib').Path('.venv/bin/pip').exists(): print('.venv/bin/')")

.PHONY: help
help:             ## Show the help.
	@echo "Usage: make <target>"
	@echo ""
	@echo "Targets:"
	@fgrep "##" Makefile | fgrep -v fgrep

.PHONY: run
run:              ## Run the application.
	python -m doi_downloader


.PHONY: show
show:             ## Show the current environment.
	@echo "Current environment:"
	@echo "Running using $(ENV_PREFIX)"
	@$(ENV_PREFIX)python -V
	@$(ENV_PREFIX)python -m site

.PHONY: install
install:          ## Install the project in dev mode.
	@echo "Don't forget to run 'make virtualenv' if you got errors."
	$(ENV_PREFIX)pip install -e .[examples] --group dev --group docs --group publishing

.PHONY: fmt
fmt:              ## Format code using black & isort.
	$(ENV_PREFIX)ruff format doi_downloader/

.PHONY: lint
lint:             ## Run pep8, black, mypy linters.
	$(ENV_PREFIX)ruff check doi_downloader/

.PHONY: test
test: lint        ## Run tests and generate coverage report.
	@set -e
	$(ENV_PREFIX)pytest -v --cov-config .coveragerc --cov=doi_downloader -l --tb=short --maxfail=1 tests/
	$(ENV_PREFIX)coverage xml
	$(ENV_PREFIX)coverage html

.PHONY: watch
watch:            ## Run tests on every change.
	ls **/**.py | entr $(ENV_PREFIX)pytest -s -vvv -l --tb=long --maxfail=1 tests/

.PHONY: clean
clean:            ## Clean unused files.
	@find ./ -name '*.pyc' -exec rm -f {} \;
	@find ./ -name '__pycache__' -exec rm -rf {} \;
	@find ./ -name 'Thumbs.db' -exec rm -f {} \;
	@find ./ -name '*~' -exec rm -f {} \;
	@rm -rf .cache
	@rm -rf .pytest_cache
	@rm -rf .mypy_cache
	@rm -rf build
	@rm -rf dist
	@rm -rf *.egg-info
	@rm -rf htmlcov
	@rm -rf .tox/
	@rm -rf docs/_build

.PHONY: virtualenv
virtualenv:       ## Create a virtual environment.
	@echo "creating virtualenv ..."
	@rm -rf .venv
	@python -m venv .venv
	@./.venv/bin/python -m pip install -U pip setuptools
	@./.venv/bin/pip install -e .[examples] --group dev --group docs --group publishing
	@echo
	@echo "!!! Please run 'source .venv/bin/activate' to enable the environment !!!"

.PHONY: release
release:          ## Create a new tag for release (via bump-my-version).
	@set -e
	@echo "WARNING: This operation will bump the version, commit, tag and push to github"
	@test "$(shell git rev-parse --abbrev-ref HEAD)" = "main" || (echo "Not on main branch!" && exit 1)
	@git diff --exit-code --quiet || (echo "Uncommitted changes! Commit or stash first." && exit 1)
	@read -p "Bump level? (patch / minor / major) : " LEVEL
	@$(ENV_PREFIX)bump-my-version bump $${LEVEL}
	@$(ENV_PREFIX)gitchangelog > HISTORY.md
	@git add doi_downloader/VERSION pyproject.toml HISTORY.md
	@git commit -m "release: version $$(cat doi_downloader/VERSION) 🚀"
	@git tag $$(cat doi_downloader/VERSION)
	@git push origin main
	@git push origin $$(cat doi_downloader/VERSION)
	@echo "Github Actions will detect the new tag and release the new version."

.PHONY: docs
docs:             ## Build the documentation.
	@echo "building documentation ..."
	@$(ENV_PREFIX)mkdocs build
	# URL="site/index.html"; xdg-open $$URL || sensible-browser $$URL || x-www-browser $$URL || gnome-open $$URL || open $$URL

.PHONY: serve-docs
docs-serve: docs ## Serve the documentation locally.
	@echo "serving documentation locally ..."
	@echo "Open http://localhost:8000 in your browser"
	python -m http.server 8000 --directory site/

.PHONY: docs-deploy
docs-deploy: docs ## Deploy the documentation to gh-pages branch.
	@echo "Deploying documentation to gh-pages branch ..."
	@$(ENV_PREFIX)mkdocs gh-deploy


# This project has been generated from rochacbruno/python-project-template
# __author__ = 'rochacbruno'
# __repo__ = https://github.com/rochacbruno/python-project-template
# __sponsor__ = https://github.com/sponsors/rochacbruno/
