#! /usr/bin/make -f

###

# Configure arguments

confflags = --prefix=/usr --enable-static --disable-debug --disable-dependency-tracking

DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

confflags += --build $(DEB_BUILD_GNU_TYPE)

# Only specify --host when cross-compiling
ifneq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
    confflags += --host $(DEB_HOST_GNU_TYPE)
endif

###

# Directory to make the build on

objdir = $(CURDIR)/obj-$(DEB_HOST_GNU_TYPE)

###

# CFLAGS

CFLAGS = -Wall -g

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif

###

configure: configure-stamp
configure-stamp:
	dh_testdir

	ln -sf /usr/share/misc/config.sub admin
	ln -sf /usr/share/misc/config.guess admin

	ln -sf debian/patches
	quilt --quiltrc /dev/null push -a || test $$? = 2

	# I've stripped Makefile.in hunks from debian/patches/01_update-libtool.diff,
	# so we need to touch these files.
	touch config.h.in
	find -name Makefile.in | xargs touch

	-mkdir $(objdir)
	cd $(objdir) && \
	env CFLAGS="$(CFLAGS)" CXXFLAGS="$(CFLAGS)" ../configure $(confflags)

	touch $@

#

build: build-arch build-indep

build-arch: build-arch-stamp
build-arch-stamp: configure-stamp
	cd $(objdir) && \
	$(MAKE)

	touch $@

build-indep: build-indep-stamp
build-indep-stamp: configure-stamp
	# build-indep gets invoked in buildds as well...
	if [ -x /usr/bin/doxygen ]; then \
	    cd taglib-api && doxygen taglib.doxygen; \
	fi

	touch $@

#

clean:
	dh_testdir
	dh_testroot

	quilt --quiltrc /dev/null pop -a -R || test $$? = 2
	rm -rf .pc
	rm -f patches

	rm -f configure-stamp build-arch-stamp build-indep-stamp install-stamp
	rm -f admin/config.guess admin/config.sub

	# Remove build tree
	rm -rf $(objdir)
	rm -rf taglib-api/html

	dh_clean

#

install: install-stamp
install-stamp:
	dh_testdir
	dh_testroot
	dh_clean
	dh_installdirs
	cd $(objdir) && $(MAKE) install DESTDIR=$(CURDIR)/debian/tmp
	install bindings/README debian/tmp/README.bindings

	touch $@

#

binary: binary-arch binary-indep

binary-arch: build install
	$(MAKE) -f debian/rules DH_OPTIONS=-s binary-common

binary-indep: build install
	$(MAKE) -f debian/rules DH_OPTIONS=-i binary-common

binary-common:
	dh_testdir
	dh_testroot
	dh_install
	dh_installchangelogs
	dh_installdocs
	dh_installexamples
	dh_installman
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs -V
	dh_installdeb
	dh_shlibdeps -ldebian/libtag1c2a/usr/lib:debian/libtagc0/usr/lib
	dh_gencontrol
	dh_md5sums
	dh_builddeb

#

.PHONY: configure build build-arch build-indep clean install binary binary-arch binary-indep binary-common
