#!/usr/bin/make -f

SETUP=python2.7 setup.py

# This filters out *.so for the package "calibre". With this, we can
# install usr/lib in calibre.install and just leave out the binary
# bits.
DEB_DH_INSTALL_ARGS = $(if $(findstring -bin,$(cdbs_curpkg)),,-X*.so) --fail-missing
DEB_DH_GENCONTROL_ARGS = -- -Vpyqt:Version=$(shell dpkg-query -f'$${Version}' -W python-qt4)

include /usr/share/cdbs/1/rules/debhelper.mk

common-build-arch::
	[ -d build ] || $(SETUP) build

common-install-arch common-install-indep:: debian/tmp
	dh_buildinfo
	dh_sip

debian/tmp:
	env -u LANG -u LC_ALL -u LANGUAGE -u LC_MESSAGES CALIBRE_CONFIG_DIRECTORY=debian/tmp/config $(SETUP) install --root=debian/tmp/usr

	# remove bundled python libraries which are provided by system
	# packages
	rm -r debian/tmp/usr/lib/calibre/cherrypy
	rm -r debian/tmp/usr/lib/calibre/routes
	rm -r debian/tmp/usr/lib/calibre/chardet

	rm debian/tmp/usr/lib/calibre/calibre/utils/pyparsing.py
	rm debian/tmp/usr/lib/calibre/calibre/web/feeds/feedparser.py
	find debian/tmp/ -name '*.py' | xargs sed -i 's/calibre.utils.pyparsing/pyparsing/g'
	find debian/tmp/ -name '*.py' | xargs sed -i 's/calibre.ebooks.markdown/markdown/g'

	# not needed
	rm debian/tmp/usr/bin/calibre-uninstall

	# upstream's version is pretty crackful and suid root; replace with
	# simple script to use udisks --mount (just in case we don't have
	# automount) and eject
	rm debian/tmp/usr/bin/calibre-mount-helper
	install -m 755 debian/local/calibre-mount-helper debian/tmp/usr/bin/

	# remove hardcoded prs500 fonts, so that calibre falls back to
	# using the liberation ones in /usr/share/fonts/
	rm -r debian/tmp/usr/share/calibre/fonts/prs500/
	# 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

	# use system mathjax
	rm -rf debian/tmp/usr/share/calibre/viewer/mathjax
	ln -s /usr/share/javascript/mathjax debian/tmp/usr/share/calibre/viewer/mathjax

	# fix executable permissions
	find debian/tmp/usr/lib/calibre/calibre -type f | xargs chmod a-x
	find debian/tmp/usr/share/calibre/images -type f | xargs chmod a-x

	# add icon
	mkdir -p debian/tmp/usr/share/pixmaps/
	cp manual/resources/logo.png debian/tmp/usr/share/pixmaps/calibre-gui.png

	# remove bogus files on KFreeBSD installation
	rm -rf debian/tmp/config

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

install/calibre::
	# Create and install some of the man pages
	env -u LANG -u LC_ALL -u LANGUAGE -u LC_MESSAGES $(MAKE) -C debian/man VERSION="$(MAIN_VERSION)"

clean::
	rm -rf debian/orig
	[ ! -d build ] || $(SETUP) build -c
	$(MAKE) -C debian/man clean
	find . -name *.pyc -delete
	find . -name *.so -delete
	find . -name *.qrc -delete
	find . -name *_ui.py -delete

# Downloads the current upstream release according to debian/changelog, removes
# some bundled software copies and replaces the non-free prs500 TTFs with symlinks
# to their free liberation fonts counterparts. This also updates the unpacked
# source tree to the new upstream version.
get-orig-source:
	set -e; \
	V=`dpkg-parsechangelog | sed -rn '/^Version:/ {s/^Version: ([0-9.]+).*$$/\1/; p}'`; \
	mkdir -p debian/orig; cd debian/orig; \
	wget -O - https://downloads.sourceforge.net/project/calibre/$$V/calibre-$$V.tar.xz | xz -cd | tar x; \
	D=`pwd`; rm -f calibre*/src/odf/thumbnail.py; \
	rm -rf calibre*/src/unrar; \
	rm -rf calibre*/resources/viewer/mathjax; \
	rm -rf calibre*/src/calibre/ebooks/markdown; \
	cd calibre*/resources/fonts; \
	rm prs500/tt0003m_.ttf; ln -s ../liberation/LiberationSans-Regular.ttf prs500/tt0003m_.ttf; \
	rm prs500/tt0011m_.ttf; ln -s ../liberation/LiberationSerif-Regular.ttf prs500/tt0011m_.ttf; \
	rm prs500/tt0419m_.ttf; ln -s ../liberation/LiberationMono-Regular.ttf prs500/tt0419m_.ttf; \
	cd $$D; tar c . | xz -9 > ../../../calibre_$$V+dfsg.orig.tar.xz; \
	cd ../..; rm -r debian/orig; \
	find -mindepth 1 -maxdepth 1 ! -name '.bzr*' ! -name debian | xargs rm -rf; \
	[ ! -e setup.py ] || tar xzf ../calibre_$$V+dfsg.orig.tar.xz  --strip-components 2
