#! /usr/bin/make -f

include /usr/share/quilt/quilt.make

###

# Configure arguments

confflags = --prefix=/usr --enable-static

DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

confflags += --build $(DEB_BUILD_GNU_TYPE)

# Only specify --host when cross-compiling
ifneq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
    confflags += --host $(DEB_HOST_GNU_TYPE)
endif

###

# Directory to make the build on

objdir = $(CURDIR)/obj-$(DEB_HOST_GNU_TYPE)

###

# CFLAGS

CFLAGS = -Wall -g

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

###

# Support non-Linux ports

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

ifneq (linux,$(DEB_HOST_ARCH_OS))
    DH_INSTALL_EXCLUDE := --exclude usr/lib/ao/plugins-2/libalsa09.
endif

###

$(objdir)/config.status: configure $(QUILT_STAMPFN)
	dh_testdir

	ln -sf /usr/share/misc/config.sub .
	ln -sf /usr/share/misc/config.guess .

	-mkdir $(objdir)
	cd $(objdir) && \
	env CFLAGS="$(CFLAGS)" ../configure $(confflags)

#

build: build-stamp
build-stamp: $(objdir)/config.status
	cd $(objdir) && \
	$(MAKE)

	touch $@

#

clean: unpatch
	dh_testdir
	dh_testroot

	rm -f build-stamp install-stamp
	rm -f config.guess config.sub

	# Remove build tree
	rm -rf $(objdir)

	dh_clean

#

install: install-stamp
install-stamp:
	dh_testdir
	dh_testroot
	dh_clean
	dh_installdirs
	cd $(objdir) && $(MAKE) install DESTDIR=$(CURDIR)/debian/tmp
	dh_install --list-missing $(DH_INSTALL_EXCLUDE)
	touch $@ 

#

binary: binary-arch

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs CHANGES
	dh_installdocs
	dh_installexamples
	dh_installmime
	dh_installman
	dh_link
	dh_strip --dbg-package=libao-dbg
	dh_compress
	dh_fixperms
	dh_makeshlibs -V
	dh_installdeb
	dh_shlibdeps --exclude=/usr/lib/ao/plugins-2
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary-indep:
	@echo "Nothing to do."

#

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