#!/usr/bin/make -f

export DEB_LDFLAGS_MAINT_APPEND=-Wl,--as-needed

DEB_CFLAGS          ?= $(shell dpkg-buildflags --get CFLAGS)
DEB_LDFLAGS         ?= $(shell dpkg-buildflags --get LDFLAGS)

DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_ARCH_OS    ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)
DEB_HOST_ARCH       ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

ifeq ($(DEB_HOST_ARCH_OS),linux)
       LINUX_CONF_ARGS_ALL=--enable-wlan --enable-ibm
endif

ifeq ($(DEB_HOST_ARCH),i386)
ifeq ($(DEB_HOST_ARCH_OS),linux)
       ENABLE_NVIDIA=--enable-nvidia
endif
endif

ifeq ($(DEB_HOST_ARCH),amd64)
ifeq ($(DEB_HOST_ARCH_OS),linux)
       ENABLE_NVIDIA=--enable-nvidia
endif
endif

COMMON_CONFIGURE_FLAGS = --host=$(DEB_HOST_GNU_TYPE) \
                         --build=$(DEB_BUILD_GNU_TYPE) \
                         --prefix=/usr --sysconfdir=/etc \
                         --mandir=\$${prefix}/share/man \
                         --infodir=\$${prefix}/share/info \
                         --srcdir=.. \
                         --disable-static 

%:
	dh $@ --with autotools_dev

override_dh_clean:
	rm -rf build-all
	dh_clean

override_dh_auto_configure:
	mkdir -p build-all
	
	cd build-all && CFLAGS="$(DEB_CFLAGS)" LDFLAGS="$(DEB_LDFLAGS)" \
	../configure $(COMMON_CONFIGURE_FLAGS) \
	--enable-imlib2 --enable-rss --enable-weather-xoap \
	--enable-eve --enable-lua-cairo --enable-lua-imlib2 \
	--enable-xmms2 --enable-audacious \
	$(LINUX_CONF_ARGS_ALL) $(ENABLE_NVIDIA)

override_dh_auto_build:
	cd build-all && $(MAKE)

override_dh_auto_install:
	cd build-all && $(MAKE) install DESTDIR=$(CURDIR)/debian/conky-all
	
	# Clean up cruft left upstream
	rm -rf debian/conky-all/usr/lib/conky/*.la \
	       debian/conky-all/usr/lib/conky/*.so.?

override_dh_makeshlibs:
	# Do not run ldconfig on private shared libraries in /usr/lib/conky
	# This fixes lintian warning "postinst-has-useless-call-to-ldconfig"
	dh_makeshlibs -X/usr/lib/conky
