#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

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

include /usr/share/dpatch/dpatch.make

# 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)
DEB_HOST_ARCH_OS    ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)
DEB_HOST_ARCH       ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_BUILD_ARCH_CPU  ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH_CPU)

#biarch_map := i386=amd64 powerpc=ppc64 sparc=sparc64 s390=s390x \
#		amd64=i386 ppc64=powerpc
#biarch_cpu := $(strip $(patsubst $(DEB_BUILD_ARCH_CPU)=%, %, \
#			   $(filter $(DEB_BUILD_ARCH_CPU)=%, $(biarch_map))))
#
#ifneq (,$(biarch_cpu))
#  ifneq (,$(findstring /$(DEB_HOST_ARCH)/, /i386/powerpc/sparc/s390/))
#    bi = 64
#    bilibdir = usr/lib64
#  endif
#  ifneq (,$(findstring /$(DEB_HOST_ARCH)/, /amd64/ppc64/))
#    bi = 32
#    bilibdir = usr/lib32
#  endif
#  ifneq (,$(bi))
#    configure_biarch_stamp = configure-biarch-stamp
#    build_biarch_stamp = build-biarch-stamp
#    BIARCH_CC = gcc -m$(bi)
#    dpkg_ctrl_args = -- '-Vbilib:depends=libc6-$(biarch_cpu)' \
#	'-Vbidev:depends=libc6-dev-$(biarch_cpu)'
#  endif
#endif

CFLAGS = -Wall -g -D_REENTRANT
ALSA_CFLAGS = -g -Wall

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

ifeq (linux,$(DEB_HOST_ARCH_OS))
	CONFIGURE_SOUND_ARGS = --with-oss=yes --with-alsa=yes
	DH_GENCONTROL_ARGS = -Valsa-dev="libasound2-dev"
else
	CONFIGURE_SOUND_ARGS = --with-oss=yes --with-alsa=no
endif

#
# pa_ringbuffer required for jack support is only guaranteed
# SMP safe on i386 and powerpc
# NOTE: 20071207 should now be safe on amd64, so its enabled for now.
#

ENABLE_JACK = no

ifeq (yes,$(ENABLE_JACK))
	DH_GENCONTROL_ARGS += -Vjack-dev="libjack0.100.0-dev"
endif

CONFIGURE_SOUND_ARGS += --with-jack=no

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

#######################################################




###############################################################


CFLAGS = -Wall -g -D_REENTRANT

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

ifeq (linux,$(DEB_HOST_ARCH_OS))
	CONFIGURE_SOUND_ARGS = --with-oss=yes --with-alsa=yes
	DH_GENCONTROL_ARGS = -Valsa-dev="libasound2-dev"
else
	CONFIGURE_SOUND_ARGS = --with-oss=yes --with-alsa=no
endif

#
# pa_ringbuffer required for jack support is only guaranteed
# SMP safe on i386 and powerpc
# NOTE: 20071207 should now be safe on amd64, so its enabled for now.
#
ENABLE_JACK = no
ifneq (, $(filter $(DEB_HOST_ARCH), i386 lpia amd64))
	ENABLE_JACK = no
else
ifeq (powerpc,$(DEB_HOST_ARCH))
	ENABLE_JACK = no
endif
endif
#ENABLE_JACK = no

ifeq (yes,$(ENABLE_JACK))
	DH_GENCONTROL_ARGS += -Vjack-dev="libjack0.100.0-dev"
endif

CONFIGURE_SOUND_ARGS += --with-jack=no

DEBVERSION:=$(shell head -n 1 debian/changelog \
		    | sed -e 's/^[^(]*(\([^)]*\)).*/\1/')
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/branches/v19-devel
SRCDIR := portaudio19-$(UPVERSION).orig

autoreconf:
	aclocal
	autoconf
	libtoolize -c
	$(RM) -r autom4te.cache

config: patch config.status
	cd ./alsa-lib/ && ./configure --prefix=/usr \
		    --mandir=\$${prefix}/share/man \
		    --infodir=\$${prefix}/share/info \
		    --datadir=\$${prefix}/share \
		    --build=$(DEB_BUILD_GNU_TYPE) \
		    --disable-python \
		    --enable-static --disable-maintainer-mode
	

config.status:
	dh_testdir
	CFLAGS="$(CFLAGS)"  LDFLAGS="-Lalsa-lib/include" ./configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info --enable-cxx $(CONFIGURE_SOUND_ARGS)

build: config build-stamp
build-stamp:
	dh_testdir
	$(MAKE) lib/libportaudio.la
	$(MAKE) -C bindings/cpp
	doxygen
	touch build-stamp

clean:
	-cd ./alsa-lib/ && $(MAKE) clean
	
	dh_testdir
	dh_testroot
	rm -f build-stamp 
	[ ! -f bindings/cpp/doc/Makefile ]||$(MAKE) -C bindings/cpp/doc distclean
	[ ! -f bindings/cpp/Makefile ]||$(MAKE) -C bindings/cpp distclean
	[ ! -f Makefile ]||$(MAKE) distclean
	$(RM) -r lib/.libs
	$(RM) doc/html/*
	$(MAKE) -f $(CURDIR)/debian/rules unpatch
	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs
	
	install -d $(CURDIR)/debian/tmp/usr/lib
	install -d $(CURDIR)/debian/tmp/usr/include
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp

# Build architecture-independent files here.
binary-indep: build install
	cd ./alsa-lib/ && $(MAKE) CFLAGS="$(ALSA_CFLAGS)"	
	dh_testdir -i
	dh_testroot -i
	dh_installchangelogs -i
	dh_installdocs -i
	dh_installexamples -i
	dh_install -i --sourcedir=debian/tmp
	dh_compress -i --exclude=usr/share/doc/portaudio19-doc/docs
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build install
	cd ./alsa-lib/ && $(MAKE) CFLAGS="$(ALSA_CFLAGS)"	
	dh_testdir -a
	dh_testroot -a
	dh_installchangelogs -a
	dh_installdocs -a
	dh_install -a --sourcedir=debian/tmp --list-missing
	dh_strip -a
	dh_compress -a --exclude=usr/share/doc/portaudio19-doc/docs
	dh_fixperms -a
	dh_makeshlibs -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a -- $(DH_GENCONTROL_ARGS)
	dh_md5sums -a
	dh_builddeb -a

print-version:
	@@echo "Debian version:          $(DEBVERSION)"
	@@echo "Upstream version:        $(UPVERSION)"

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

binary: binary-indep binary-arch
.PHONY: alsa_dep build clean binary-indep binary-arch binary install patch unpatch config autoreconf print-version get-orig-source
