#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-
# Copyright © 2010-2011, 2016-2018 Jonas Smedegaard <dr@jones.dk>
# Copyright © 2012-2015 Scott Howard <showard@debian.org>
# Description: Main Debian packaging script for Groestlcoin
#
# 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/>.

include /usr/share/cdbs/1/rules/autoreconf.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))
major = 0
lib = lib$(stem)consensus
pkg-lib = lib$(stem)consensus$(major)
pkg-dev = lib$(stem)consensus-dev
daemon = $(stem)d
pkg-daemon = $(daemon)

CDBS_BUILD_DEPENDS +=, pkg-config, autoconf-archive

DEB_BUILD_PARALLEL = 1

DEB_INSTALL_DOCS_ALL += doc/release-notes.md
DEB_INSTALL_EXAMPLES_$(pkg-daemon) += contrib/debian/examples/*

# work around build failure with gcc 4.7
#CXXFLAGS += -fpermissive

# Silence seemingly harmless noise about unused variables
#CXXFLAGS += -Wno-unused-variable

CDBS_BUILD_DEPENDS +=, libdb++-dev
CDBS_SUGGESTS_$(pkg-daemon) = db-util
DEB_CONFIGURE_EXTRA_FLAGS += --with-incompatible-bdb

# avoid upstream optimizations - we want $DEB_* variables tocontrol them
DEB_CONFIGURE_EXTRA_FLAGS += --disable-ccache --disable-hardening

# Bitcoin developers fear uncovered vulnerabilities
# * see https://github.com/bitcoin/bitcoin/commit/21d27eb
DEB_CONFIGURE_EXTRA_FLAGS += --without-miniupnpc

CDBS_BUILD_DEPENDS +=, libleveldb-dev
DEB_CONFIGURE_EXTRA_FLAGS += --with-system-leveldb

CDBS_BUILD_DEPENDS +=, libsecp256k1-dev
DEB_CONFIGURE_EXTRA_FLAGS += --with-system-secp256k1

CDBS_BUILD_DEPENDS +=, libunivalue-dev
DEB_CONFIGURE_EXTRA_FLAGS += --with-system-univalue

CDBS_BUILD_DEPENDS +=, libzmq3-dev
DEB_CONFIGURE_EXTRA_FLAGS += --with-zmq

CDBS_BUILD_DEPENDS +=, qtbase5-dev, qttools5-dev-tools
CDBS_BUILD_DEPENDS +=, libprotobuf-dev, protobuf-compiler
CDBS_BUILD_DEPENDS +=, libqrencode-dev, libevent-dev
CXXFLAGS += -fPIC
DEB_CONFIGURE_EXTRA_FLAGS += --with-gui=qt5 --with-qrencode
#export QT_SELECT=5

# resolve if release is experimental
EXP_RELEASE = $(filter experimental% UNRELEASED,$(DEB_SUITE))

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

CDBS_BUILD_DEPENDS +=, bash-completion
binary-install/$(pkg-daemon)::
	dh_bash-completion

# Run tests
# * Fake HOME and TMPDIR and use virtual X11 in testsuite
# * print test-suite.log if testsuite fails
# * relax experimental build to succed if testsuite fails
CDBS_BUILD_DEPENDS +=, xvfb, xauth
FAKETMP = $(CURDIR)/debian/faketmp
DEB_MAKE_CHECK_TARGET = check \
 || { cat src/test-suite.log; $(if $(EXP_RELEASE),,false;) }
$(cdbs_make_check_stamps): DEB_MAKE_INVOKE := \
 HOME=$(FAKETMP) TMPDIR=$(FAKETMP) xvfb-run -a $(DEB_MAKE_INVOKE)
pre-build::
	mkdir -p "$(FAKETMP)"
clean::
	rm -rf "$(FAKETMP)"

deps-boost = system
# TODO: Drop these when (if ever?) system json is used
deps-boost += filesystem program-options thread test chrono
CDBS_BUILD_DEPENDS +=, $(foreach dep,$(deps-boost),libboost-$(dep)-dev$(comma) )

CDBS_BUILD_DEPENDS +=, libssl1.0-dev, python

DEB_INSTALL_DOCS_ALL += README.md doc/release-notes/release-notes-*.md

pre-build::
	mkdir -p src/obj/nogui
clean::
	-find src/obj -type f -not -name .gitignore -delete
	-rmdir --ignore-fail-on-non-empty build
	rm -f src/$(daemon)

# Generate manpages during build
#  * TODO: generate groestlcoin-qt manpage when solved how without X11
CDBS_BUILD_DEPENDS +=, help2man
common-post-build-arch:: debian/stamp-manpage-build
debian/stamp-manpage-build:
	BITCOINQT=' ' TOPDIR=. contrib/devtools/gen-manpages.sh $(DEB_UPSTREAM_VERSION)
	touch $@
clean::
	rm -f doc/man/groestlcoin*.1

# Ensure wrapper is set executable
binary-post-install/$(pkg-daemon)::
	chmod +x debian/$(pkg-daemon)/usr/bin/$(daemon)

# 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/$(libpkg) 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 \
		--override 's/libanl1-dev//' \
		--override 's/libsecp256k1-0-dev/libsecp256k1-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)

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