#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-
# Copyright © 2011-2013, 2015 Jonas Smedegaard <dr@jones.dk>
# Description: Main Debian packaging script for GNU SIP Witch
#
# 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 be declared/run before including CDBS snippets
stem = $(DEB_SOURCE_PACKAGE)
major := $(shell grep ^LT_VERSION= configure.ac | grep -o '[[:digit:]]*' | head -n 1)
pkg-lib = lib$(stem)$(major)
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 = ,
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-bin = $(stem)
pkg-cgi = $(stem)-cgi
pkg-dev = $(libname)-dev
pkg-cgi = $(stem)-doc
libname = lib$(stem)
pkgconfigstems = $(libname)
ucommon-major := $(shell grep ^USES_UCOMMON_REQUIRED= configure.ac | grep -o '[[:digit:]]*' | head -n 1)

CDBS_BUILD_DEPENDS += , libucommon-dev (>= $(ucommon-major)), libexosip2-dev
CDBS_BUILD_DEPENDS += , pkg-config, libavahi-client-dev
CDBS_BUILD_DEPENDS +=, libsystemd-dev [linux-any]

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

CDBS_BUILD_DEPENDS +=, pkg-kde-tools
include /usr/share/pkg-kde-tools/makefiles/1/cdbs/symbolshelper.mk
export DPKG_GENSYMBOLS_CHECK_LEVEL=$(if $(EXP_RELEASE),0,1)

DEB_MAKE_CHECK_TARGET = check \
 || { cat test/test-suite.log; $(if $(EXP_RELEASE),,false;) }

binfiles = sippasswd sipquery sipcontrol
sbinfiles = sipw
cgifiles = sipwitch-cgi
DEB_INSTALL_MANPAGES_$(pkg-bin) = $(patsubst %,debian/tmp/usr/share/man/man1/%.1,$(binfiles))
DEB_INSTALL_MANPAGES_$(pkg-bin) += $(patsubst %,debian/tmp/usr/share/man/man8/%.8,$(sbinfiles))
DEB_INSTALL_MANPAGES_$(pkg-cgi) = $(patsubst %,debian/tmp/usr/share/man/man8/%.8,$(cgifiles))
DEB_DH_INSTALL_ARGS_ALL = --autodest
DEB_DH_INSTALL_ARGS_$(pkg-bin) = $(addprefix usr/bin/,$(binfiles))
DEB_DH_INSTALL_ARGS_$(pkg-bin) += $(addprefix usr/sbin/,$(sbinfiles))
DEB_DH_INSTALL_ARGS_$(pkg-bin) += usr/lib/sipwitch/*.so etc
DEB_DH_INSTALL_ARGS_$(pkg-cgi) = $(addprefix usr/bin/,$(cgifiles))
DEB_DH_INSTALL_ARGS_$(pkg-cgi) += usr/lib/cgi-bin/

# put aside upstream-shipped temp files during build but after copyright-check
makefiledirs = common inc inc/sipwitch server test utils
upstreamtmpfiles = Makefile.in $(addsuffix /Makefile.in,$(makefiledirs))
upstreamtmpfiles += aclocal.m4 autoconf/ltmain.sh configure sipwitch.spec
upstreamtmpfiles += autoconf/compile autoconf/depcomp autoconf/missing
upstreamtmpfiles += sipwitch-config.h.in
pre-build:: debian/stamp-upstreamtmpstuff
debian/stamp-upstreamtmpstuff: debian/stamp-copyright-check
	for file in $(upstreamtmpfiles); do \
		[ ! -e $$file ] || [ -e $$file.upstream ] || cp -a $$file $$file.upstream; \
	done
	touch $@
clean::
	for file in $(upstreamtmpfiles); do \
		[ ! -e $$file.upstream ] || mv -f $$file.upstream $$file; \
	done
	rm -f debian/stamp-upstreamtmpstuff

# Have autotools force use pkg-config
DEB_CONFIGURE_EXTRA_FLAGS += --with-pkg-config

# Have autotools add non-shipped autotools and cleanup again
DEB_AUTOMAKE_ARGS = --add-missing
clean::
	rm -rf m4

# TODO: Enable docs when sensible (i.e. not mainly doing illegal ../ucommon)
#DEB_INSTALL_DOCS_$(pkg-doc) += doc/html
#common-build-indep:: debian/stamp-docs
#debian/stamp-docs:
#	rm -rf doc
#	doxygen Doxyfile
#	touch $@
#clean::
#	rm -rf doc
#	rm -f debian/stamp-docs

# Re-install initscript with debhelper to add pre- and postinst routines
install/$(pkg-bin)::
	mv debian/tmp/etc/init.d/$(stem) debian/$(stem).init
clean::
	rm -f debian/$(stem).init

# Let d-shlibs calculate development package dependencies
#  and handle shared library install
#  * d-shlibs 0.68 needed for libsipwitch quirk
#  * d-shlibs 0.73 needed for libucommon, libexosip and libosib quirks
CDBS_BUILD_DEPENDS +=, d-shlibs (>= 0.73~)
binary-post-install/$(pkg-lib):: debian/stamp-local-shlibs-$(libname)
debian/stamp-local-shlibs-$(libname): binary-install/$(pkg-lib)
	d-shlibmove --commit \
		--devunversioned \
		--exclude-la \
		--movedev "debian/tmp/usr/include/*" usr/include/ \
		$(patsubst %,--movedev debian/tmp/usr/lib/pkgconfig/%.pc usr/lib/pkgconfig,$(pkgconfigstems)) \
		$(patsubst %,--extralib debian/tmp/usr/lib/%.so,$(extralibnames)) \
		debian/tmp/usr/lib/$(libname).so
	touch $@
clean::
	rm -f debian/stamp-local-shlibs-$(libname)
