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

# These need to be declared before CDBS includes
DEB_AUTO_UPDATE_LIBTOOL = pre
DEB_AUTO_UPDATE_ACLOCAL = ,
DEB_AUTO_UPDATE_AUTOCONF = ,
DEB_AUTO_UPDATE_AUTOMAKE = ,
DEB_AUTO_UPDATE_AUTOHEADER = ,

# set library package from library major version
#  * Apply like this: debian/rules clean DEB_MAINTAINER_MODE=1
debian/control:: debian/control.in
DEB_PHONY_RULES += debian/control.in
debian/control.in:: debian/control.in.in
	sed -e 's/__LIBPKG__/$(pkg-lib)/g' <$< >$@

include /usr/share/cdbs/1/class/autotools.mk
include /usr/share/cdbs/1/rules/debhelper.mk

pkg = $(DEB_SOURCE_PACKAGE)
stem = $(patsubst lib%,%,$(pkg))
major = 0
lib = lib$(stem)
pkg-lib = lib$(stem)$(major)
pkg-dev = lib$(stem)-dev

# resolve if release is experimental
EXP_RELEASE = $(filter experimental% UNRELEASED,\
 $(shell dpkg-parsechangelog -S Distribution))

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

DEB_MAKE_CHECK_TARGET = check

# relax symbols check when targeting experimental
export DPKG_GENSYMBOLS_CHECK_LEVEL=$(if $(EXP_RELEASE),0,1)

# Let d-shlibs calculate development package dependencies
#  and handle shared library install
CDBS_BUILD_DEPENDS +=, d-shlibs
binary-post-install/$(pkg-lib) binary-post-install/$(pkg-dev):: \
 debian/stamp-local-shlibs-$(lib)
debian/stamp-local-shlibs-$(lib): \
 binary-install/$(pkg-lib) \
 binary-install/$(pkg-dev)
	d-shlibmove --commit \
		--devunversioned \
		--exclude-la \
		--multiarch \
		--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)/$(lib).so
	touch $@
clean::
	rm -f debian/stamp-local-shlibs-$(lib)

# create autotools files from scratch during build
auxdir = build-aux
macrodir = $(auxdir)/m4
makefiledirs = .
autoheaders = $(lib)-config.h
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)
stems_aux_autoconf = compile install-sh missing
files_autoconf = aclocal.m4 configure $(stems_aux_autoconf:%=$(auxdir)/%)
files_automake = $(auxdir)/depcomp $(makefiledirs:=/Makefile.in)
files_automake += $(auxdir)/test-driver
files_autoheader = config.h.in $(autoheaders:=.in)
files_configure = config.log
stems_autotools = autotools_core libtool aclocal autoconf automake
stems_autotools += autoheader configure
files_autotools = $(foreach var,$(stems_autotools),$(files_$(var)))
DEB_ACLOCAL_ARGS = -I$(macrodir) --install --force
DEB_AUTOMAKE_ARGS = --add-missing --copy --foreign --force
DEB_MAKE_CLEAN_TARGET = distclean
makefile-clean::
	rm -rf autom4te.cache
	rm -f $(filter-out $(DEB_UPSTREAM_CRUFT_MOVE),$(files_autotools))
clean::
	rm -f $(filter-out $(DEB_UPSTREAM_CRUFT_MOVE),$(files_autotools_core))
	test ! -d $(auxdir) || rmdir --ignore-fail-on-non-empty $(auxdir)

# cleanup missed autotools
clean::
	rm -f univalue-config.h.in

# track symbols using pkgkde-symbolshelper
CDBS_BUILD_DEPENDS +=, pkg-kde-tools
include /usr/share/pkg-kde-tools/makefiles/1/cdbs/symbolshelper.mk
