#!/usr/bin/make -f

# 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)

CFLAGS = -Wall -g -D_REENTRANT

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

%:
	dh $@

override_dh_auto_clean:
	[ ! -f Makefile ] || $(MAKE) distclean
	[ ! -f bindings/cpp/Makefile ] || $(MAKE) distclean -C bindings/cpp
	rm -f bindings/cpp/doc/Makefile bindings/cpp/libtool doc/html/*

override_dh_auto_configure:
	CFLAGS="$(CFLAGS)" ./configure --enable-cxx --prefix=/usr \
		--host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE)

override_dh_auto_build:
	$(MAKE)
	doxygen

DEBVERSION := $(shell dpkg-parsechangelog | egrep '^Version:' | cut -f 2 -d ' ')
UPVERSION:=$(shell echo $(DEBVERSION) | sed -e 's/^.*://' -e 's/-[0-9.]*$$//' -e 's/.dfsg$$//')
UPDATE:=$(shell echo $(UPVERSION) | sed -e 's/^.*\+svn//')

FILENAME := portaudio19_$(UPVERSION).orig.tar.gz
URL := https://www.portaudio.com/repos/portaudio/trunk
SRCDIR := portaudio19-$(UPVERSION)

get-orig-source:
	@dh_testdir
	@mkdir -p ../tarballs
	@echo Downloading $(FILENAME) from $(URL) ...
	@svn export -r{$(UPDATE)T0000Z} $(URL) ../tarballs/$(SRCDIR)
	@echo Building snapshot tarball
	@GZIP=-9 tar -caf ../tarballs/$(FILENAME) -C ../tarballs $(SRCDIR)
	@echo Cleaning up
	@rm -rf ../tarballs/$(SRCDIR)
