#!/usr/bin/make -f

SETUP=python2.7 setup.py

# build hardening
export DEB_BUILD_MAINT_OPTIONS=hardening=+all

%:
	dh $@ --buildsystem=makefile

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

export QT_SELECT=qt5

override_dh_auto_configure:
	# use system mathjax
	$(SETUP) mathjax --system-mathjax --path-to-mathjax=/usr/share/javascript/mathjax

override_dh_auto_build:
	[ -d build ] || $(SETUP) build

override_dh_install:
	dh_buildinfo
	dh_sip -pcalibre-bin
	dh_install

override_dh_missing:
	dh_missing --fail-missing

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
	env -u LANG -u LC_ALL -u LANGUAGE -u LC_MESSAGES 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 python2" style hashbangs
	find debian/tmp -type f | xargs sed -i '1 { /^#!.*python/ s_^.*$$_#!/usr/bin/python2.7_ }'

override_dh_installman:
	# Create and install some of the man pages
	dh_installman -pcalibre man-pages/man1/*.1
	for i in man-pages/?? man-pages/??_?? ; do ll=`basename $$i`; dh_installman -pcalibre --language $$ll $$i/man1/*.1 ; done

override_dh_auto_clean:
	rm -rf debian/orig
	[ ! -d build ] || $(SETUP) build -c
	$(SETUP) mathjax --clean
	find . -name '*.pyc' -delete
	find . -name '*.so'  -delete
	find . -name '*.qrc' -delete

override_dh_gencontrol:
	dh_gencontrol $(DEB_DH_GENCONTROL_ARGS)

.PHONY: get-orig-source
get-orig-source:
	./debian/get-orig-source.sh
