#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS=hardening=+all
export DEB_CFLAGS_MAINT_APPEND=-fopenmp -ffunction-sections -fdata-sections
export DEB_CPPFLAGS_MAINT_APPEND = -D_FILE_OFFSET_BITS=64
export DEB_LDFLAGS_MAINT_APPEND=-Wl,--as-needed -Wl,--gc-sections
PERL_ARCHLIB := $(shell perl -MConfig -e 'print $$Config{vendorarch}')

MAKEOPTIONS = X_SYSTEM=Linux Q=

MAKE := $(MAKE) X_SYSTEM=Linux Q=

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 --with-php=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:
	sed -e 's,$${perl_archlib},$(PERL_ARCHLIB),g' debian/libexactimage-perl.install.in > debian/libexactimage-perl.install
	dh_auto_install -- $(MAKEOPTIONS) WITHPYTHON=0
	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

override_dh_strip:
	dh_strip -a --ddeb-migration='exactimage-dbg (<< 0.9.1-10~)'

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
