VERSION_FILE = src/paperwork/_version.py
PYTHON ?= python3

build: build_c build_py

install: install_py install_c

uninstall: uninstall_py

build_py: ${VERSION_FILE}
	${PYTHON} ./setup.py build

build_c:

version: ${VERSION_FILE}

${VERSION_FILE}:
	echo "# -*- coding: utf-8 -*-" >| $@
	echo -n "version = \"" >> $@
	echo -n $(shell git describe --always) >> $@
	$(eval branch_name = $(shell git symbolic-ref HEAD 2>/dev/null))
	if [ -n "${branch_name}" ] && [ "${branch_name}" != "refs/heads/master" ] ; then echo -n "-${branch_name}" >> $@ ; fi
	echo "\"" >> $@
	echo "authors_code = \"\"\"" >> $@
	echo "    Code contributors:" >> $@
	git shortlog -sne >> $@
	echo "\"\"\"" >> $@
	echo >> $@
	cat ../AUTHORS.py >> $@

doc:

check:
	flake8 --exclude=_version.py src/paperwork

test:

windows_exe:
	${PYTHON} /mingw32/bin/pip3-script.py install .
	${PYTHON} ./setup_cx_freeze.py build_exe
	mkdir -p ../build
	mv $$(find build -name exe\*) ../build/exe
	# a bunch of things are missing ...
	cp -Ra /mingw32/lib/gdk-pixbuf-2.0 ../build/exe/lib
	mkdir -p ../build/exe/share
	cp -Ra /mingw32/share/icons ../build/exe/share
	cp -Ra /mingw32/share/locale ../build/exe/share
	cp -Ra /mingw32/share/themes ../build/exe/share
	cp -Ra /mingw32/share/fontconfig ../build/exe/share
	cp -Ra /mingw32/share/poppler ../build/exe/share
	cp -Ra /mingw32/share/glib-2.0 ../build/exe/share
	cp -Ra data ../build/exe

linux_exe:

release:
ifeq (${RELEASE}, )
	@echo "You must specify a release version (make release RELEASE=1.2.3)"
else
	@echo "Will release: ${RELEASE}"
	@echo "Checking release is in ChangeLog ..."
	grep ${RELEASE} ChangeLog | grep -v "/xx"
	@echo "Checking release is in data/work.openpaper.Paperwork.appdata.xml ..."
	grep ${RELEASE} data/work.openpaper.Paperwork.appdata.xml
endif

release_pypi:
	@echo "Releasing paperwork-gtk (paperwork) ..."
	${PYTHON} ./setup.py sdist
	twine upload dist/paperwork-${RELEASE}.tar.gz
	@echo "All done"

clean:
	rm -f ${VERSION_FILE}
	rm -rf build dist src/*.egg-info

# PIP_ARGS is used by Flatpak build
install_py: ${VERSION_FILE}
	${PYTHON} ./setup.py install ${PIP_ARGS}

install_c:

uninstall_py:
	pip3 uninstall -y paperwork

uninstall_c:

help:
	@echo "make build || make build_py"
	@echo "make check"
	@echo "make help: display this message"
	@echo "make install || make install_py"
	@echo "make uninstall || make uninstall_py"
	@echo "make release"

.PHONY: \
	build \
	build_c \
	build_py \
	check \
	doc \
	exe \
	help \
	install \
	install_c \
	install_py \
	release \
	test \
	uninstall \
	uninstall_c \
	version
