#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-
# Copyright 2012, 2015 Jonas Smedegaard <dr@jones.dk>
# Description: Main Debian packaging script for sugar-toolkit-gtk3
#
# 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_BUILDDIR = build
include /usr/share/cdbs/1/rules/upstream-tarball.mk
include /usr/share/cdbs/1/rules/utils.mk
include /usr/share/cdbs/1/class/python-autotools.mk
include /usr/share/cdbs/1/rules/debhelper.mk

# TODO: convince upstream to install headers, and provide proper -dev package
stem = sugarext
abi = 0
lib = lib$(stem)
libpkg = lib$(stem)$(abi)
devpkg = lib$(stem)-dev
girpkg = gir1.2-$(stem)-1.0
datapkg = lib$(stem)-data
#binpkg = sugar-activity
binpkg = python-sugar3
pythonpkg = python-sugar3

DEB_UPSTREAM_URL = http://download.sugarlabs.org/sources/sucrose/glucose/$(DEB_UPSTREAM_PACKAGE)
DEB_UPSTREAM_TARBALL_EXTENSION = tar.xz

# Needed by upstream build process
#  * linux-libc-dev/hurd-dev needed by sugar-fatattr
dev-bdeps = gtk-3 gdk-pixbuf2.0 sm ice rsvg2 xfixes xi x11
bdeps = $(patsubst %,$(comma) lib%-dev,$(dev-bdeps))
bdeps +=, gettext, intltool, libxml-parser-perl, x11proto-core-dev
bdeps +=, libasound2-dev | libasound-dev
bdeps +=, libgirepository1.0-dev (>= 1.30.0)
bdeps +=, linux-libc-dev [linux-any], hurd-dev [hurd-any]

# Needed (always/often/seldom) at runtime
#  * telepathy-* versioning follows http://wiki.sugarlabs.org/go/0.90/Notes
#  * zip, git and subversion are used in .xo format packaging routines
#  * TODO: relax dependency on our own gir when its API is stable
gir-deps = glib-2.0 gtk-3.0 gconf-2.0 gdkpixbuf-2.0 pango-1.0 rsvg-2.0
gir-deps += sugarext-1.0 gconf-2.0 atspi-2.0
python-deps = gi dbus cairo telepathy dateutil
depends = $(patsubst %,$(comma) python-%,$(python-deps))
depends +=, $(patsubst %,$(comma) gir1.2-%,$(gir-deps))
depends +=, gir1.2-webkit2-4.0 | gir1.2-webkit2-3.0 | gir1.2-webkit-3.0
depends-dev = $(girpkg) (= $(DEB_VERSION))
depends-gir = $(libpkg) (= $(DEB_VERSION))
recommends = unzip, telepathy-mission-control-5 (>= 1:5.6)
recommends +=, telepathy-gabble (>= 0.10), telepathy-salut (>= 0.4)
recommends +=, gir1.2-webkit2-4.0 | gir1.2-webkit2-3.0
suggests = git-core, subversion, ttf-dejavu-extra

# Binary sugar-activity moved from sugar-tools-* to python-sugar3
# (unneeded since Wheezy: can be dropped after Wheezy+1)
non-gtk3-branches = 0.84 0.86 0.88 0.90 0.92 0.94
obsoletes = $(foreach ver,$(non-gtk3-branches),$(comma) sugar-tools-$(ver))

CDBS_BUILD_DEPENDS +=, $(bdeps)
CDBS_DEPENDS_$(pythonpkg) +=, $(depends)
CDBS_DEPENDS_$(devpkg) = $(depends-dev)
CDBS_DEPENDS_$(girpkg) = $(depends-gir)
CDBS_RECOMMENDS_$(pythonpkg) = $(recommends)
CDBS_SUGGESTS_$(pythonpkg) = $(suggests)
CDBS_CONFLICTS_$(binpkg) = $(obsoletes)
CDBS_REPLACES_$(binpkg) = $(obsoletes)

# Multiarch quirks
# (unneeded since Wheezy: can be dropped after Wheezy+1)
CDBS_PREDEPENDS_$(libpkg) = $(if $(DEB_HOST_MULTIARCH),multiarch-support)
DEB_CONFIGURE_EXTRA_FLAGS += --libdir=\$${prefix}/lib/$(DEB_HOST_MULTIARCH)

#DEB_AUTOMAKE_ARGS = --add-missing --copy

# optimize linking
LDFLAGS += -Wl,-O1
# TODO: disallow undefined symbols
#LDFLAGS += -Wl,-z,defs
# link only when needed
LDFLAGS += -Wl,--as-needed

DEB_DH_INSTALL_ARGS_$(girpkg) = usr/lib/$(DEB_HOST_MULTIARCH)/girepository-1.0 usr/lib
DEB_DH_INSTALL_ARGS_$(datapkg) = usr/share/locale

# Drop created dir if empty to please lintian
binary-post-install/$(datapkg)::
	rmdir --ignore-fail-on-non-empty --parents $(DEB_DESTDIR)/usr/share/locale

# Let d-shlibs calculate development package dependencies
#  and handle shared library install
CDBS_BUILD_DEPENDS += , d-shlibs (>= 0.48)
binary-post-install/$(libpkg):: debian/stamp-local-shlibs-$(lib)
debian/stamp-local-shlibs-$(lib): binary-install/$(libpkg) debian/stamp-local-strip-la
	d-shlibmove --commit \
		--multiarch \
		--exclude-la --exclude-a \
		--override s/libcairo-gobject2-dev/libcairo-dev/ \
		--override s/libgdk-3-0-dev/libgtk-3-dev/ \
		--override s/libgtk-3-0-dev/libgtk-3-dev/ \
		--override s/librsvg-2-2-dev/librsvg2-dev/ \
		--override s/libgconf-2-4-dev/libgconf2-dev/ \
		--devunversioned --ignorelibdep \
		--movedev debian/tmp/usr/share/gir-1.0 usr/share/ \
		--extralib debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libsugar-eventcontroller.so \
		debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/$(lib).so
	touch $@
clean::
	rm -f debian/stamp-local-shlibs-$(lib)

# avoid unneeded .la file
binary-post-install/$(pythonpkg):: debian/stamp-local-strip-la
debian/stamp-local-strip-la: binary-install/$(pythonpkg)
	find debian -type f -name '*.la' -delete
	touch $@
clean::
	rm -f debian/stamp-local-strip-la

# Ensure proper hash-bang in Python script (Python Policy 0.9 §3.1)
#  * cdbs_expand_pythonruntime only in cdbs 0.5, but fallback is sensible
binary-fixup/$(pythonpkg)::
	egrep -r -l -Z '^#! ?/usr/bin/(env )?python[[:print:]]*$$' \
		debian/$(cdbs_curpkg)/usr/bin/* \
		| xargs -r -0 perl -pi -e 's,^#! ?/usr/bin/(env )?python[[:print:]]*$$,#!/usr/bin/python$(cdbs_expand_pythonruntime),'
