#!/usr/bin/make -f

# try building with python3
export CALIBRE_PY3_PORT=1
SETUP=python3 setup.py

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


# Do all test by default
TEST_FLAGS =

# Disable some test when in package builder environment
ifdef SCHROOT_USER
  include debian/buildd-options.mak
endif

%:
	dh $@ --buildsystem=makefile

.PHONY: get-orig-source test

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

export QT_SELECT=qt5

override_dh_auto_configure:
#	# apply binary patch
	find debian/patches/py3-binary/ -type f -name "*.patch"  | sort | xargs --verbose --no-run-if-empty git apply
#	# use system mathjax
	$(SETUP) mathjax --mathjax-url="file://$(CURDIR)/debian/mathjax/MathJax-3.0.5.tar.gz"

override_dh_auto_build:
#	# Add false-positive info for blhc(1) check
	@echo 'blhc: ignore-line-regexp: \tCompiling form /.+\.ui'
	[ -d build ] || $(SETUP) build

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

test:
	LANGUAGE= LC_ALL=C LC_CTYPE=C LC_MESSAGES=C LANG=C TZ=UTC xvfb-run $(SETUP) test $(TEST_FLAGS)

override_dh_install:
	dh_buildinfo
	dh_sip3 -pcalibre-bin
	dh_install

override_dh_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

#	# do not ship a copy of fonts-liberation
	rm -r debian/tmp/usr/share/calibre/fonts/liberation/
	ln -s /usr/share/fonts/truetype/liberation/ debian/tmp/usr/share/calibre/fonts/liberation

#	# fix executable permissions
	find debian/tmp/usr/lib/calibre/calibre -type f -executable | xargs --no-run-if-empty chmod a-x

#	# fix "env python3" style hashbangs
	find debian/tmp -type f | xargs sed -i '1 { /^#!.*python/ s_^.*$$_#!/usr/bin/python3_ }'

override_dh_installman:
#	# Create and install some of the man pages
	ln -sf $(CURDIR)/man-pages debian/man
	dh_installman

override_dh_auto_clean:
	rm -f  debian/man
	[ ! -d build ] || $(SETUP) build -c
	$(SETUP) mathjax --clean
	py3clean setup src
	find . -name '*.pyc' -delete
	find . -name '*.so'  -delete

override_dh_gencontrol:
	dh_gencontrol $(DEB_DH_GENCONTROL_ARGS)

get-orig-source:
	uscan --verbose
