#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-
# Copyright © 2010 Dave Beckett <dajobe@debian.org>
# Copyright © 2011-2013, 2015-2016 Jonas Smedegaard <dr@jones.dk>
# Description: Main Debian packaging script for 4store
#
# 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 of the License, 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 before inclusion of CDBS snippets
debian/control:: debian/control.in
DEB_PHONY_RULES += debian/control.in
debian/control.in::
	sed -e 's/__LIBPKGNAME__/$(libpkgname)/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 = ,
DEB_AUTO_UPDATE_AUTOHEADER = ,
include /usr/share/cdbs/1/rules/upstream-tarball.mk
include /usr/share/cdbs/1/class/autotools.mk
include /usr/share/cdbs/1/rules/debhelper.mk
include /usr/share/cdbs/1/rules/utils.mk

stem = 4store
abi = 0
pkg = $(stem)
libpkgname = lib$(stem)$(abi)
libname = lib$(stem)
pkgconfigfile = $(stem)-$(abi).pc

DEB_UPSTREAM_URL = https://github.com/garlik/4store/archive
DEB_UPSTREAM_TARBALL_BASENAME = v$(DEB_UPSTREAM_TARBALL_VERSION)

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

# Needed by upstream build
bdeps = libpcre3-dev, libglib2.0-dev, ncurses-dev, libreadline-dev
bdeps +=, libavahi-client-dev, libavahi-glib-dev, libstemmer-dev
bdeps +=, uuid-dev, librasqal3-dev, libraptor2-dev

CDBS_BUILD_DEPENDS +=, $(bdeps)
CDBS_DEPENDS_$(pkg) = adduser, netbase
CDBS_RECOMMENDS_$(pkg) = avahi-daemon, perl, libnet-http-perl, liburi-perl

LDFLAGS += -Wl,-z,defs

# skip copyright-check icon file
DEB_COPYRIGHT_CHECK_IGNORE_REGEX = ^(app-aux/Resources/4store.icns|debian/(changelog|copyright(|_hints|_newhints)))$

# Work around upstream code assuming build directly from git
pre-build::
	echo "$(DEB_UPSTREAM_TARBALL_BASENAME)" > .version
clean::
	rm -f .version

# detect when when targeted experimental suite
exp-relax-check := $(if $(shell dpkg-parsechangelog \
	| grep -x 'Distribution: \(experimental\|UNRELEASED\)'),yes)

# TODO: Fix failing tests - for now enable only relaxed for experimental
DEB_MAKE_CHECK_TARGET = $(if $(exp-relax-check),$(strip\
	test FS_STORE_ROOT="$(cdbs_curdestdir)/var/lib/4store" || true))

DEB_INSTALL_DOCS_ALL = docs/*
DEB_INSTALL_CHANGELOGS_ALL = NEWS
DEB_DH_INSTALL_ARGS_$(pkg) = src/admin/README.md /usr/share/doc/$(pkg)/README.admin

DEB_DH_INSTALLINIT_ARGS = --no-start --update-rcd-params="defaults" --error-handler=installinit_error

# put aside upstream-shipped temp files during build, and cleanup afterwards
DEB_UPSTREAM_CRUFT_COPY = version.sh
DEB_UPSTREAM_CRUFT_MOVE += tests/httpd-extras/results
clean::
	rm -rf tests/httpd/results tests/query/results

# create autotools file files during build and cleanup afterwards
makefilesrcdirs = admin backend common frontend http libs utilities
makefilesrcdirs += libs/double-metaphone libs/mt19937-64 libs/stemmer
makefiledirs = . man src $(makefilesrcdirs:%=src/%) tests
files_autotools_notshipped = $(macrodir) $(auxdir)/compile $(auxdir)/ar-lib
files_autotools_notshipped += 4store-config.h.in
# TODO: move below to CDBS autotools-files.mk snippet (if indeed generic)
basedir = $(DEB_SRCDIR)
auxdir = $(basedir)
macrodir = $(basedir)/m4
autom4tedir = $(basedir)/autom4te.cache
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)
files_aclocal = $(basedir)/aclocal.m4
stems_aux_autoconf = ar-lib compile install-sh missing
files_autoconf = $(basedir)/configure $(stems_aux_autoconf:%=$(auxdir)/%)
files_automake = $(auxdir)/depcomp $(makefiledirs:%=$(basedir)/%/Makefile.in)
files_autotools = $(files_autotools_core) $(files_libtool) $(files_aclocal)
files_autotools += $(files_autoconf) $(files_automake)
files_configure = $(DEB_BUILDDIR)/config.log
files_make = $(makefiledirs:%=$(DEB_BUILDDIR)/%/Makefile)
DEB_ACLOCAL_ARGS = -Im4 --install --force
DEB_AUTOMAKE_ARGS = --add-missing --copy --foreign --force
DEB_MAKE_CLEAN_TARGET = distclean
makefile-clean::
	rm -rf $(dirs_autotools)
	rm -f $(filter-out $(DEB_UPSTREAM_CRUFT_MOVE),\
		$(files_autotools) $(files_configure) $(files_make))
clean::
	rm -f $(filter-out $(DEB_UPSTREAM_CRUFT_MOVE),\
		$(files_autotools_core))
	$(if $(call cdbs_streq,$(basedir),$(auxdir)),\
		,\
		test ! -d $(auxdir) \
		|| rmdir --ignore-fail-on-non-empty $(auxdir))
	$(if $(files_autotools_notshipped),\
		rm -rf $(files_autotools_notshipped))

# Let d-shlibs calculate development package dependencies
#  and handle shared library install
CDBS_BUILD_DEPENDS +=, d-shlibs
binary-post-install/$(libpkgname):: debian/stamp-local-shlibs-$(libname)
debian/stamp-local-shlibs-$(libname): binary-install/$(libpkgname)
	d-shlibmove --commit \
		--override s/libavahi-client.-dev/libavahi-client-dev/ \
		--override s/libavahi-common.-dev/libavahi-common-dev/ \
		--override s/libavahi-glib.-dev/libavahi-glib-dev/ \
		--movedev "debian/tmp/usr/include/*" usr/include/ \
		--movedev "debian/tmp/usr/lib/pkgconfig/$(pkgconfigfile)" usr/lib/pkgconfig/ \
		--devunversioned \
		--exclude-la \
		debian/tmp/usr/lib/$(libname).so
	touch $@
clean::
	rm -f debian/stamp-local-shlibs-$(libname)

# cleanup makefile skipped due to entanglement in patch 2002
clean::
	rm -f src/libs/stemmer/Makefile
