#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-
# Copyright © 2014 Jonas Smedegaard <dr@jones.dk>
# Description: Main Debian packaging script for libmarpa
#
# 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 3, 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/>.

# This needs to run (and thus be declared) before CDBS includes
debian/control:: debian/control.in
DEB_PHONY_RULES += debian/control.in
debian/control.in::
	sed -e 's/__LIBPKG__/$(libpkg)/g' <debian/control.in.in >debian/control.in

DEB_AUTO_UPDATE_LIBTOOL = pre
DEB_AUTO_UPDATE_ACLOCAL = ,
DEB_AUTO_UPDATE_AUTOCONF = ,
DEB_AUTO_UPDATE_AUTOMAKE = ,
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

# TODO: remove below once symbols file covers all architectures
DEB_DH_MAKESHLIBS_ARGS_DEFAULT = -- -c0

pkg = $(DEB_SOURCE_PACKAGE)
stem = $(patsubst lib%,%,$(pkg))
abi = 0
lib = lib$(stem)
libpkg = lib$(stem)$(abi)
devpkg = lib$(stem)-dev

DEB_UPSTREAM_TARBALL_BASENAME = Marpa-R2-2.086000
DEB_UPSTREAM_URL = http://www.cpan.org/CPAN/authors/id/J/JK/JKEGL

DEB_UPSTREAM_REPACKAGE_EXCLUDES += \
 ./t/etc/wall_proof.txt

DEB_SRCDIR = libmarpa_dist
DEB_BUILDDIR = build

# put aside upstream-shipped autogenerated files and (re)generate during build
makefiledirs = .
files_autoconf_modern = $(auxdir)/compile
DEB_UPSTREAM_CRUFT_MOVE = $(filter-out $(files_autoconf_modern),\
	$(files_autotools))
DEB_UPSTREAM_CRUFT_COPY = libmarpa_dist/LIB_VERSION
makefile-clean::
	rm -f $(files_autoconf_modern)
# TODO: move below to CDBS autotools-files.mk snippet (if indeed generic)
basedir = $(DEB_SRCDIR)
auxdir = $(basedir)
macrodir = $(basedir)/m4
autom4tedir = $(basedir)/autom4te.cache
files_autotools_core = $(auxdir)/config.guess $(auxdir)/config.sub
stems_m4_libtool = libtool ltoptions ltsugar ltversion lt~obsolete
files_libtool = $(auxdir)/ltmain.sh $(stems_m4_libtool:%=$(macrodir)/%.m4)
files_aclocal = $(basedir)/aclocal.m4
stems_aux_autoconf = ar-lib compile install-sh missing
files_autoconf = $(basedir)/configure $(stems_aux_autoconf:%=$(auxdir)/%)
files_automake = $(auxdir)/depcomp $(makefiledirs:%=$(basedir)/%/Makefile.in)
files_autotools = $(files_autotools_core) $(files_libtool) $(files_aclocal)
files_autotools += $(files_autoconf) $(files_automake)
files_configure = $(DEB_BUILDDIR)/config.log
files_make = $(makefiledirs:%=$(DEB_BUILDDIR)/%/Makefile)
DEB_ACLOCAL_ARGS = -Im4 --install --force
DEB_AUTOMAKE_ARGS = --add-missing --copy --foreign --force
DEB_MAKE_CLEAN_TARGET = distclean
makefile-clean::
	rm -rf $(dirs_autotools)
	rm -f $(filter-out $(DEB_UPSTREAM_CRUFT_MOVE),\
		$(files_autotools) $(files_configure) $(files_make))
clean::
	rm -f $(filter-out $(DEB_UPSTREAM_CRUFT_MOVE),\
		$(files_autotools_core))
	test ! -d $(auxdir) || rmdir --ignore-fail-on-non-empty $(auxdir)

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

# Let d-shlibs calculate development package dependencies
#  and handle shared library install
#  * recent d-shlibs needed for --multiarch option
CDBS_BUILD_DEPENDS +=, d-shlibs (>= 0.48)
binary-post-install/$(libpkg) binary-post-install/$(devpkg):: \
 debian/stamp-local-shlibs-$(lib)
debian/stamp-local-shlibs-$(lib): \
 binary-install/$(libpkg) \
 binary-install/$(devpkg)
	d-shlibmove --commit \
		--devunversioned \
		--exclude-la \
		--multiarch \
		--movedev "debian/tmp/usr/include/*" usr/include/ \
		debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/$(lib).so
	touch $@
clean::
	rm -f debian/stamp-local-shlibs-$(lib)
