#!/usr/bin/make -f

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

SHELL := /bin/bash  # for the <(echo …) below, this was the easiest way.
PY_VERSION := $(shell awk '/^VERSION = / {gsub(/"/, "", $$3); print $$3}' diffoscope/__init__.py )
CHECK_VERSION := $(shell grep '^$(PY_VERSION)[^0-9].*$$' <(echo $(DEB_VERSION)))

ifneq ($(DEB_VERSION),$(PY_VERSION))
 ifneq ($(DEB_DISTRIBUTION),UNRELEASED)
  $(info $(CHECK_VERSION))
  ifneq (,$(CHECK_VERSION))
   $(info Accepting version mismatch $(PY_VERSION) <-> $(DEB_VERSION))
  else
   $(error Version in debian/changelog ($(DEB_VERSION)) does not match diffoscope version ($(PY_VERSION)))
  endif
 endif
endif

export PATH := $(CURDIR)/bin:$(PATH)
export PYBUILD_DESTDIR = debian/diffoscope-minimal
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
# the /debian/ one is required in buster-backports - unclear what creates that and why it's not present in bullseye
export PYBUILD_AFTER_INSTALL = rm -rvf '{destdir}/{install_dir}/htmlcov' '{destdir}/{install_dir}/.coverage' '{destdir}/{install_dir}/debian'

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

doc/%.1:
	$(MAKE) -C doc $*.1

debian/diffoscope-minimal.bash-completion:
	(register-python-argcomplete diffoscope || register-python-argcomplete3 diffoscope) > $@

execute_after_dh_auto_build: debian/diffoscope-minimal.bash-completion
	dh_auto_build -O--buildsystem=makefile -Ddoc

override_dh_python3:
	# dh_python3 will only work on binaries shipping files
	dh_python3 -p diffoscope-minimal \
		--depends-section=distro_detection \
		--recommends-section=cmdline \
		--recommends-section=comparators
	grep Recommends debian/diffoscope-minimal.substvars >> debian/diffoscope.substvars
	# Remove big Recommends from the -minimal package, leaving them only in the full one
	sed -i 's/python3-guestfs,//' debian/diffoscope-minimal.substvars
	sed -i 's/python3-binwalk,//' debian/diffoscope-minimal.substvars

execute_before_dh_gencontrol:
	bin/diffoscope --list-debian-substvars | tee \
		>(sed '/^diffoscope-minimal:/d' >> debian/diffoscope.substvars) | \
		sed '/^diffoscope:R/d' >> debian/diffoscope-minimal.substvars
	head debian/*.substvars

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

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

override_dh_auto_clean:
	debian/tests/control.sh
	@set -e ; if ! diff -q debian/tests/control debian/tests/control.tmp ; then \
		echo ;\
		echo "The generated control file differs from the actual one." ;\
		echo "A sourceful upload of this package is needed." ;\
		echo ;\
		echo "Differences:" ;\
		diff -u debian/tests/control debian/tests/control.tmp ;\
	else \
		rm debian/tests/control.tmp ;\
	fi
	dh_auto_clean -O--buildsystem=pybuild
	dh_auto_clean -O--buildsystem=makefile -Ddoc
	find -type d -name '__pycache__' -empty -delete
