#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-
# Copyright 2007-2010, 2012, 2015 Jonas Smedegaard <dr@jones.dk>
# Description: Main Debian packaging script for sugar
#
# 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/>.

# These need to be declared/run before CDBS includes
pkgbranch = $(lastword $(subst -,$(space),$(DEB_SOURCE_PACKAGE)))
DEB_BUILDDIR = build
DEB_PYTHON2_MODULE_PACKAGES = python-jarabe-$(pkgbranch) sugar-session-$(pkgbranch)
debian/control:: debian/control.in
DEB_PHONY_RULES += debian/control.in
debian/control.in::
	sed -e 's/__BRANCH__/$(pkgbranch)/g' <debian/control.in.in >debian/control.in

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

# Suppress unneeded auto-resolved build-dependency on python-dev
CDBS_BUILD_DEPENDS_class_python-autotools_python = python$(cdbs_python_nondefault_version)

CDBS_BUILD_DEPENDS +=, libgtk-3-dev

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

DEB_PYTHON_PRIVATE_MODULES_DIRS = /usr/share/sugar

# Generate (and cleanup) files containing variables static per build
infiles = $(filter-out debian/control.in%, $(wildcard debian/*.in))
outfiles = $(subst __VER__,$(pkgbranch),$(basename $(infiles)))
DEB_PHONY_RULES += $(outfiles)
pre-build:: $(outfiles)
$(outfiles):
	sed 's/__VER__/$(pkgbranch)/' <$(subst $(pkgbranch),__VER__,$@).in >$@
clean::
	rm -f $(outfiles)

# Register GConf schemas
post-install/sugar-session-$(pkgbranch)::
	mkdir -p $(DEB_DESTDIR)/usr/share/gconf/schemas/
	mv $(DEB_DESTDIR)/etc/gconf/schemas/sugar.schemas $(DEB_DESTDIR)/usr/share/gconf/schemas/
	rmdir $(DEB_DESTDIR)/etc/gconf/schemas
	rmdir $(DEB_DESTDIR)/etc/gconf
binary-install/sugar-session-$(pkgbranch)::
	dh_gconf -p$(cdbs_curpkg)

# Needed by upstream build process
CDBS_BUILD_DEPENDS += , libglib2.0-dev, python-gtk2-dev, libgconf2-dev, gettext, intltool, shared-mime-info

comma = ,

# $(1): activity stems
# $(2): default branch (optional)
# $(3): fallback flag (if non-empty unversioned fallback is added)
sugar_expand_activities = $(foreach stem,$(1),$(comma) $(patsubst %,sugar-$(stem)-activity-%$(if $(3), | ),$(2))$(if $(if $(2),,true)$(3),sugar-$(stem)-activity))

# Fructose activities
session_expand_activities += $(call sugar_expand_activities,pippy turtleart,,$(1))
# Fructose activities not avilable in Debian main for this Sugar branch
session_expand_activities_relaxed += $(call sugar_expand_activities,calculate etoys,,$(1))
session_expand_activities_relaxed += $(call sugar_expand_activities,browse chat log write,0.86,$(1))
session_expand_activities_relaxed += $(call sugar_expand_activities,jukebox imageviewer,$(pkgbranch),$(1))

# Needed (always/often/sometimes) at runtime
#  * dbus-x11 (bus.get_object) used in sugar-launch, exensions/* and jarabe.*
#  * python-carquinyol-* (org.laptop.sugar.DataStore) used in
#    jarabe.apisocket and jarabe.journal.model
#  * metacity needed in jarabe.main
#  * lsb-release (lsb_release), tzdata and ethtool needed in extension
#    ControlPanel
#  * avahi-autoipd needed in jarabe/desktop/meshbox.py to establish
#    NM_802_11_MODE_ADHOC
#  * openssh-client (ssh-keygen) needed in
#    intro.intro.IntroWindow()
#  * olpc-powerd (/etc/powerd/flags) needed in extensions.cpsection.power.model
#  * gdb needed by command sugar-launch
#  * gvfs needed for GIO removable media support
#  * upower needed in extensions/deviceicon/battery.py
#  * consolekit or systemd needed in jarabe.model.session
#  * xdg-user-dir needed in
#    jarabe/journal/model.get_documents_path()
#  * TODO: Tighten to recommend / depend on Maliit when maturing
session_nm = network-manager, modemmanager
session_gio = gvfs
session_pm = upower
session_keyboard = gir1.2-maliit-1.0, maliit-keyboard
session_font = fonts-dejavu-core
jarabe_initprofile = openssh-client
jarabe_wm = metacity
jarabe_nm = avahi-autoipd
jarabe_cp_cli_exec = lsb-release, ethtool, tzdata
jarabe_fs = xdg-user-dirs
jarabe_help = gir1.2-webkit-3.0
tools_debug = gdb
CDBS_DEPENDS_sugar-session-$(pkgbranch) = python-sugar3, sugar-artwork-$(pkgbranch), python-jarabe-$(pkgbranch)
CDBS_DEPENDS_sugar-session-$(pkgbranch) +=, gir1.2-glib-2.0, gir1.2-gtk-3.0, python-dbus
CDBS_DEPENDS_sugar-session-$(pkgbranch) += , gir1.2-pango-1.0
CDBS_DEPENDS_sugar-session-$(pkgbranch) +=, gir1.2-gconf-2.0, python-cairo, gir1.2-xkl-1.0
CDBS_DEPENDS_sugar-session-$(pkgbranch) +=, gir1.2-networkmanager-1.0, gir1.2-nmgtk-1.0
CDBS_DEPENDS_python-jarabe-$(pkgbranch) = python-sugar3, python-gwebsockets
CDBS_DEPENDS_python-jarabe-$(pkgbranch) +=, gir1.2-gdkpixbuf-2.0, gir1.2-glib-2.0, gir1.2-gtk-3.0, python-cairo, python-dbus, python-telepathy, gir1.2-gtksource-3.0, python-xapian, gir1.2-gstreamer-1.0
CDBS_DEPENDS_python-jarabe-$(pkgbranch) += , gir1.2-wnck-3.0, gir1.2-sugarext-1.0, gir1.2-pango-1.0
CDBS_DEPENDS_python-jarabe-$(pkgbranch) +=, gir1.2-gconf-2.0, gir1.2-xkl-1.0, gir1.2-soup-2.4
CDBS_DEPENDS_python-jarabe-$(pkgbranch) +=, $(jarabe_wm), $(jarabe_help)
CDBS_RECOMMENDS_sugar-session-$(pkgbranch) =, dbus-x11, python-carquinyol-$(pkgbranch)
CDBS_RECOMMENDS_sugar-session-$(pkgbranch) += , $(call session_expand_activities,true)
CDBS_RECOMMENDS_sugar-session-$(pkgbranch) += , $(session_nm), $(session_gio), $(session_pm), $(jarabe_cp_cli_exec), $(session_font)
CDBS_RECOMMENDS_python-jarabe-$(pkgbranch) = python-carquinyol-$(pkgbranch)
CDBS_RECOMMENDS_python-jarabe-$(pkgbranch) +=, dbus-x11, $(session_nm), $(jarabe_nm), $(jarabe_initprofile), $(jarabe_fs), consolekit | systemd
CDBS_SUGGESTS_sugar-session-$(pkgbranch) = sucrose-$(pkgbranch), $(call session_expand_activities_relaxed,true)
CDBS_SUGGESTS_sugar-session-$(pkgbranch) +=, $(session_keyboard), $(tools_debug), olpc-powerd
CDBS_SUGGESTS_python-jarabe-$(pkgbranch) = sugar-session-$(pkgbranch)

CDBS_DEPENDS_sucrose-$(pkgbranch) = dbus-x11, sugar-session-$(pkgbranch), python-carquinyol-$(pkgbranch)
CDBS_DEPENDS_sucrose-$(pkgbranch) += , $(call session_expand_activities)
CDBS_DEPENDS_sucrose-$(pkgbranch) += , $(session_nm), $(session_gio), $(session_pm), consolekit | systemd, $(session_font), $(jarabe_nm), $(jarabe_initprofile), $(jarabe_cp_cli_exec), $(jarabe_fs), $(tools_debug)
CDBS_SUGGESTS_sucrose-$(pkgbranch) = $(call session_expand_activities_relaxed), $(session_keyboard)

# Ensure only one variant is installed at a time (Debian Policy 7.6.2)
CDBS_PROVIDES_python-jarabe-$(pkgbranch) = python-jarabe
CDBS_CONFLICTS_python-jarabe-$(pkgbranch) = python-jarabe
CDBS_REPLACES_python-jarabe-$(pkgbranch) = python-jarabe
CDBS_PROVIDES_sugar-session-$(pkgbranch) = sugar-session
CDBS_CONFLICTS_sugar-session-$(pkgbranch) = sugar-session
CDBS_REPLACES_sugar-session-$(pkgbranch) = sugar-session

CDBS_BREAKS_sugar-session-$(pkgbranch) = sugar-emulator
CDBS_BREAKS_python-jarabe-$(pkgbranch) = sugar-emulator

# Provide virtual unversioned sugar package
CDBS_PROVIDES_sugar-session-$(pkgbranch) +=, sugar

# Needed for our packaging
CDBS_BUILD_DEPENDS += , python-empy

# Transitional hints: sugar-tools-* merged into sugar-session-*
# TODO: drop when no sugar-tools-* are in stable
CDBS_REPLACES_sugar-session-$(pkgbranch) +=, sugar-tools
CDBS_BREAKS_sugar-session-$(pkgbranch) +=, sugar-tools

# Ensure optimal hash-bang + dependency for scripts (see Python Policy)
#  * TODO: drop cdbs_python_binary fallback when cdbs 0.5 is stable
CDBS_DEPENDS_sugar-session-$(pkgbranch) +=, python
binary-fixup/sugar-session-$(pkgbranch)::
	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/$(strip $(or $(cdbs_curpythonruntime),$(cdbs_python_binary))),'
