#!/usr/bin/make -f

#export DH_VERBOSE=1

export DEB_BUILD_MAINT_OPTIONS := hardening=+all

export DEB_CFLAGS_MAINT_APPEND  := -pipe -Wall
# bug #715461, provide default soundfont paths
export DEB_CPPFLAGS_MAINT_APPEND := -DMIX_INIT_SOUNDFONT_PATHS="\"/usr/share/sounds/sf3/default-GM.sf3\""

include /usr/share/dpkg/architecture.mk


# disable autoheader (invoked automatically by autoreconf), necessary in order
# to use debhelper compat level v10 without overriding dh-autoreconf calls
export AUTOHEADER := /bin/true
# use correct invocation of aclocal for autoreconf
export ACLOCAL := aclocal --force --install -I /usr/share/aclocal/ -I acinclude

CC ?= $(DEB_HOST_GNU_TYPE)-gcc

# explicitly enable/disable all supported sound
#
# issue "disable-*-shared" flags for 'configure', otherwise SDL_mixer library
# opens those libraries in a plugin-like way (with SDL_LoadObject), and
# dpkg-shlibdeps doesn't seem to generate the needed dependencies in the binary
# library package
#
# enable external music player
confflags := --enable-music-cmd
# flac
confflags += --enable-music-flac
confflags += --enable-music-flac-libflac
confflags += --disable-music-flac-libflac-shared
confflags += --disable-music-flac-drflac
# game music emulation: disabled until someone needs it
confflags += --disable-music-gme
# MIDI
confflags += --enable-music-midi-fluidsynth
confflags += --enable-music-midi-fluidsynth-shared
confflags += --enable-music-midi-timidity
confflags += --enable-music-midi-native
# MOD
confflags += --enable-music-mod
confflags += --disable-music-mod-modplug
confflags += --enable-music-mod-xmp
confflags += --disable-music-mod-xmp-lite
confflags += --disable-music-mod-xmp-shared
# MP3
confflags += --enable-music-mp3
confflags += --enable-music-mp3-mpg123
confflags += --disable-music-mp3-mpg123-shared
confflags += --disable-music-mp3-minimp3
# ogg
confflags += --enable-music-ogg
confflags += --enable-music-ogg-vorbis
confflags += --disable-music-ogg-vorbis-shared
confflags += --disable-music-ogg-tremor
confflags += --disable-music-ogg-stb
# opus
confflags += --enable-music-opus
confflags += --disable-music-opus-shared
# wav
confflags += --enable-music-wave
# WavPack: disabled until someone needs it
confflags += --disable-music-wavpack
confflags += --disable-music-wavpack-dsd
confflags += --disable-music-wavpack-shared

# install manual tests to a multiarch-friendly location, since we don't have
# a libsdl2-mixer-tests package
confflags += --bindir=/usr/lib/$(DEB_HOST_MULTIARCH)/installed-tests/SDL2_mixer

%:
	dh $@

override_dh_auto_configure:
	dh_auto_configure -- $(confflags)

override_dh_auto_install:
	dh_auto_install -- install install-bin

override_dh_autoreconf:
	$(ACLOCAL)
	dh_autoreconf

override_dh_installchangelogs:
	dh_installchangelogs -- CHANGES.txt

override_dh_link:
	# fix lintian warning dev-pkg-without-shlib-symlink
	dh_link -plibsdl2-mixer-dev usr/lib/$(DEB_HOST_MULTIARCH)/libSDL2_mixer-2.0.so.0 usr/lib/$(DEB_HOST_MULTIARCH)/libSDL2_mixer-2.0.so
	dh_link --remaining-packages

override_dh_compress:
	dh_compress -Xplaymus.c -Xplaywave.c

debian/shlibdeps-helper.so: debian/shlibdeps-helper.c debian/rules
	$(CC) -shared $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -Wl,--no-as-needed \
		-o$@ $< \
		$$(pkg-config --cflags --libs fluidsynth)

execute_after_dh_shlibdeps-arch: debian/shlibdeps-helper.so
	dh_shlibdeps -plibsdl2-mixer-2.0-0 -- --warnings=0 -pdlopen -dRecommends -e$<
