#!/usr/bin/make -f

export DEB_HOST_ARCH      ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
export DEB_HOST_ARCH_OS   ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)
export DEB_HOST_ARCH_CPU  ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_CPU)

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    $(info Enable parallel builds with $(NUMJOBS) jobs)
    NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    MAKEFLAGS += -j$(NUMJOBS)
endif

COMMON_CONFIGURE_ARGS=--with-ladspa \
				--with-static-xg \
				--enable-snd-debug \
				--with-gmp \
				--prefix=/usr

ifeq ($(DEB_HOST_ARCH_OS),linux)
EXTRA_CONFIGURE_ARGS=--with-alsa
endif

ifeq ($(DEB_HOST_ARCH_OS),kfreebsd)
EXTRA_CONFIGURE_ARGS=--with-oss
endif

ifeq ($(DEB_HOST_ARCH_OS),hurd)
EXTRA_CONFIGURE_ARGS=--with-oss
endif

%:
	dh --with=autotools_dev $@

override_dh_auto_clean:
	dh_auto_clean
	rm -f sndlib.h
	rm -f po/Makefile po/Makefile.in po/POTFILES

override_dh_auto_configure:
override_dh_auto_build:
override_dh_auto_install:
	# First, build without GUI
	./configure $(COMMON_CONFIGURE_ARGS)    \
			$(EXTRA_CONFIGURE_ARGS) \
			--with-no-gui
	$(MAKE)
	dh_install -psnd-nox
	dh_installman -psnd-nox snd.1
	# Clean the tree
	$(MAKE) distclean

	# then, build with PulseAudio
	./configure $(COMMON_CONFIGURE_ARGS) \
			--with-gtk \
			--with-pulseaudio
	$(MAKE)
	dh_install -psnd-gtk-pulse
	dh_installman -psnd-gtk-pulse snd.1
	# Clean the tree
	$(MAKE) distclean

	# finally, build with JACK
ifneq (,$(findstring :$(DEB_HOST_ARCH_CPU):,:i386:powerpc:amd64:))
	./configure $(COMMON_CONFIGURE_ARGS) \
			--with-gtk \
			--with-jack
	$(MAKE)
	dh_install -psnd-gtk-jack
	dh_installman -psnd-gtk-jack snd.1
	# Clean the tree
	$(MAKE) distclean
endif

override_dh_install:
	dh_install -psnd
	dh_install -psnd-doc

override_dh_installchangelogs:
	dh_installchangelogs HISTORY.Snd
