#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-
# Copyright © 2003 Keegan Quinn <ice@thebasement.org>
# Copyright © 2004-2008, 2011 Jonas Smedegaard <dr@jones.dk>
# Description: Main Debian packaging script for libshout
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

DEB_AUTO_UPDATE_LIBTOOL = pre
DEB_AUTO_UPDATE_ACLOCAL = 1.11
DEB_AUTO_UPDATE_AUTOCONF = 2.68
DEB_AUTO_UPDATE_AUTOHEADER = 2.68
DEB_AUTO_UPDATE_AUTOMAKE = 1.11
-include /usr/share/cdbs/1/rules/upstream-tarball.mk
include /usr/share//cdbs/1/rules/utils.mk
include /usr/share/cdbs/1/class/autotools.mk
include /usr/share/cdbs/1/rules/debhelper.mk

stem = shout
abi = 3
pkg = lib$(stem)
libname = lib$(stem)
libpkgname = lib$(stem)$(abi)
devpkgname = lib$(stem)$(abi)-dev

# Needed at build time by upstream code
CDBS_BUILD_DEPENDS += , libogg-dev, libvorbis-dev, pkg-config
CDBS_BUILD_DEPENDS += , libtheora-dev, libspeex-dev

# Needed at build time for our packaging
CDBS_BUILD_DEPENDS += , d-shlibs (>= 0.48)

# suppress optional build-dependencies
CDBS_BUILD_DEPENDS_rules_upstream-tarball =
CDBS_BUILD_DEPENDS_rules_utils_copyright-check =

# Multiarch quirks (should ideally be resolved automagically)
CDBS_BUILD_DEPENDS_rules_debhelper_buildinfo =
CDBS_BUILD_DEPENDS_rules_buildcore_pkgrel =
CDBS_PREDEPENDS = $(if $(DEB_HOST_MULTIARCH),multiarch-support)

DEB_UPSTREAM_PACKAGE = $(pkg)
DEB_UPSTREAM_URL = http://downloads.us.xiph.org/releases/$(pkg)
DEB_UPSTREAM_TARBALL_MD5 = 11765b2592e7ea623ccd93d3f8df172c

DEB_INSTALL_EXAMPLES_$(devpkgname) = examples/example.c

DEB_CONFIGURE_EXTRA_FLAGS += --libdir=\$${prefix}/lib/$(DEB_HOST_MULTIARCH)

# avoid unneeded .la file
binary-post-install/$(libpkgname):: debian/stamp-local-strip-la
debian/stamp-local-strip-la: binary-install/$(libpkgname)
	find debian -type f -name '*.la' -delete
	touch $@
clean::
	rm -f debian/stamp-local-strip-la

# Let d-shlibs calculate development package dependencies
#  and handle shared library install
binary-post-install/$(libpkgname):: debian/stamp-local-shlibs-$(libname)
debian/stamp-local-shlibs-$(libname): binary-install/$(libpkgname) debian/stamp-local-strip-la
	d-shlibmove --commit \
		--multiarch \
		--override s/ld1-dev// \
		--movedev "debian/tmp/usr/include/*" usr/include/ \
		--movedev "debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig/*" \
			usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig \
		debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/$(libname).so
	touch $@
clean::
	rm -f debian/stamp-local-shlibs-$(libname)

# put aside autogenerated files during build
# TODO: use DEB_UPSTREAM_CRUFT_MOVE when cdbs 0.4.106 is in stable
upstreamtmpstuff = Makefile.in $(patsubst %,%/Makefile.in,doc examples include include/shout src win32)
upstreamtmpstuff += $(patsubst %,src/%/Makefile.in,avl httpp net thread timing)
upstreamtmpstuff += configure aclocal.m4 depcomp install-sh missing
upstreamtmpstuff += ltmain.sh m4/libtool.m4 m4/ltoptions.m4 m4/ltversion.m4
pre-build:: debian/upstream-stuff
debian/upstream-stuff: debian/stamp-copyright-check
	mkdir -p debian/upstream-stuff
	@for orig in $(upstreamtmpstuff); do \
		backup="debian/upstream-stuff/$$orig"; \
		[ ! -e "$$orig" ] || [ -e "$$backup" ] || { \
			mkdir -p "$$(dirname "$$backup")"; \
			echo mv "$$orig" "$$backup"; \
			mv "$$orig" "$$backup"; \
		}; \
	done
clean::
	@for orig in $(upstreamtmpstuff); do \
		backup="debian/upstream-stuff/$$orig"; \
		if [ -e "$$backup" ]; then \
			if [ -e "$$orig" ]; then \
				echo "rm -rf" "$$orig"; \
				rm -rf "$$orig"; \
			fi; \
			echo mv "$$backup" "$$orig"; \
			mv "$$backup" "$$orig"; \
		fi; \
	done
	rm -rf debian/upstream-stuff

# (re)generate most possible autotools files
DEB_ACLOCAL_ARGS = -Im4 --install --force
DEB_AUTOMAKE_ARGS = --add-missing --copy --foreign --force
DEB_MAKE_CLEAN_TARGET = distclean
clean::
	rm -f m4/pkg.m4
