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

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

DEB_UPSTREAM_URL = DEB_UPSTREAM_URL = https://github.com/libbitcoin/libbitcoin/archive

# Build-depend unversioned on debhelper
#  TODO: Drop when adopted in cdbs
CDBS_BUILD_DEPENDS_rules_debhelper_v9 = debhelper

# Needed by upstream build
bdeps-dev = leveldb secp256k1 boost
bdeps-boost = chrono date-time filesystem locale program-options regex
bdeps-boost += system thread test
deps = $(patsubst %,$(comma) lib%-dev,$(bdeps-dev))
deps +=, $(patsubst %,$(comma) libboost-%-dev,$(bdeps-boost))
deps +=, pkg-config

# Needed for our packaging
deps-pkg +=, autoconf-archive

CDBS_BUILD_DEPENDS +=, $(deps), $(deps-pkg)

# Multiarch quirk (see also other uses of that variable in this file)
CDBS_PREDEPENDS_$(libpkg) = $(if $(DEB_HOST_MULTIARCH),multiarch-support)

# extract metadata from images before copyright check
CDBS_BUILD_DEPENDS +=, libregexp-assemble-perl, libimage-exiftool-perl
CDBS_BUILD_DEPENDS +=, libfont-ttf-perl
local_inspection_regex = png
DEB_COPYRIGHT_CHECK_IGNORE_REGEX = ^((.*/)?[^/]+\.($(local_inspection_regex))|debian/(changelog|copyright(|_hints|_newhints)))$$
debian/stamp-copyright-check: debian/stamp-extract-copyright
debian/stamp-extract-copyright:
	find * -type f -regextype posix-extended \
		-regex '.*\.($(local_inspection_regex))' \
		-print0 | perl -0 debian/license-miner
	touch $@
pre-build:: clean-extracted-copyright-during-build
clean-extracted-copyright-during-build: debian/stamp-copyright-check
	find -type f -name '*.metadata_dump' -delete
clean::
	find -type f -name '*.metadata_dump' -delete
	rm -f debian/stamp-extract-copyright

# resolve if release is experimental
CDBS_BUILD_DEPENDS +=, dpkg-dev (>= 1.17.0)
EXP_RELEASE = $(filter experimental% UNRELEASED,$(shell dpkg-parsechangelog -S Distribution))

# work around build failure with g++ 4.8
#CXXFLAGS += -fpermissive

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

DEB_CONFIGURE_EXTRA_FLAGS += --enable-bdb --enable-leveldb

# libbitcoin upstream testsuite is intel-only
# https://github.com/libbitcoin/libbitcoin/issues/446
ifneq (,$(filter $(DEB_HOST_ARCH),amd64 i386))
DEB_MAKE_CHECK_TARGET = check
endif

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

DEB_INSTALL_EXAMPLES_$(devpkg) = examples/*

# Let d-shlibs calculate development package dependencies
#  and handle shared library install
deps-pkg +=, d-shlibs
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 \
		--override s/libleveldb1-dev/libleveldb-dev/ \
		--override 's/libboost_\([a-z0-9.]\+\)\.[0-9]-dev/libboost-\1-dev/' \
		--override 's/libboost_\([a-z0-9.]\+\)_\([a-z0-9.]\+\)\.[0-9]-dev/libboost-\1-\2-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)/$(lib).so
	touch $@
clean::
	rm -f debian/stamp-local-shlibs-$(lib)

# put aside upstream-shipped autogenerated files and (re)generate during build
auxdir = build-aux
macrodir = m4
makefiledirs = . include/bitcoin src
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_m4_autoconf = pkg
stems_aux_autoconf = ar-lib compile install-sh missing
files_autoconf = aclocal.m4 configure $(stems_aux_autoconf:%=$(auxdir)/%)
files_autoconf += $(stems_m4_autoconf:%=$(macrodir)/%.m4)
stems_m4_archive = check_compile_flag check_link_flag check_preproc_flag
stems_m4_archive += cxx_compile_stdcxx_11 java_devel jni_include_dir
stems_m4_archive += prog_jar prog_javac prog_javac_works prog_java
stems_m4_archive += prog_java_works pthread python_devel
files_archive = $(stems_m4_archive:%=$(macrodir)/ax_%.m4)
stems_m4_boost = base chrono date_time filesystem locale program_options
stems_m4_boost += regex system thread unit_test_framework
files_boost = $(stems_m4_boost:%=$(macrodir)/ax_boost_%.m4)
stems_m4_misc += lib-ld lib-link lib-prefix
files_misc = $(stems_m4_axmisc:%=$(macrodir)/%.m4)
files_automake = $(auxdir)/depcomp $(makefiledirs:=/Makefile.in)
files_automake += $(auxdir)/test-driver
files_configure = config.log
stems_autotools = autotools_core libtool aclocal autoconf automake
stems_autotools += configure archive boost misc
files_autotools = $(foreach var,$(stems_autotools),$(files_$(var)))
DEB_UPSTREAM_CRUFT_MOVE = $(files_archive) $(files_boost) $(files_misc)
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)

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