#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
TEST_HOME=$(CURDIR)/tmp-home

# Append -Wl,-E to LDFLAGS for mips,mipsel,s390x archs, see:
# https://lists.debian.org/debian-mips/2014/09/msg00041.html
DEB_HOST_ARCH := $(shell dpkg-architecture -qDEB_HOST_ARCH)
ifneq (,$(filter mips mipsel mips64 mips64el s390x,$(DEB_HOST_ARCH)))
	LDFLAGS += "-Wl,-E"
endif

%:
	dh $@ --with python2,autotools_dev

override_dh_auto_configure:
	dh_auto_configure -- --without-included-ltdl LDFLAGS="$(LDFLAGS) -Wl,--as-needed"

override_dh_auto_build:
	dh_auto_build

	# Build python-wit:
	dh_auto_build --sourcedirectory app/python/wit

	# Build dicoclient python modules:
	dh_auto_build --sourcedirectory dicoweb/dicoclient

	# Make those files executable before installing them:
	chmod +x debian/dicodconfig debian/dicodconfig.alias

override_dh_auto_install:
	dh_auto_install

	# Install python-wit:
	dh_auto_install --sourcedirectory app/python/wit -- --root=$(CURDIR)/debian/python-wit

	# Install dicoclient python modules:
	dh_auto_install --sourcedirectory dicoweb/dicoclient -- --root=$(CURDIR)/debian/python-dicoclient

override_dh_install:
	dh_install
	# Install dicoweb:
	dh_install -pdicoweb dicoweb usr/share -Xdicoclient -XINSTALL -Xtemplates -Xsettings-sample.py -Xstatic
	dh_install -pdicoweb dicoweb/templates etc/dicoweb
	dh_install -pdicoweb dicoweb/static etc/dicoweb
	dh_install -pdicoweb debian/apache2.conf etc/dicoweb
	[ ! -d debian/dicoweb/etc/dicoweb/ ] || install -m 0644 dicoweb/settings-sample.py $(CURDIR)/debian/dicoweb/etc/dicoweb/settings.py

override_dh_auto_clean:
	dh_auto_clean

	# Clean python-wit:
	dh_auto_clean --sourcedirectory app/python/wit

	# Clean dicoclient python modules:
	dh_auto_clean --sourcedirectory dicoweb/dicoclient

override_dh_auto_test:
	mkdir -p $(TEST_HOME)
	HOME=$(TEST_HOME) dh_auto_test
