#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

upstream_version ?= $(shell dpkg-parsechangelog | sed -rne 's/^Version: ([0-9.]+)(\+dfsg\d+)?.*$$/\1/p')
dfsg_version = $(upstream_version)~ds0
pkg = $(shell dpkg-parsechangelog | sed -ne 's/^Source: //p')

DEB_HOST_ARCH_OS ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
CONFIGURE_FLAGS = --enable-debug --with-jack --with-pulse

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

ifeq ($(DEB_HOST_ARCH_OS),linux)
CONFIGURE_FLAGS += --with-alsa
else
CONFIGURE_FLAGS += --with-oss
endif

%:
	dh $@ --parallel --with autoreconf,autotools-dev

override_dh_auto_clean:
	dh_auto_clean
	rm -rf doc/html doc/images

override_dh_auto_configure:
	dh_auto_configure -- $(CONFIGURE_FLAGS)

override_dh_installchangelogs:
	dh_installchangelogs doc/release.txt

# get-orig-source to drop doc and win files
get-orig-source:
	uscan --noconf --force-download --rename --download-current-version --destdir=.
	tar -xf $(pkg)_$(upstream_version).orig.tar.gz
	mv $(pkg)-$(upstream_version) $(pkg)-$(dfsg_version)
	rm -fR $(pkg)-$(dfsg_version)/configure/
	rm -fR $(pkg)-$(dfsg_version)/autom4te.cache/
	rm -fR $(pkg)-$(dfsg_version)/contrib/
	rm -fR $(pkg)-$(dfsg_version)/doc/html/
	rm -fR $(pkg)-$(dfsg_version)/doc/images/
	rm -fR $(pkg)-$(dfsg_version)/include/
	rm -fR $(pkg)-$(dfsg_version)/tests/Windows/
	tar cf $(pkg)_$(dfsg_version).orig.tar $(pkg)-$(dfsg_version)
	xz -zf $(pkg)_$(dfsg_version).orig.tar
	rm -rf $(pkg)-$(dfsg_version)
	rm -rf $(pkg)_$(upstream_version).orig.tar.gz
	mv $(pkg)_$(dfsg_version).orig.tar.xz ../$(pkg)_$(dfsg_version).orig.tar.xz
