#!/usr/bin/make -f
# -*- makefile -*-

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

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
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)

CFLAGS = -Wall -g

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif

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

DEB_QUILT_PATCHES = debian/patches

patch: debian/patch-stamp
debian/patch-stamp:
	QUILT_PATCHES=$(DEB_QUILT_PATCHES) quilt push -a || test $$? = 2

	touch debian/patch-stamp

unpatch: 
	QUILT_PATCHES=$(DEB_QUILT_PATCHES) quilt pop -a -R || test $$? = 2 

	rm -rf ./.pc
	rm -f debian/patch-stamp

configure: debian/configure-stamp
debian/configure-stamp: debian/patch-stamp
	dh_testdir

	chmod +x ./autogen.sh
	AUTOMAKE=automake ./autogen.sh

	rm -f config.cache config.guess config.sub
	ln -sf /usr/share/misc/config.sub .
	ln -sf /usr/share/misc/config.guess .

	# Some problem with pkg-config so we force XFT_LIBS and RSS_LIBS
	XFT_LIBS="-lXft" RSS_LIBS="-lxml2 -lcurl" ./configure \
	  --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) \
	  --prefix=/usr --sysconfdir=/etc \
	  --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info \
	  --enable-debug --enable-rss $(EXTRA_CONF_ARGS) \
          --enable-rss --enable-wlan --enable-xft --datarootdir=/opt/conky/share
	
	touch debian/configure-stamp

build: debian/build-stamp
debian/build-stamp: debian/configure-stamp debian/patch-stamp
	dh_testdir

	$(MAKE)

	touch debian/build-stamp

clean: unpatch
	dh_testdir
	dh_testroot
	rm -f debian/build-stamp debian/configure-stamp
	[ ! -f Makefile ] || $(MAKE) clean
	[ ! -f Makefile ] || $(MAKE) distclean
	
	# Remove autoconf generated files
	rm -f ./config.guess ./config.sub ./data/Makefile.in \
	./configure ./configure.ac ./doc/Makefile.in \
	./ltmain.sh ./Makefile.in \
	./src/config.h.in ./src/Makefile.in ./aclocal.m4 \
	./m4/libtool.m4 ./m4/ltversion.m4 ./m4/lt~obsolete.m4 \
	./m4/ltoptions.m4 ./m4/ltsugar.m4

	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs
	# perl -pi -e 'BEGIN{undef $$/;} s/\.SH COMPILING.*?(\.SH.*)/$$1/sg' doc/conky.1
	$(MAKE) install DESTDIR=$(CURDIR)/debian/conky

binary-indep: build install

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs ChangeLog
	dh_installdocs
	dh_installexamples doc/conky.conf
	dh_installmenu
	dh_installman doc/conky.1
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install configure patch reverse-patch
