#!/usr/bin/make -f
#export DH_VERBOSE=1

DEB_HOST_ARCH_OS ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)

# Use this variable to set the PackageKit configure flags
# We set aptcc as default backend
PK_CONFIGURE_FLAGS = --disable-static \
		--disable-tests \
		--enable-gtk-doc \
		--disable-cron \
		--enable-introspection \
		--enable-apt --enable-aptcc --enable-smart \
		--disable-local \
		--with-default-backend=aptcc \
		--enable-browser-plugin \
		--enable-gtk-module \
		--enable-gstreamer-plugin \
		--enable-command-not-found

ifneq ($(DEB_HOST_ARCH_OS), linux)
	PK_CONFIGURE_FLAGS += --disable-networkmanager \
		--disable-connman
endif

LDFLAGS += -Wl,--as-needed

ifeq ($(shell dpkg-vendor --derives-from Ubuntu && echo yes), yes)
	SUBSTVARS = -Vdist:Browser="firefox (>> 3.0) | www-browser"
else
	SUBSTVARS = -Vdist:Browser="iceweasel (>> 3.0) | www-browser"
endif

# Define install target dir
INSTALLDIR = $(CURDIR)/debian/tmp

%:
	dh $@ --parallel --with python2

extrainstallfiles-stamp:
	cat debian/packagekit.install | grep -v '/pm-utils/' > debian/packagekit.install.kfreebsd
	cat debian/packagekit.install | grep -v '/pm-utils/' > debian/packagekit.install.hurd
	touch $@

override_dh_auto_configure:
	cp -dpr $(CURDIR)/docs/api $(CURDIR)/docs/api.orig
	# Remove Qt .moc-files, which break build
	rm $(CURDIR)/lib/packagekit-qt/src/*.moc
	dh_auto_configure -- $(PK_CONFIGURE_FLAGS)

override_dh_auto_install:
	dh_auto_install --destdir=$(INSTALLDIR)

	# These files do not need to be installed
	rm -f  $(INSTALLDIR)/usr/lib/packagekit-backend/*.la
	rm -f  $(INSTALLDIR)/usr/lib/mozilla/plugins/packagekit-plugin.la
	rm -f  $(INSTALLDIR)/usr/lib/gtk-?.0/modules/*.la
	rm -f  $(INSTALLDIR)/usr/lib/*.la
	rm -rf $(INSTALLDIR)/usr/lib/gtk-2.0
	rm -f  $(INSTALLDIR)/usr/bin/packagekit-bugreport.sh
	rm -f  $(INSTALLDIR)/usr/lib/libpackagekit-qt.*
	rm -rf $(INSTALLDIR)/usr/include/PackageKit/packagekit-qt
	rm -f  $(INSTALLDIR)/usr/lib/pkgconfig/packagekit-qt.pc

override_dh_install: extrainstallfiles-stamp
	dh_install --list-missing

override_dh_makeshlibs:
	dh_makeshlibs -Xlibpk-gtk-module -Xlibpk_ -Xpackagekit-plugin
	dh_girepository
	# Remove Qt .moc-files, which break build
	rm $(CURDIR)/lib/packagekit-qt/src/*.moc

override_dh_auto_clean:
	dh_auto_clean
	# Remove the autogenerated .install files for non-linux architectures
	rm -f debian/packagekit.install.kfreebsd
	rm -f debian/packagekit.install.hurd
	# Restore documentation
	if test -d $(CURDIR)/docs/api.orig; then \
		rm -rf $(CURDIR)/docs/api; \
		mv $(CURDIR)/docs/api.orig $(CURDIR)/docs/api; \
	fi

override_dh_gencontrol:
	dh_gencontrol -- $(SUBSTVARS)

override_dh_strip:
	dh_strip --dbg-package=packagekit-dbg
