#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-
# Copyright © 2010 Dave Beckett <dajobe@debian.org>
# Copyright © 2011-2012 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 = ,
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

CDBS_BUILD_DEPENDS += , libpcre3-dev, libglib2.0-dev, ncurses-dev, libreadline-dev
CDBS_BUILD_DEPENDS += , libavahi-client-dev, libavahi-glib-dev, libstemmer-dev
CDBS_BUILD_DEPENDS += , uuid-dev
CDBS_BUILD_DEPENDS += $(if $(DEB_NOEPOCH_VERSION:1.0%=),,$(comma) librasqal2-dev (<< 0.9.22)$(comma) libraptor1-dev)
CDBS_BUILD_DEPENDS += $(if $(DEB_NOEPOCH_VERSION:1.1%=),,$(comma) librasqal3-dev$(comma) libraptor2-dev)

CDBS_DEPENDS_$(pkg) = adduser, lsb-base (>= 3.2-13), netbase
CDBS_RECOMMENDS_$(pkg) = avahi-daemon, perl, libnet-http-perl, liburi-perl

DEB_UPSTREAM_URL = http://www.4store.org/download
DEB_UPSTREAM_TARBALL_BASENAME_MANGLE = s/-(\d)/-v$$1/
DEB_UPSTREAM_TARBALL_MD5 = 90512417abc588c6b60d7b2167f7dc39

LDFLAGS += -Wl,-z,defs

# TODO: Enable when upstream supports overriding FS_STORE_ROOT
#DEB_MAKE_CHECK_TARGET = test

# Avoid installing virtually empty README, NEWS and ChangeLog files
DEB_INSTALL_DOCS_ALL =
DEB_INSTALL_CHANGELOGS_ALL =

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

# put aside upstream-shipped temp files during build but after copyright-check
DEB_UPSTREAM_CRUFT_COPY = src/common/params.h version.sh
# put aside upstream-shipped autobuilt files and remake during build
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
DEB_UPSTREAM_CRUFT_MOVE += $(filter-out $(files_autoconf_modern),\
	$(files_autotools))
# 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 (>= 0.45~)
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
