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

#export DH_VERBOSE=1


# NOTE:
# Anyone willing to convert the package to pure debhelper
# (i.e., without dh sequencer) is very welcome to do so.

%:
	dh $@ --with python3,sphinxdoc

override_dh_auto_build: build-stamp

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

build_cython:
	dh_testdir
	python3 setup.py build_cython
	touch $@

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

build_sphinx:
	dh_testdir
	python3 setup.py build_ext --inplace 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
	python3-dbg setup.py install --force --root=debian/tmp \
               --install-lib=/usr/lib/s3ql \
               --install-scripts=/usr/lib/s3ql \
               --no-compile -O0 --install-layout=deb

	python3 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)))
	python3 setup.py build_ext --inplace
	# Some tests will try to create log files in $HOME
	mkdir -p debian/tmphome
	HOME=$(CURDIR)/debian/tmphome py.test-3 tests/
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

	# Link all executable files not ending in .py into /usr/bin 
	mkdir -p debian/s3ql/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_link:
	rm -rf debian/s3ql-dbg/usr/share/doc/s3ql-dbg
	dh_link


# We don't want to call setup.py clean, because this
# requires a number of (potentially uninstalled) build-dependencies.
override_dh_auto_clean:
	rm -rf build doc tests/test.log src/s3ql/deltadump.c contrib/*.1
	find \( \( -name '*.egg-info' -type d \) \
	     -o \( -name __pycache__  -type d \) \
	     -o \( -name '*.so' -type f \) \
	     \) -prune -exec rm -rf '{}' +
	rm -rf build-python build_cython build_sphinx build-stamp
	rm -rf debian/tmphome

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