#!/usr/bin/make -f

DEB_BUILDDIR = build
include /usr/share/cdbs/1/class/python-autotools.mk
include /usr/share/cdbs/1/rules/debhelper.mk

include /usr/share/cdbs/1/rules/autoreconf.mk

# TODO: convince upstream to install headers, and provide proper -dev package
stem = sugarext
abi = 0
lib = lib$(stem)
pkg-lib = lib$(stem)$(abi)
pkg-gir = gir1.2-$(stem)-1.0
pkg-data = lib$(stem)-data
pkg-python = python-sugar3

# Multiarch quirks
DEB_CONFIGURE_EXTRA_FLAGS += --libdir=\$${prefix}/lib/$(DEB_HOST_MULTIARCH)

# optimize linking
LDFLAGS += -Wl,-O1
# TODO: disallow undefined symbols
#LDFLAGS += -Wl,-z,defs
# link only when needed
LDFLAGS += -Wl,--as-needed

DEB_DH_INSTALL_ARGS_ALL = --autodest
DEB_DH_INSTALL_ARGS_$(pkg-python) = usr/bin usr/lib/python*
DEB_DH_INSTALL_ARGS_$(pkg-gir) = usr/lib/$(DEB_HOST_MULTIARCH)/girepository-1.0
DEB_DH_INSTALL_ARGS_$(pkg-data) = usr/share/locale

# Drop created dir if empty to please lintian
binary-post-install/$(pkg-data)::
	rmdir --ignore-fail-on-non-empty --parents $(DEB_DESTDIR)/usr/share/locale

# Let d-shlibs calculate development package dependencies
#  and handle shared library install
binary-post-install/$(pkg-lib):: debian/stamp-local-shlibs-$(lib)
debian/stamp-local-shlibs-$(lib): binary-install/$(pkg-lib)
	d-shlibmove --commit \
		--multiarch \
		--exclude-la --exclude-a \
		--devunversioned --ignorelibdep \
		--movedev debian/tmp/usr/share/gir-1.0 usr/share/ \
		--extralib debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libsugar-eventcontroller.so \
		debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/$(lib).so
	touch $@
clean::
	rm -f debian/stamp-local-shlibs-$(lib)

# Ensure proper hash-bang in Python script (Python Policy 0.9 §3.1)
#  * TODO: drop cdbs_python_binary fallback when cdbs 0.5 is stable
binary-fixup/$(pkg-python)::
	egrep -r -l -Z '^#! ?/usr/bin/(env )?python[[:print:]]*$$' \
		debian/$(cdbs_curpkg)/usr/bin/* \
		| xargs -r -0 perl -pi -e 's,^#! ?/usr/bin/(env )?python[[:print:]]*$$,#!/usr/bin/$(strip $(or $(cdbs_curpythonruntime),$(cdbs_python_binary))),'

# Fix favor ISO 639-1 when differing from ISO 639-2
binary-post-install/$(pkg-data)::
	mv debian/$(cdbs_curpkg)/usr/share/locale/ibo \
		debian/$(cdbs_curpkg)/usr/share/locale/ig
