#!/usr/bin/make -f

MANPAGES = $(patsubst %.1.rst,%.1,$(wildcard debian/*.1.rst))
VERSION_dch := $(shell dpkg-parsechangelog --show-field Version)
VERSION_py := $(shell awk '/^VERSION = / {gsub(/"/, "", $$3); print $$3}' diffoscope/__init__.py )

ifneq ($(VERSION_dch),$(VERSION_py))
 $(error "Version in debian/changelog doesn't match diffoscope version")
endif

export PYBUILD_DESTDIR = debian/diffoscope

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

override_dh_python3:
	dh_python3 -p trydiffoscope
	dh_python3 -p diffoscope \
		--recommends=python-debian \
		--recommends=rpm-python \
		--recommends=tlsh \
		--recommends=guestfs \
		--recommends=argcomplete

override_dh_gencontrol:
	TOOLS="$$(bin/diffoscope --list-tools=debian | awk -F': ' '/Available-in-Debian-packages/ { print $$2 }' | \
		sed -e 's/\(^\| \)\(coreutils\|diffutils\|e2fsprogs\|findutils\|gzip\|tar\)\(,\|$$\)//g')"; \
		[ -n "$$TOOLS" ] || { echo '--list-tools failed' >&2; exit 1; }; \
		echo "diffoscope:Recommends=$$TOOLS" >> debian/diffoscope.substvars
	dh_gencontrol -O--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_installman: $(MANPAGES)
	dh_installman -O--buildsystem=pybuild

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

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