#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS=hardening=+all,-pie
export DEB_CFLAGS_MAINT_APPEND=-fopenmp -ffunction-sections -fdata-sections
export DEB_LDFLAGS_MAINT_APPEND=-Wl,--as-needed -Wl,--gc-sections

MAKEOPTIONS = X_SYSTEM=Linux Q=

DBG_PACKAGE=$(shell dpkg-parsechangelog|grep '^Source: '|sed 's/^Source:\s*//')-dbg

MAKE := $(MAKE) X_SYSTEM=Linux Q=
PHPAPI = $(shell php-config5 --phpapi)

binary binary-arch binary-indep build build-arch build-indep clean install install-arch install-indep:
	dh $@ --parallel --list-missing --with=python2

get-orig-source:
	$(CURDIR)/debian/get-orig-source.sh

override_dh_auto_configure:
	./configure --prefix=/usr --includedir=/usr/include --mandir=/usr/share/man --infodir=/usr/share/info --sysconfdir=/etc --libdir=/usr/lib --libexecdir=/usr/lib --with-ruby=no

override_dh_auto_clean:
	touch config.make
	dh_auto_clean -- $(MAKEOPTIONS)

override_dh_auto_test:

override_dh_auto_build:
	mkdir -p objdir/api/python/
	touch objdir/api/python/_ExactImage.so # prevent from being built this time
	dh_auto_build -- all $(MAKEOPTIONS)
	set -e; \
	for python in $(shell pyversions -r); do \
		rm -f objdir/api/python/_ExactImage.so; \
		dh_auto_build -- $(MAKEOPTIONS) objdir/api/python/_ExactImage.so PYTHONINCS=`$$python-config --includes`; \
		mkdir -p objdir/api/$$python; \
		cp objdir/api/python/*.so objdir/api/$$python/; \
		cp objdir/api/python/*.py objdir/api/$$python/; \
		chmod a-x objdir/api/$$python/*; \
	done
	$(MAKE) -C debian/manpages/

override_dh_auto_install:
	dh_auto_install -- $(MAKEOPTIONS) WITHPYTHON=0
	mkdir debian/tmp/usr/lib/php5
	mv debian/tmp/usr/lib/php debian/tmp/usr/lib/php5/$(PHPAPI)
	echo "php:Depends=phpapi-$(PHPAPI)" >> debian/php5-exactimage.substvars
	set -e; \
	for python in $(shell pyversions -r); do \
		PYDIR=$$($${python} -c 'from distutils.sysconfig import get_python_lib; print get_python_lib()'); \
		mkdir -p debian/tmp/$${PYDIR}; \
		cp -a objdir/api/$$python/* debian/tmp/$${PYDIR}; \
	done
	cd debian/tmp/usr/lib/perl5 && mv site_perl/* . && rmdir site_perl

override_dh_strip:
	dh_strip -a --dbg-package="$(DBG_PACKAGE)"

override_dh_shlibdeps:
	dh_shlibdeps
	sed -i -e '/shlibs:Depends.*\(libevas[^ ]*\).*/ { p; s//evas:Depends=\1-engines-x/; b }; /^evas:Depends=/d' debian/*.substvars

.PHONY: binary binary-arch binary-indep build build-arch build-indep clean install install-arch install-indep \
	get-orig-source override_dh_auto_configure override_dh_auto_build override_dh_auto_install override_dh_strip override_dh_shlibdeps override_dh_auto_clean override_dh_auto_test
