#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

export CFLAGS := $(shell dpkg-buildflags --get CFLAGS) $(shell dpkg-buildflags --get CPPFLAGS)
export CXXFLAGS := $(shell dpkg-buildflags --get CXXFLAGS) $(shell dpkg-buildflags --get CPPFLAGS)
export LDFLAGS := $(shell dpkg-buildflags --get LDFLAGS) -Wl,--as-needed
export QT_SELECT := qt5

# Filter -g from environment on troublesome arches. Replace it with -gstabs
# See also: stabs_format_debug_info.diff
DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
ifneq (,$(filter $(DEB_HOST_ARCH),s390 s390x armel armhf mips mipsel m68k))
    export DEB_CFLAGS_MAINT_STRIP := -g
    export DEB_CFLAGS_MAINT_APPEND := -gstabs
    export DEB_CXXFLAGS_MAINT_STRIP := -g
    export DEB_CXXFLAGS_MAINT_APPEND := -gstabs
endif
ifneq (,$(filter $(DEB_HOST_ARCH),alpha))
    export DEB_LDFLAGS_MAINT_APPEND := -Wl,--no-relax
endif

%:
	dh $@ --parallel --dbg-package=libqt5webkit5-dbg --with pkgkde_symbolshelper

override_dh_auto_configure:
	qmake WEBKIT_CONFIG-=use_webp

override_dh_auto_build-arch:
	# This override may seem futile, but it helps us make sure that the tools
	# needed for building the docs are built before calling them.
	# If there's a better way, do not heasitate to tell us.
	dh_auto_build

override_dh_auto_build-indep:
	dh_auto_build -Smakefile -- docs

override_dh_auto_install-arch:
	make install INSTALL_ROOT=$(CURDIR)/debian/tmp
	
	# Remove rpath from the offending binaries
	chrpath -d $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/qt5/libexec/QtWebProcess
	chrpath -d $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/qt5/libexec/QtWebPluginProcess
	
	# Fix wrong path in pkgconfig files
	find $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig -type f -name '*.pc' \
	-exec sed -i -e 's/$(DEB_HOST_MULTIARCH)\/$(DEB_HOST_MULTIARCH)/$(DEB_HOST_MULTIARCH)/g' {} \;
	
	# Remove libtool-like files
	rm -f debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/*.la

override_dh_auto_install-indep:
	make INSTALL_ROOT=$(CURDIR)/debian/tmp install_docs

override_dh_install:
	dh_install --fail-missing

override_dh_builddeb:
	dh_builddeb -- -Zxz
