#!/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))
daemon = $(stem)d
pkg-daemon = $(daemon)

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

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

DEB_CONFIGURE_EXTRA_FLAGS += --with-system-leveldb

DEB_CONFIGURE_EXTRA_FLAGS += --with-system-secp256k1

DEB_CONFIGURE_EXTRA_FLAGS += --with-system-univalue

DEB_CONFIGURE_EXTRA_FLAGS += --with-zmq

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)

DEB_CONFIGURE_EXTRA_FLAGS += --without-libs

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
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)"

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
#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)
