#! /usr/bin/make -f

export DEB_CFLAGS_MAINT_APPEND += -fvisibility=hidden -fvisibility-inlines-hidden
export DEB_CXXFLAGS_MAINT_APPEND += -fvisibility=hidden -fvisibility-inlines-hidden
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

# Build with NDEBUG (i.e. no debug messages) unless noopt build was requested
ifeq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
    export DEB_CFLAGS_MAINT_APPEND += -DNDEBUG
    export DEB_CXXFLAGS_MAINT_APPEND += -DNDEBUG
endif

CMAKE_ARGS = -DCMAKE_USE_RELATIVE_PATHS=ON -DWITH_ASF=ON -DWITH_MP4=ON -DLIB_INSTALL_DIR:PATH=/usr/lib/$(DEB_HOST_MULTIARCH)
DH_AUTO_ARGS = --buildsystem=cmake
QUILT := quilt --quiltrc /dev/null
MAKE_ME = $(MAKE) -f $(lastword $(MAKEFILE_LIST))

FLAVOURS := vanilla rusxmms
flavour = $(filter-out %-,$(subst -,- ,$@))
builddir = builddir-$(flavour)

# Various "functions" (can be invoked via $(call ...))
buildstamp = $(patsubst %,debian/stamp-build-%,$(1))
libpkg = libtag1-$(1)
quilt_safe = $(QUILT) $(1) || test $$? = 2

### Handle patching of rusxmms flavour ###
RUSXMMS_PATCH_DIR := debian/rusxmms-patches
RUSXMMS_PATCH_SERIES := debian/rusxmms-patches/series

$(RUSXMMS_PATCH_SERIES):
	cat $(wildcard debian/patches/series) $(RUSXMMS_PATCH_DIR)/rusxmms_series > $@

debian/stamp-patch-rusxmms: $(RUSXMMS_PATCH_SERIES)
	QUILT_PATCHES="$(RUSXMMS_PATCH_DIR)" $(call quilt_safe,push -a)
	touch $@

unpatch-rusxmms: $(RUSXMMS_PATCH_SERIES)
	lastpatch="$(shell QUILT_SERIES=debian/patches/series $(QUILT) series | tail -n1)"; \
	if [ -z "$$lastpatch" ]; then \
	    QUILT_PATCHES="$(RUSXMMS_PATCH_DIR)" $(call quilt_safe,pop -a); \
	elif $(QUILT) applied | grep -qF $$lastpatch; then \
	    QUILT_PATCHES="$(RUSXMMS_PATCH_DIR)" $(call quilt_safe,pop $$lastpatch); \
	fi

### Build rusxmms first, then build vanilla ###
$(call buildstamp,$(FLAVOURS)):
	@echo Building $(flavour)
	dh_auto_configure --parallel --builddirectory=$(builddir) $(DH_AUTO_ARGS) -- $(CMAKE_ARGS)
	dh_auto_build --parallel --builddirectory=$(builddir) $(DH_AUTO_ARGS)
	touch $@

$(call buildstamp,rusxmms): debian/stamp-patch-rusxmms
$(call buildstamp,vanilla): | unpatch-rusxmms

override_dh_auto_configure:
ifneq ($(shell dh_listpackages -p$(call libpkg,rusxmms)),)
	$(MAKE_ME) $(call buildstamp,rusxmms)
endif
ifneq ($(shell dh_listpackages -p$(call libpkg,vanilla)),)
	$(MAKE_ME) $(call buildstamp,vanilla)
endif

### Install / binary building ###
override_dh_auto_install:
ifneq ($(shell dh_listpackages -p$(call libpkg,vanilla)),)
	dh_auto_install --builddirectory=builddir-vanilla $(DH_AUTO_ARGS)
endif
ifneq ($(shell dh_listpackages -p$(call libpkg,rusxmms)),)
	install -d -m755 debian/tmp-rusxmms/usr/lib/$(DEB_HOST_MULTIARCH)
	cp -a builddir-rusxmms/taglib/libtag.so.* debian/tmp-rusxmms/usr/lib/$(DEB_HOST_MULTIARCH)/
endif

override_dh_installdocs:
	dh_installdocs
	# Install bindings README
	if [ -d debian/libtag1c2a ]; then \
	    install -m644 -oroot -groot -T bindings/README debian/libtag1c2a/usr/share/doc/libtag1c2a/README.bindings; \
	fi;

override_dh_install:
	dh_install -plibtag1-rusxmms --sourcedir=$(CURDIR)/debian/tmp-rusxmms
	dh_install -Nlibtag1-rusxmms

# Generate docs when building indep
builddir-vanilla/doc/html/index.html: $(call buildstamp,vanilla)
	# build-indep gets invoked in buildds as well...
	if [ -x /usr/bin/doxygen ]; then \
	    dh_auto_build --buildsystem=makefile --builddirectory=builddir-vanilla -- docs; \
	fi

build-indep: builddir-vanilla/doc/html/index.html

clean: unpatch-rusxmms
	dh_auto_clean --builddirectory=builddir-vanilla $(DH_AUTO_ARGS)
	dh_auto_clean --builddirectory=builddir-rusxmms $(DH_AUTO_ARGS)
	rm -rf $(patsubst %,debian/tmp-%,$(FLAVOURS))
	rm -f debian/stamp-*
	rm -f $(RUSXMMS_PATCH_SERIES)
	# Clean documentation
	rm -rf doc/html
	dh clean

# All-in-one default dh rule
%:
	dh $@ --parallel
