#! /usr/bin/make -f

ifneq (,$(shell which tg))
QUILT_PATCH_DIR := debian/patches
include /usr/share/topgit/tg2quilt.mk
endif

export CFLAGS += -fvisibility=hidden -fvisibility-inlines-hidden
export CXXFLAGS += -fvisibility=hidden -fvisibility-inlines-hidden
CMAKE_ARGS = -DCMAKE_USE_RELATIVE_PATHS=ON -DWITH_ASF=ON -DWITH_MP4=ON

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

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

QUILT := quilt --quiltrc /dev/null
DH := dh $(filter-out dh_auto_% --builddirectory%,$(dh_auto))
MAKE_ME = $(MAKE) -f debian/rules

### 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)
	$(call dh_auto,configure,$(builddir)) -- $(CMAKE_ARGS)
	$(call dh_auto,build,$(builddir))
	touch $@

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

build:
	$(DH) --before dh_auto_configure $@
	$(MAKE_ME) $(call buildstamp,rusxmms)
	$(MAKE_ME) $(call buildstamp,vanilla)
	# Complete building
	$(DH) $@ --after dh_auto_build $@

### Install / binary building ###
override_dh_auto_install:
	$(call dh_auto,install,builddir-vanilla)
	install -d -m755 debian/tmp-rusxmms/usr/lib/
	cp -a builddir-rusxmms/taglib/libtag.so.* debian/tmp-rusxmms/usr/lib 

override_dh_makeshlibs:
	dh_makeshlibs -plibtag1-vanilla -plibtag1-rusxmms -V'libtag1c2a (>= 1.6.3)'
	dh_makeshlibs -plibtagc0 -V'libtagc0 (>= 1.5)'
	dh_makeshlibs --remaining-packages

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;

# Generate docs when building indep
doc/html/index.html: | build
	# build-indep gets invoked in buildds as well...
	if [ -x /usr/bin/doxygen ]; then \
	    cd doc && doxygen taglib.doxygen; \
	fi

binary binary-indep: doc/html/index.html

clean: unpatch-rusxmms
	$(call dh_auto,clean,builddir-vanilla)
	$(call dh_auto,clean,builddir-rusxmms)
	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 dh rule
%:
	$(DH) $@
