#!/usr/bin/make -f

DEB_HOST_GNU_TYPE  := $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE := $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
CFLAGS := $(shell dpkg-buildflags --get CFLAGS)
CPPFLAGS := $(shell dpkg-buildflags --get CPPFLAGS)
LDFLAGS := $(shell dpkg-buildflags --get LDFLAGS)
CFLAGS += -Wall

# if this is a linux-any architecture, give no option
# (currently the only way to enable alsa (--enable-alsa disables it))
# otherwise, give --disable-alsa, so configure succeeds without libasound
ALSA_FLAGS := $(shell dpkg-architecture -ilinux-any || printf "%s" "--disable-alsa")

ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
	CROSS= --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
else
	CROSS= --build $(DEB_BUILD_GNU_TYPE)
endif

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp
	rm -f config.guess config.sub
	rm -f doc/sweep.1
	[ ! -f Makefile ] || $(MAKE) distclean
	dh_autoreconf_clean
	dh_clean

config.status: configure
	dh_testdir
	dh_autoreconf
	./configure $(CROSS) --prefix=/usr --mandir=\$${prefix}/share/man \
		$(ALSA_FLAGS) \
		CPPFLAGS='$(CPPFLAGS)' CFLAGS='$(CFLAGS)' LDFLAGS='$(LDFLAGS)'

build-indep:
build-arch: build-stamp
build: build-stamp

build-stamp: config.status
	$(MAKE)
	touch build-stamp

install: build-stamp
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	$(MAKE) prefix=$(CURDIR)/debian/sweep/usr install
	mkdir -p debian/sweep-dev/usr
	mv debian/sweep/usr/include debian/sweep-dev/usr

binary: binary-arch binary-indep

binary-arch: install
	dh_testdir -a
	dh_testroot -a
	dh_installchangelogs -a ChangeLog
	dh_installdocs -a
	dh_install -a
	dh_installmenu -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary-indep: install
	dh_testdir -i
	dh_testroot -i
	dh_installchangelogs -i ChangeLog
	dh_installdocs -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

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