#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-
# Copyright © 2011-2015 Jonas Smedegaard <dr@jones.dk>
# Description: Main Debian packaging script for GNU uCommon C++
#
# 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 declared/run before including CDBS snippets
stem = $(DEB_SOURCE_PACKAGE)
lib = lib$(stem)
major := $(shell grep ^LT_VERSION= configure.ac | grep -o '[[:digit:]]*' | head -n 1)
# TODO: Drop v5 suffix when G++5 transition is over
pkg-lib = $(lib)$(major)v5
debian/control:: debian/control.in
DEB_PHONY_RULES += debian/control.in
debian/control.in::
	sed -e 's/__LIBPKGNAME__/$(pkg-lib)/g' <debian/control.in.in >debian/control.in

DEB_AUTO_UPDATE_LIBTOOL = pre
DEB_AUTO_UPDATE_AUTOCONF = ,
DEB_AUTO_UPDATE_AUTOHEADER = ,
DEB_AUTO_UPDATE_ACLOCAL = ,
DEB_AUTO_UPDATE_AUTOMAKE = || automake --add-missing
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/class/cmake.mk
include /usr/share/cdbs/1/rules/debhelper.mk

pkg-dev = $(lib)-dev
pkg-doc = $(stem)-doc
pkg-bin = $(stem)-utils
extralibnames = libusecure
pkgconfigstems = $(stem)

DEB_UPSTREAM_URL = http://ftp.gnu.org/gnu/commoncpp

CDBS_BUILD_DEPENDS +=, pkg-config, libgnutls28-dev, graphviz

DEB_MAKE_CHECK_TARGET = check

devutils = ucommon-config
utils = args scrub-files mdsum zerofill car sockaddr pdetach keywait

# Transitional compatibility wrapper for Common C++
extralibnames += libcommoncpp
pkgconfigstems += commoncpp
devutils += commoncpp-config
#DEB_CONFIGURE_EXTRA_FLAGS += --disable-commoncpp --disable-stdcpp

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

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

DEB_CONFIGURE_EXTRA_FLAGS += --enable-atomics

DEB_INSTALL_DOCS_$(pkg-doc) += doc/html
DEB_INSTALL_MANPAGES_$(pkg-dev) = $(patsubst %,debian/tmp/usr/share/man/man1/%.1,$(devutils))
DEB_INSTALL_MANPAGES_$(pkg-bin) = $(patsubst %,debian/tmp/usr/share/man/man1/%.1,$(utils))
DEB_DH_INSTALL_ARGS_$(pkg-dev) = --autodest $(patsubst %,usr/bin/%,$(devutils))
DEB_DH_INSTALL_ARGS_$(pkg-bin) = --autodest $(patsubst %,usr/bin/%,$(utils))

# keep backup during build of upstream-shipped temp files
DEB_UPSTREAM_CRUFT_COPY = Makefile.in $(patsubst %,%/Makefile.in,commoncpp corelib gnutls inc inc/commoncpp inc/ucommon nossl openssl test utils)
DEB_UPSTREAM_CRUFT_COPY += aclocal.m4 autoconf/ltmain.sh configure ucommon.spec

# Upgrade Doxyfile, and use SVG graphics
DEB_COMPRESS_EXCLUDE_DEFAULT += .xhtml
common-configure-indep:: debian/stamp-doxyupgrade
debian/stamp-doxyupgrade:
	[ ! -f /usr/bin/doxygen ] || ( set -e; \
		cp ${DEB_SRCDIR}/Doxyfile ${DEB_SRCDIR}/Doxyfile.orig; \
		doxygen -u ${DEB_SRCDIR}/Doxyfile; \
		perl -i -pe 's/DOT_IMAGE_FORMAT\K.*/=svg/;s/HTML_FILE_EXTENSION\K.*/=.xhtml/' ${DEB_SRCDIR}/Doxyfile; \
	)
	touch $@
clean::
	[ ! -f ${DEB_SRCDIR}/Doxyfile.orig ] || \
		cp ${DEB_SRCDIR}/Doxyfile.orig ${DEB_SRCDIR}/Doxyfile
	rm -f ${DEB_SRCDIR}/Doxyfile.bak
	rm -f debian/stamp-doxyupgrade

# Build documentation only if doxy is available. See bug#632343
common-build-indep:: debian/stamp-docs
debian/stamp-docs:
	[ ! -d /usr/share/doc/doxygen-latex ] || $(DEB_MAKE_INVOKE) doxy
	touch $@
clean::
	rm -f debian/stamp-docs

# Strip bad pkg-config directives, and
# Let d-shlibs calculate development package dependencies
#  and handle shared library install
#  * d-shlibs 0.50 required for --multiarch and --exclude-la options
#  * d-shlibs 0.61 required for --v5 option
# TODO: Drop --v5 when G++5 transition is over
CDBS_BUILD_DEPENDS +=, d-shlibs (>= 0.61~)
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)
	perl -i -pe 's/\s+-[fW]\S+//g' debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig/*.pc
	d-shlibmove --commit \
		--v5 \
		--devunversioned \
		--exclude-la \
		--multiarch \
		--override  s/$(libname)$(major)-dev/$(libname)-dev/ \
		--movedev "debian/tmp/usr/include/*" usr/include/ \
		$(patsubst %,--movedev debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig/%.pc \
		usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig,$(pkgconfigstems)) \
		$(patsubst %,--extralib debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/%.so,$(extralibnames)) \
		debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/$(lib).so
	touch $@
clean::
	rm -f debian/stamp-local-shlibs-$(lib)

# use separately packaged jquery
CDBS_BUILD_DEPENDS +=, libjs-jquery
CDBS_DEPENDS_$(pkg-doc) += , libjs-jquery
DEB_DH_LINK_$(pkg-doc) = /usr/share/javascript/jquery/jquery.js \
	/usr/share/doc/$(pkg-doc)/html/jquery.js
