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

include /usr/share/cdbs/1/rules/autoreconf.mk
include /usr/share/cdbs/1/class/autotools.mk
include /usr/share/cdbs/1/rules/debhelper.mk

pkgbranch = $(subst $(space),.,$(wordlist 1, 2,$(subst .,$(space),$(DEB_UPSTREAM_VERSION))))
pkg = sugar-themes
pkg-icon = sugar-icon-theme
pkg-gtk2 = gtk2-engines-sugar

# Adapt packaging in maintainer mode when targeting experimental branch:
#  * Switch git packaging branch
#  * Track any release (else only current branch)
ifneq (,$(DEB_MAINTAINER_MODE))
experimental := $(strip $(if \
	$(shell dpkg-parsechangelog \
		| grep -x 'Distribution: \(experimental\|UNRELEASED\)'),\
	yes))
watch-limit = $(if $(experimental),,$(pkgbranch)\\.)
debian-branch = $(if $(experimental),master,master-$(pkgbranch))
clean::
	perl -i -pe 's/\([\d.\\]*\.\*\)/($(watch-limit).*)/' \
		debian/watch
	perl -i -pe 's/^debian-branch *= *\K.*/$(debian-branch)/' \
		debian/gbp.conf
endif

# Needed by upstream build process
bdeps = libgtk2.0-dev, libgtk-3-dev, libglib2.0-dev, libcairo2-dev
bdeps +=, python-empy, icon-slicer, icon-naming-utils

# Needed by our packaging
bdeps +=, dh-autoreconf, gnome-common

# Needed (always/often) at runtime
#  * librsvg2-common used with GTK3
depends = $(pkg-icon)
recommends = fonts-dejavu-core, librsvg2-common
suggests = $(pkg-gtk2)

CDBS_BUILD_DEPENDS +=, $(bdeps)
CDBS_DEPENDS_$(pkg) +=, $(depends)
CDBS_RECOMMENDS_$(pkg) = $(recommends)
CDBS_SUGGESTS_$(pkg) = $(suggests)

# Transitional quirk: split into engine and theme packages
#  * TODO: Drop when stretch is obsolete
obsoletes +=, sugar-artwork-0.88 (<= 0.88.1-4)
define sugar-relations
CDBS_REPLACES_$($1) +=, $(obsoletes)
CDBS_BREAKS_$($1) +=, $(obsoletes)
endef
$(foreach p,pkg pkg-icon pkg-gtk2,$(eval $(call sugar-relations,$(value p))))

# Multiarch quirks
DEB_CONFIGURE_EXTRA_FLAGS += --libdir=\$${prefix}/lib/$(DEB_HOST_MULTIARCH)

DEB_DH_INSTALL_ARGS_ALL = --autodest
DEB_DH_INSTALL_ARGS_$(pkg) = /usr/share/themes
DEB_DH_INSTALL_ARGS_$(pkg-icon) = usr/share/icons
DEB_DH_INSTALL_ARGS_$(pkg-gtk2) = usr/lib/$(DEB_HOST_MULTIARCH)/gtk-2.0/*/engines/*.so

# Avoid empty changelog
DEB_INSTALL_CHANGELOGS_ALL =

# Register icons for optional caching
binary-post-install/$(pkg-icon)::
	dh_icons -p$(cdbs_curpkg)
