#!/usr/bin/make -f

include /usr/share/dpkg/pkg-info.mk

MANPAGES = $(patsubst %.1.rst,%.1,$(wildcard debian/*.1.rst))
PY_VERSION := $(shell awk '/^VERSION = / {gsub(/"/, "", $$3); print $$3}' diffoscope/__init__.py )

ifneq ($(DEB_VERSION),$(PY_VERSION))
 ifneq ($(DEB_DISTRIBUTION),UNRELEASED)
  ifneq (,$(filter %-backports,$(DEB_DISTRIBUTION)))
    ifeq ($(DEB_VERSION:~bpo9+1=),$(PY_VERSION))
     $(info Building a Debian backport, accepting version mismatch $(PY_VERSION) <-> $(DEB_VERSION))
    else
     $(error Version in debian/changelog does not match diffoscope version or backport policy)
    endif
  else
   $(error Version in debian/changelog does not match diffoscope version)
  endif
 endif
endif

export PATH := $(CURDIR)/bin:$(PATH)
export PYBUILD_DESTDIR = debian/diffoscope
export PYBUILD_TEST_ARGS = -vv -r sxX -l --cov=diffoscope --cov-report=term-missing --cov-report=html
export PYBUILD_BEFORE_TEST = flake8 --config=/dev/null --select=F821
export PYBUILD_AFTER_TEST = debian/tests/basic-command-line
export PYBUILD_AFTER_INSTALL = rm -rvf '{destdir}/{install_dir}/htmlcov' '{destdir}/{install_dir}/.coverage'

ifneq ($(JENKINS_EXPORTS_DIR),)
  export PYBUILD_BEFORE_TEST += && mkdir -pv $(JENKINS_EXPORTS_DIR)/test-results
  export PYBUILD_TEST_ARGS += --junit-xml=$(JENKINS_EXPORTS_DIR)/test-results/tests.xml
endif


%:
	dh $@ --with python3 --with bash-completion --buildsystem=pybuild

debian/%.1: debian/%.1.rst
	rst2man $< $@

debian/diffoscope.bash-completion:
	register-python-argcomplete diffoscope > $@

override_dh_auto_build: debian/diffoscope.bash-completion $(MANPAGES)
	dh_auto_build -O--buildsystem=pybuild

override_dh_auto_test:
# Let's avoid plonking everything in the "DOS/MBR" category, this is really not
# a sustainable filetype testing solution (e.g. c051ba9d and c1442405).
	! grep 'DOS/MBR' -R diffoscope
	dh_auto_test

override_dh_installman: $(MANPAGES)
	dh_installman -O--buildsystem=pybuild

override_dh_python3:
	dh_python3 -p diffoscope \
		--depends=distro \
		--recommends=argcomplete \
		--recommends=binwalk \
		--recommends=defusedxml \
		--recommends=guestfs \
		--recommends=progressbar \
		--recommends=python-debian \
		--recommends=rpm-python \
		--recommends=tlsh \

override_dh_gencontrol:
	bin/diffoscope --list-debian-substvars >> debian/diffoscope.substvars
	dh_gencontrol -O--buildsystem=pybuild

diffoscope/presenters/icon.py: favicon.png
	(echo '# Generated from $@'; \
	 echo 'FAVICON_BASE64 = """'; \
	 base64 $<; \
	 echo '""".replace("\\n", "")'; \
	) > $@

favicon.png: logo.svg
	inkscape -w 32 -h 32 -e $@ $<
