#!/usr/bin/make -f

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

# This has to be exported to make some magic below work.
export DH_OPTIONS

CFLAGS = -Wall -g

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

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp extract-stamp install-stamp

	if [ -f Makefile ]; \
	then\
		if [ -d .svn ]; \
		then \
			$(MAKE) maintainer-clean; \
		else \
			$(MAKE) distclean; \
		fi; \
	fi

	dh_clean

update:
	dh_testdir
	-test -r /usr/share/misc/config.sub && \
		cp -f /usr/share/misc/config.sub config.sub
	-test -r /usr/share/misc/config.guess && \
		cp -f /usr/share/misc/config.guess config.guess

configure: configure-stamp
configure-stamp:
	dh_testdir
	[ -f ./configure ] || ./bootstrap
	CFLAGS="$(CFLAGS)" ./configure \
		--prefix=/usr --mandir=\$${prefix}/share/man \
		--infodir=\$${prefix}/share/info --enable-shared \
		--enable-doxygen --enable-html-docs \
		--sysconfdir=/etc --enable-all-components \
		--disable-gr-audio-osx --disable-gr-audio-windows \
		--disable-gr-qtgui
	touch configure-stamp

extract: extract-stamp
extract-stamp: configure-stamp debian/gen-install-files.sh
	dh_testdir
	debian/gen-install-files.sh 
	touch $@

build: build-stamp
build-stamp: extract-stamp

	$(MAKE) 
	$(MAKE) check
	touch $@

install: install-stamp
install-stamp: build-stamp
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp
	: # make python scripts starting with '#!' executable
	for i in `find debian -mindepth 2 -type f ! -perm 755`; do \
	  if head -1 $$i | grep -q '^#!'; then \
	    chmod 755 $$i; \
	    echo "made executable: $$i"; \
	  fi; \
	done

	dh_install --sourcedir=debian/tmp

# FIXME: Ubuntu vs. Debian (below)
#	install -m 0755 debian/usrp.hotplug \
#		$(CURDIR)/debian/usrp/etc/hotplug.d/usb/

	touch $@


# Must not depend on anything. This is to be called by
# binary-arch/binary-indep
# in another 'make' thread.
binary-common:
	dh_testdir
	dh_testroot
	dh_installchangelogs ChangeLog
	dh_installdocs
	dh_pycentral
	dh_link
	dh_strip
	dh_compress 
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture independant packages using the common target.
binary-indep: build install
	$(MAKE) -f debian/rules DH_OPTIONS=-i binary-common

# Build architecture dependant packages using the common target.
binary-arch: build install
	$(MAKE) -f debian/rules DH_OPTIONS=-s binary-common

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