#!/usr/bin/make -f

# Pass -Wall and some other compiler options we wish to use.
CFLAGS = -Wall $(shell dpkg-buildflags --get CFLAGS 2>/dev/null | sed -e 's/-g\|-O2//g')
CXXFLAGS = -Wall $(shell dpkg-buildflags --get CXXFLAGS 2>/dev/null | sed -e 's/-g\|-O2//g')

# nostrip option implies noopt
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
   TYPE=Release
else
   TYPE=Debug
endif

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

# Use this variable to allow options passed to cmake to be overridable
DEB_CMAKE_OPTIONS ?= -DCMAKE_VERBOSE_MAKEFILE=ON \
		-DCMAKE_BUILD_TYPE=$(TYPE) \
		-DCMAKE_INSTALL_PREFIX="/usr" \
		-DCMAKE_C_FLAGS="$(CFLAGS)" \
		-DCMAKE_CXX_FLAGS="$(CXXFLAGS)" \
		-DCMAKE_CXX_COMPILER="g++" \
		-DLIB_SUFFIX="/$(DEB_HOST_MULTIARCH)"

%:
	dh $@ --parallel

override_dh_auto_clean:
	rm -rf build/*

override_dh_auto_configure:
	cd build && cmake $(DEB_CMAKE_OPTIONS) ..

override_dh_auto_build:
	$(MAKE) --directory=build

override_dh_auto_install:
	$(MAKE) --directory=build install DESTDIR=$(CURDIR)/debian/tmp

override_dh_shlibdeps: debian/tmp/alure-dummy.so
	dh_shlibdeps -O--parallel
	# Need to manually add dependencies for dlopened libs.
	dpkg-shlibdeps -dRecommends -edebian/tmp/alure-dummy.so -xlibc6 -O >>debian/libalure1.substvars

debian/tmp/alure-dummy.so:
	mkdir -p debian/tmp
	cc -xc -shared -Wl,--no-as-needed -o $@ /dev/null \
		-lsndfile \
		-lvorbisfile \
		-lFLAC \
		-lmpg123 \
		-ldumb \
		-lmodplug \
		-lfluidsynth

get-orig-source:
	$(dir $_)alure-get-orig-source
