#!/usr/bin/make -f

# help for reproducible build
export LANG=C.UTF-8

# building with python3
PYTHON=$(shell py3versions -d)
PYTHON_PATH=$(shell command -v $(PYTHON))
SETUP=$(PYTHON) setup.py

# build options
export DEB_BUILD_MAINT_OPTIONS=hardening=+all reproducible=+all

# display build commands while CMake build
export VERBOSE=1

# Set hash seed to Qt resource compiler output will deterministic one.
# See QHash class document on Qt document.
export QT_HASH_SEED=0

# Do all test by default
TEST_FLAGS =
CI_ENV =

# Disable some test when in package builder environment
ifeq ($(wildcard .git),)
  include debian/buildd-options.mak
  CI_ENV += CI=true
endif

%:
	dh $@ --with=python3 --buildsystem=makefile

.PHONY: calibre_auto_test calibre_auto_install blhc-note

DEB_DH_GENCONTROL_ARGS = -- -Vpyqt:Version=$(shell dpkg-query -f'$${Version}' -W python3-pyqt6)

blhc-note:
#	# Add false-positive info for blhc(1) check
	@echo 'blhc: ignore-line-regexp: \tCompiling form /.+\.ui'
	@echo 'blhc: ignore-line-regexp: .+ -dM -E -o moc_predefs\.h /usr/lib/[^/]+/qt6/mkspecs/features/data/dummy\.cpp'
	@echo 'blhc: ignore-line-regexp: /usr/lib/qt6/libexec/moc .+\.cpp'

override_dh_auto_configure:

override_dh_auto_build: blhc-note
	$(SETUP) build
#
#	backup original font files and use system fonts
	if [ ! -d debian/liberation.backup ] ; then \
		mv resources/fonts/liberation debian/liberation.backup ; \
		$(SETUP) liberation_fonts --system-liberation_fonts --path-to-liberation_fonts=/usr/share/fonts/truetype/liberation2 ; \
	fi

# Invoke "calibre_auto_test" target with automatic "HOME" and "XDG_*" variables.
override_dh_auto_test:
	dh_auto_test -O--buildsystem=makefile -- -f debian/calibre-builder.mak test

# "test_ajax_book" fails if previous test is "test_workers" (Closes: #973458)
calibre_auto_test:
	env LANGUAGE= LC_ALL=C LC_CTYPE=C LC_MESSAGES=C LANG=C TZ=UTC $(CI_ENV) $(SETUP) test $(TEST_FLAGS) --exclude-test-name=ajax_book --exclude-test-name=workers --exclude-test-name=qt --exclude-test-name=dom_load
	env LANGUAGE= LC_ALL=C LC_CTYPE=C LC_MESSAGES=C LANG=C TZ=UTC $(CI_ENV) $(SETUP) test --test-name=ajax_book $(TEST_FLAGS)
	env LANGUAGE= LC_ALL=C LC_CTYPE=C LC_MESSAGES=C LANG=C TZ=UTC $(CI_ENV) $(SETUP) test --test-name=workers   $(TEST_FLAGS)
	env LANGUAGE= LC_ALL=C LC_CTYPE=C LC_MESSAGES=C LANG=C TZ=UTC $(CI_ENV) $(SETUP) test --test-name=qt        $(TEST_FLAGS)
	env LANGUAGE= LC_ALL=C LC_CTYPE=C LC_MESSAGES=C LANG=C TZ=UTC $(CI_ENV) $(SETUP) test --test-name=dom_load  $(TEST_FLAGS)

execute_before_dh_install:
	dh_buildinfo

override_dh_auto_install:
	dh_auto_install -O--buildsystem=makefile -- -f debian/calibre-builder.mak install

calibre_auto_install:
	mkdir -p  debian/tmp/usr/share/icons/hicolor
	mkdir -p  debian/tmp/usr/share/applications
	mkdir -p  debian/tmp/usr/share/mime/packages
	mkdir -p  debian/tmp/usr/share/zsh/vendor-completions
	env LANGUAGE= LC_ALL=C LC_CTYPE=C LC_MESSAGES=C LANG=C XDG_DATA_DIRS=$(CURDIR)/debian/tmp/usr/share CALIBRE_CONFIG_DIRECTORY=debian/tmp/config $(SETUP) install --root=debian/tmp/usr
#
#	# not needed
	rm debian/tmp/usr/share/calibre/calibre-portable.*
	rm debian/tmp/usr/lib/python*/site-packages/init_calibre.py
	rmdir debian/tmp/usr/share/desktop-directories
#
#	# do not install developer's script
	rm debian/tmp/usr/lib/calibre/calibre/devices/mtp/unix/upstream/update.py
#	# Debian package "python3-tinycss" doesn't installs test code
	rm debian/tmp/usr/lib/calibre/tinycss/tests/*.py
#
# 	# Replace "python" to "python3" in shebang
	find debian/tmp/usr/lib/calibre debian/tmp/usr/share/calibre -name '*.py' -print | xargs --no-run-if-empty sed --separate --file=debian/fix-python-shebang.sed --in-place
#
#	# Remove source path name from pyuic5 outputs
	find debian/tmp/usr/lib/calibre/calibre/gui2 -name '*_ui.py' -print | xargs --no-run-if-empty sed --separate --file=debian/remove-ui-basepath.sed --in-place

override_dh_installman:
	debian/install-manpages.sh

override_dh_auto_clean:
	$(SETUP) build --clean
	if [ -d debian/liberation.backup ] ; then \
		$(SETUP) liberation_fonts --clean ; \
		mv debian/liberation.backup resources/fonts/liberation ; \
	fi
	py3clean setup src

override_dh_gencontrol:
	dh_gencontrol $(DEB_DH_GENCONTROL_ARGS)

# No need to touch "calibre-bin".
override_dh_python3:
	dh_python3 --no-package=calibre-bin --shebang=$(PYTHON_PATH)
