#!/usr/bin/make -f

SETUP=python 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

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

debian/tmp:
	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/pyPdf
	rm -r debian/tmp/usr/lib/calibre/routes

	rm debian/tmp/usr/lib/calibre/calibre/utils/pyparsing.py
	find debian/tmp/ -name '*.py' | xargs sed -i 's/calibre.utils.pyparsing/pyparsing/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 ttf-liberation
	rm -r debian/tmp/usr/share/calibre/fonts/liberation/
	ln -s /usr/share/fonts/truetype/ttf-liberation/ debian/tmp/usr/share/calibre/fonts/liberation

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

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

	# remove invalid hashbangs from recipes, they have no __main__
	find debian/tmp -name '*.recipe' | xargs sed -i '1 { /^#!/ d }'

install/calibre::
	# Create and install some of the man pages
	$(MAKE) -C debian/man VERSION="$(MAIN_VERSION)"

clean::
	rm -rf debian/orig
	[ ! -d build ] || $(SETUP) build -c
	$(MAKE) -C debian/man clean

# Downloads the current upstream release according to
# debian/changelog, and replace 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 - http://calibre.kovidgoyal.net/downloads/calibre-$$V.tar.gz | tar xz; \
	D=`pwd`; rm -f calibre*/src/odf/thumbnail.py; \
	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 . | gzip -9 > ../../../calibre_$$V+dfsg.orig.tar.gz; \
	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.gz  --strip-components 2
