#!/usr/bin/make -f
include /usr/share/dpkg/architecture.mk

WITH_TREMOR=--without-tremor --enable-vorbis --disable-shine-encoder
ifeq ($(DEB_HOST_ARCH), arm)
WITH_TREMOR=--with-tremor --disable-vorbis --enable-shine-encoder
endif
ifeq ($(DEB_HOST_ARCH), armel)
WITH_TREMOR=--with-tremor --disable-vorbis --enable-shine-encoder
endif
ifeq ($(DEB_HOST_ARCH), armeb)
WITH_TREMOR=--with-tremor --disable-vorbis --enable-shine-encoder
endif

# work around ld assertion fail ../../bfd/elf-strtab.c:254/:272 on mips/mipsel
RUN_TESTS=--enable-test
ifeq ($(DEB_HOST_ARCH), mips)
    RUN_TESTS=--disable-test
endif
ifeq ($(DEB_HOST_ARCH), mipsel)
    RUN_TESTS=--disable-test
endif
ifeq ($(DEB_HOST_ARCH), mips64el)
    RUN_TESTS=--disable-test
endif

# no systemd and no alsa on non-linux arches
ifeq ($(DEB_HOST_ARCH_OS), linux)
WITH_SYSTEMD=--enable-systemd --with-systemdsystemunitdir=/lib/systemd/system --enable-alsa
else
WITH_SYSTEMD=--disable-systemd --disable-alsa
endif


###############################################################
## Define Debian feature set (in configure summary order)

# Archive support
ENABLE_ARCHIVES=--enable-bzip2 \
	        --enable-iso9660 \
		--enable-zlib \
		--enable-zzip

# Autodiscovery support
ENABLE_AUTODISCOVERY=--enable-neighbor-plugins \
		     --enable-nfs \
		     --enable-smbclient \
		     --enable-upnp \
		     --with-zeroconf=avahi

# Client support
ENABLE_CLIENTS=--enable-ipv6 \
	       --enable-tcp \
	       --enable-un

# File format support
# Note: OggTremor / OggVorbis are in WITH_TREMOR
ENABLE_FILEFORMATS=--enable-aac \
		   --enable-adplug \
		   --enable-sidplay \
		   --enable-ffmpeg \
		   --enable-flac \
		   --enable-fluidsynth \
		   --enable-gme \
		   --enable-sndfile \
		   --enable-mikmod \
		   --enable-modplug \
		   --enable-mad \
		   --enable-mpg123 \
		   --enable-mpc \
		   --enable-opus \
		   --enable-audiofile \
		   --enable-wavpack \
		   --enable-wildmidi

# Other features
ENABLE_OTHER=--with-boost \
	     --enable-database \
	     --enable-expat \
	     --enable-glib \
	     --enable-icu \
	     --enable-lsr \
	     --enable-libmpdclient \
	     --enable-inotify \
	     --enable-soxr \
	     --enable-sqlite

# Metadata support
ENABLE_METADATA=--enable-id3

# Playback support
# Note: ALSA is in WITH_SYSTEMD
ENABLE_PLAYBACKS=--enable-fifo \
		 --enable-recorder-output \
		 --enable-httpd-output \
		 --enable-jack \
		 --enable-ao \
		 --enable-oss \
		 --enable-openal \
		 --enable-pipe-output \
		 --enable-pulse \
		 --enable-roar \
		 --enable-shout

# Streaming encoder support
# Note: FLAC and Opus enabled under file formats
ENABLE_ENCODERS= --enable-lame-encoder \
		--enable-vorbis-encoder \
		--enable-wave-encoder

# Streaming support
ENABLE_STREAMING=--enable-cdio-paranoia \
		 --enable-curl \
		 --enable-soundcloud \
		 --enable-mms

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


export DEB_BUILD_MAINT_OPTIONS = hardening=+all
LDFLAGS += -Wl,--as-needed

%:
	dh $@ --with autoreconf --with=systemd --parallel

override_dh_auto_configure:
	dh_auto_configure -- $(WITH_TREMOR) $(WITH_SYSTEMD) \
		$(RUN_TESTS) \
		$(ENABLE_ARCHIVES) \
		$(ENABLE_AUTODISCOVERY) \
		$(ENABLE_CLIENTS) \
		$(ENABLE_FILEFORMATS) \
		$(ENABLE_OTHER) \
		$(ENABLE_METADATA) \
		$(ENABLE_PLAYBACKS) \
		$(ENABLE_ENCODERS) \
		$(ENABLE_STREAMING)

override_dh_auto_build:
	dh_auto_build
	# from the available documentation, generate just the user manual, as one html file
	/usr/bin/xmlto --stringparam chunker.output.encoding=utf-8 html-nochunks $(CURDIR)/doc/user.xml
	mv user.html user-manual.html

override_dh_auto_install:
	dh_auto_install
	test -f $(CURDIR)/debian/mpd/lib/systemd/system/mpd.service && \
	    install -Dm644 $(CURDIR)/debian/mpd.service.user $(CURDIR)/debian/mpd/usr/lib/systemd/user/mpd.service

override_dh_strip:
	# can be dropped after Stretch
	dh_strip  --ddeb-migration='mpd-dbg (<< 0.19.14-1~)'

override_dh_installchangelogs:
	dh_installchangelogs NEWS
