#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-
# Copyright © 2010-2011, 2016 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/>.

DEB_AUTO_UPDATE_LIBTOOL = pre
DEB_AUTO_UPDATE_ACLOCAL = ,
DEB_AUTO_UPDATE_AUTOCONF = ,
DEB_AUTO_UPDATE_AUTOMAKE = ,
DEB_AUTO_UPDATE_AUTOHEADER = ,
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))
major = 0
lib = lib$(stem)consensus
pkg-lib = lib$(stem)consensus$(major)
pkg-dev = lib$(stem)consensus-dev
gui = $(stem)-qt
pkg-gui = $(gui)
daemon = $(stem)d
pkg-daemon = $(daemon)

DEB_UPSTREAM_URL = https://github.com/GroestlCoin/groestlcoin/archive

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

CDBS_BUILD_DEPENDS +=, pkg-config, autoconf-archive, dh-exec

DEB_BUILD_PARALLEL = 1

DEB_INSTALL_DOCS_ALL += doc/release-notes.md
DEB_INSTALL_EXAMPLES_$(pkg-daemon) += contrib/debian/examples/*
DEB_INSTALL_MANPAGES_$(pkg-daemon) += \
 contrib/debian/manpages/$(stem)-cli.1 \
 contrib/debian/manpages/$(stem).conf.5 \
 contrib/debian/manpages/$(daemon).1
DEB_INSTALL_MANPAGES_$(pkg-gui) += contrib/debian/manpages/groestlcoin-qt.1

# extract metadata from graphics files before copyright check
# - and skip compiled localizations, ICO/ICNS files, and PGP key
CDBS_BUILD_DEPENDS +=, libregexp-assemble-perl, libimage-exiftool-perl
CDBS_BUILD_DEPENDS +=, libfont-ttf-perl
local_inspection_regex = png|psd
local_inspection_skip_regex = src/qt/locale/.*\.ts|.*\.(icns|ico|pgp)|.*/DS_Store
DEB_COPYRIGHT_CHECK_IGNORE_REGEX = ^((.*/)?[^/]+\.($(local_inspection_regex))|$(local_inspection_skip_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

# put aide convenience code copies during build, to avoid accidental use
#DEB_UPSTREAM_CRUFT_MOVE += src/secp256k1

# 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 +=, qtbase5-dev, qttools5-dev-tools
CDBS_BUILD_DEPENDS +=, libprotobuf-dev, protobuf-compiler
CDBS_BUILD_DEPENDS +=, libqrencode-dev
CXXFLAGS += -fPIC
DEB_CONFIGURE_EXTRA_FLAGS += --with-gui=qt5 --with-qrencode
#export QT_SELECT=5

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

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)"
	cd src/test && [ -f groestlcoin-util-test.py ] || ln -s bitcoin-util-test.py groestlcoin-util-test.py
	cd src/test/data && [ -f groestlcoin-util-test.json ] || ln -s bitcoin-util-test.json groestlcoin-util-test.json
clean::
	rm -rf "$(FAKETMP)"
	rm -f src/test/groestlcoin-util-test.py src/test/data/groestlcoin-util-test.json

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 +=, libssl-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)

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

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

# create autotools files from scratch during build
auxdir = build-aux
macrodir = $(auxdir)/m4
makefiledirs = . src
autoheaders = src/config/$(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_m4_autoconf = pkg
stems_aux_autoconf = 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 += pthread
files_archive = $(stems_m4_archive:%=$(macrodir)/ax_%.m4)
stems_m4_boost = boost_base boost_program_options
files_boost = $(stems_m4_boost:%=$(macrodir)/ax_%.m4)
stems_m4_misc = gcc_func_attribute
files_misc = $(stems_m4_misc:%=$(macrodir)/ax_%.m4)
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 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)

# cleanup missed autotools
clean::
	rm -f src/config/bitcoin-config.h.in Makefile config.status src/Makefile

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