#!/usr/bin/make -f

include /usr/share/dpkg/architecture.mk

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

# TODO: try re-enable upstream optimizations
# disabled to test if cause of flaky multi-thread test (see bug#975696)
ifeq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
export DEB_CFLAGS_MAINT_STRIP=-O2
export DEB_CXXFLAGS_MAINT_STRIP=-O2
export DEB_CFLAGS_MAINT_APPEND=-O3
export DEB_CXXFLAGS_MAINT_APPEND=-O3
endif

no_java_archs = hppa hurd-i386 kfreebsd-amd64 kfreebsd-i386 m68k sh4
ifneq (,$(filter $(DEB_HOST_ARCH), $(no_java_archs)))
CONFIGURE_ARGS += --disable-java-bindings
endif

override_dh_auto_clean:
	dh_auto_clean
	-rm -rf bin/org
	-rm -f linkgrammar-*.jar

override_dh_autoreconf:
	dh_autoreconf --as-needed

override_dh_auto_configure:
	dh_auto_configure -- \
		--disable-static \
		--enable-pthreads \
		--with-hunspell-dictdir=/usr/share/hunspell \
		$(CONFIGURE_ARGS)

ifeq (,$(filter terse,$(DEB_BUILD_OPTIONS)))
override_dh_auto_test:
	dh_auto_test -- || { cat test/test-suite.log; false; }
endif

override_dh_install:
ifeq (,$(filter $(DEB_HOST_ARCH), $(no_java_archs)))
	mkdir -p debian/liblink-grammar-java/usr/lib/$(DEB_HOST_MULTIARCH)/jni
	mv debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/liblink-grammar-java.so* \
		debian/liblink-grammar-java/usr/lib/$(DEB_HOST_MULTIARCH)/jni
endif
	find debian/ -name "*.la" -delete
	dh_install -Xtests.py
	d-shlibmove --commit \
		--devunversioned \
		--exclude-la \
		--exclude-a \
		--multiarch \
		--movedev "debian/tmp/usr/include/*" usr/include/ \
		--movedev "debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig/*" \
			usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig \
		debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/*.so

override_dh_compress:
	dh_compress --exclude=.py

override_dh_makeshlibs:
	dh_makeshlibs -- -c4

%:
	dh $@
