##
#
#   The pyroute2 project is dual licensed, see README.license.md for details
#
#
python ?= $(shell util/find_python.sh)
platform := $(shell uname -s)
releaseTag ?= $(shell git describe --tags --abbrev=0)
releaseDescription := $(shell git tag -l -n1 ${releaseTag} | sed 's/[0-9. ]\+//')

define nox
        {\
		which nox 2>/dev/null || {\
			${python} -m venv ~/.venv-boot/;\
			. ~/.venv-boot/bin/activate;\
			pip install --upgrade pip;\
			pip install nox;\
		};\
		nox $(1) -- '${noxconfig}';\
	}
endef

.PHONY: all
all:
	@echo targets:
	@echo
	@echo \* clean -- clean all generated files
	@echo \* docs -- generate project docs
	@echo \* dist -- create the package file
	@echo \* test -- run all the tests
	@echo \* install -- install lib into the system or the current virtualenv
	@echo \* uninstall -- uninstall lib
	@echo

.PHONY: git-clean
git-clean:
	git clean -d -f -x
	git remote prune origin
	git branch --merged | grep -vE '(^\*| master )' >/tmp/merged-branches && \
		( xargs git branch -d </tmp/merged-branches ) ||:

.PHONY: clean
clean:
	@rm -f lab/*html
	@rm -f lab/_static/conf.js
	@rm -rf lab/_build
	@rm -rf docs/html
	@rm -rf docs/man
	@rm -rf dist build MANIFEST
	@rm -f docs-build.log
	@rm -rf pyroute2.egg-info
	@find pyroute2 -name "*pyc" -exec rm -f "{}" \;
	@find pyroute2 -name "*pyo" -exec rm -f "{}" \;
	@git checkout VERSION 2>/dev/null ||:

.PHONY: VERSION
VERSION:
	@${python} util/update_version.py

.PHONY: docs
docs:
	$(call nox,-e docs)

.PHONY: lab
lab:
	$(call nox,-e lab)

.PHONY: format
format:
	$(call nox,-e linter)

.PHONY: test
test:
ifeq ($(platform), Linux)
	$(call nox,)
else ifeq ($(platform), OpenBSD)
	$(call nox,-e openbsd)
else
	$(info >> Platform not supported)
endif

.PHONY: test-platform
test-platform:
	$(call nox,-e test_platform)

.PHONY: upload
upload: dist
	$(call nox,-e upload)

.PHONY: release
release: dist
	gh release create \
		--verify-tag \
		--title "${releaseDescription}" \
		${releaseTag} \
		./dist/*${releaseTag}*

.PHONY: setup
setup:
	$(MAKE) VERSION

.PHONY: dist
dist: setup
	$(call nox,-e build)

.PHONY: dist-minimal
dist-minimal: setup
	$(call nox,-e build_minimal)

.PHONY: install
install: setup
	$(MAKE) uninstall
	$(MAKE) clean
	$(call nox,-e build)
	${python} -m pip install dist/pyroute2-*whl ${root}

.PHONY: install-minimal
install-minimal: dist-minimal
	${python} -m pip install dist/pyroute2.minimal*whl ${root}

.PHONY: uninstall
uninstall:
	${python} -m pip uninstall -y pyroute2
	${python} -m pip uninstall -y pyroute2-minimal

.PHONY: audit-imports
audit-imports:
	findimports -n pyroute2 2>/dev/null | awk -f util/imports_dict.awk

.PHONY: nox
nox:
	$(call nox,-e ${session})
