#!/usr/bin/make -f
# -*- makefile -*-

#export DH_VERBOSE=1

%:
	dh $@ --with python2,sphinxdoc

override_dh_auto_build: build-stamp

build-stamp: build_cython build-python build_sphinx 
	touch $@

build_cython:
	dh_testdir
	python setup.py build_cython
	touch $@

build-python: 
	dh_testdir
	python-dbg setup.py build -g
	python setup.py build -g
	touch $@

build_sphinx:
	dh_testdir
	python setup.py build_ext --inplace
	python setup.py build_sphinx
	touch $@

override_dh_auto_install: 
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	# Note: Install non -dbg last, so that scripts don't get a -dbg interpreter
	python-dbg setup.py install --force --root=debian/tmp \
               --install-lib=/usr/lib/s3ql \
               --install-scripts=/usr/lib/s3ql \
               --no-compile -O0 --install-layout=deb

	python setup.py install --force --root=debian/tmp \
               --install-lib=/usr/lib/s3ql \
               --install-scripts=/usr/lib/s3ql \
               --no-compile -O0 --install-layout=deb

override_dh_auto_test:
ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
	python setup.py test
endif

override_dh_install:
	dh_install

	# Rename to avoid conflict with pcp package
	(cd debian/s3ql/usr/share/man/man1/; mv pcp.1 parallel-cp.1)

	# Install debugging extension and remove from regular package
	(cd debian/s3ql; \
	    find . -name "*_d.so" -or \
	           -regextype posix-egrep -regex ".+-[a-z0-9]*d[a-z]*.so" \
	           -printf "%P\0" -delete) \
	| xargs -0 dh_install --autodest -ps3ql-dbg

override_dh_link:
	rm -rf debian/s3ql-dbg/usr/share/doc/s3ql-dbg
	dh_link

	# Link all executable files not ending in .py into /usr/bin
	for entry in debian/s3ql/usr/lib/s3ql/*; do \
	    if [ -d "$$entry" ] || [ ! -x "$$entry" ] \
               || [ "$${entry%.py}" != "$$entry" ]; then \
               continue; \
            fi; \
            ln -s ../lib/s3ql/`basename "$$entry"` \
                  "debian/s3ql/usr/bin/`basename \"$$entry\"`"; \
        done

override_dh_auto_clean:
	# We don't want to call setup.py clean, because this
	# requires a number of (potentially uninstalled) build-dependencies.
	rm -rf dist setup.log build doc src/s3ql/*.so
	rm -rf build-python* build_cython build_sphinx build-stamp

override_dh_strip:
ifeq (,$(filter nostrip,$(DEB_BUILD_OPTIONS)))
	dh_strip -ps3ql --dbg-package=s3ql-dbg
endif
