# -*- mode: makefile; coding: utf-8 -*-
# Copyright © 2008-2012, 2014, 2016 Jonas Smedegaard <dr@jones.dk>
# Description: Class to build and install Python-based Sugar packages
#
# 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/>.

#PATH_RULES#

ifndef _cdbs_class_python_sugar
_cdbs_class_python_sugar = 1

# Space-delimited list of supported branches, lowest listed first
# (comment out if all current branches are supported)
# NB! This variable must be declared *above* inclusion of this snippet
#DEB_SUGAR_BRANCHES = 0.84 0.86

# Primary branch to use when DEB_SUGAR_BRANCHES is unset
# to ensure depending on virtual packages only as fallback
#DEB_SUGAR_PRIMARY_BRANCH ?= 0.88

# include both simple and versioned activity packages by default
# NB! This variable must be declared *above* inclusion of this snippet
DEB_PYTHON_SUGAR_PACKAGES ?= $(filter \
	sugar-%-activity \
	$(call cdbs_expand_branches,\
		sugar-%-activity,\
		$(DEB_SUGAR_BRANCHES),,),\
	$(DEB_PACKAGES))

# FIXME: support multiple Python packaging systems in same binary package
#DEB_PYTHON2_MODULE_PACKAGES = $(DEB_PYTHON_SUGAR_PACKAGES)
DEB_PYTHON3_MODULE_PACKAGES = $(DEB_PYTHON_SUGAR_PACKAGES)

include $(_cdbs_class_path)/python-module.mk$(_cdbs_makefile_suffix)
include $(_cdbs_rules_path)/debhelper.mk$(_cdbs_makefile_suffix)

# Convenience variable for e.g. conflicts/provides/replaces
DEB_SUGAR_SOURCE_PKGBASE = $(strip $(if $(DEB_SOURCE_PACKAGE),\
	$(firstword $(subst -0.,$(space),$(DEB_SOURCE_PACKAGE)))))

# prepare sanity checks
cdbs_python_sugar_packages_pre := $(DEB_PYTHON_SUGAR_PACKAGES)
cdbs_python_sugar_pkgresolve_check = $(if $(call cdbs_streq,$(strip $(DEB_PYTHON_SUGAR_PACKAGES)),$(cdbs_python_sugar_packages_pre)),\
	,\
	$(call cdbs_warn,Redefining DEB_PYTHON_SUGAR_PACKAGES late  is\
		currently unsupported: set DEB_PYTHON_SUGAR_PACKAGES \
		before including python-sugar.mk))
## TODO: Rephrase when DEB_PYTHON_SUGAR_PACKAGES is only expanded inside rules
cdbs_python_sugar_pkg_check = $(if $(DEB_PYTHON_SUGAR_PACKAGES),\
	,\
	$(call cdbs_warn,No Python-based Sugar packages found or declared \
		- either rename binary packages or set \
		DEB_PYTHON_SUGAR_PACKAGES before including python-sugar.mk))

# convenience wrappers to resolve package dependencies for all Sugar branches
cdbs_sugar_allbranchdeps = $(call cdbs_expand_branches,$1,$(DEB_SUGAR_BRANCHES),$(comma) ,$(comma) ,$(DEB_SUGAR_PRIMARY_BRANCH))
cdbs_sugar_anybranchdeps = $(call cdbs_expand_branches,$1,$(DEB_SUGAR_BRANCHES),$(comma) , | ,$(DEB_SUGAR_PRIMARY_BRANCH))

CDBS_BUILD_DEPENDS_class_python-sugar ?= $(cdbs_python_builddeps_cdbs)
CDBS_BUILD_DEPENDS +=, $(CDBS_BUILD_DEPENDS_class_python-sugar)

# Python-related dependencies according to Python policy, appendix A
CDBS_BUILD_DEPENDS_class_python-sugar_python ?= $(cdbs_python_builddeps)
CDBS_BUILD_DEPENDS +=, $(CDBS_BUILD_DEPENDS_class_python-sugar_python)

# warn about wrong number of resolved packages
CDBS_BUILD_DEPENDS += $(cdbs_python_sugar_pkg_check)

# Sugar-related dependencies
CDBS_BUILD_DEPENDS_class_python-sugar_sugar ?= \
	$(call cdbs_sugar_anybranchdeps,python3-sugar3), unzip
CDBS_BUILD_DEPENDS +=, $(CDBS_BUILD_DEPENDS_class_python-sugar_sugar)

# warn early about late changes to DEB_PYTHON_SUGAR_PACKAGES
testsanity::
	$(cdbs_python_sugar_pkgresolve_check)

pre-build::
	mkdir -p debian/stamps-configure

$(patsubst %,build/%,$(DEB_PYTHON_SUGAR_PACKAGES)) :: build/%:
	$(strip [ ! -e $(cdbs_cursrcdir)/MANIFEST ] \
		|| [ -e $(cdbs_cursrcdir)/MANIFEST.upstream ] \
		|| mv $(cdbs_cursrcdir)/MANIFEST \
			$(cdbs_cursrcdir)/MANIFEST.upstream)
	$(strip [ ! -e $(cdbs_cursrcdir)/MANIFEST.upstream ] \
		|| egrep -v '^locale/.*/(.*\.mo|activity\.linfo)$$' \
			$(cdbs_cursrcdir)/MANIFEST.upstream \
			> $(cdbs_cursrcdir)/MANIFEST)
	$(strip set -e; \
		$(foreach flavor,$(cdbs_curpythonflavors),\
			$(call cdbs_expand_pythonruntime,$(cdbs_curpkg),$(flavor)) \
				$(cdbs_cursrcdir)/setup.py build; ))
	$(strip [ ! -e $(cdbs_cursrcdir)/MANIFEST.upstream ] \
		|| IFS="`printf '\n'`" find "$(cdbs_cursrcdir)/locale" \
			-type f \( -name '*.mo' -or -name 'activity.linfo' \) \
		| while read path; do \
			echo "$$path" \
				| sed 's!^$(cdbs_cursrcdir)/!!' \
				>> $(cdbs_cursrcdir)/MANIFEST; \
		done)

$(patsubst %,install/%,$(DEB_PYTHON_SUGAR_PACKAGES)) :: install/%:
	mkdir -p $(DEB_DESTDIR)usr/share/sugar/activities
	$(strip set -e; \
		$(foreach flavor,$(cdbs_curpythonflavors),\
			LANG=C XDG_DATA_HOME="$(DEB_DESTDIR)/usr/share" \
				$(call cdbs_expand_pythonruntime,$(cdbs_curpkg),$(flavor)) \
				$(cdbs_cursrcdir)/setup.py install \
				--prefix="$(DEB_DESTDIR)/usr"; ))

# Replace superfluous COPYING files with symlinks
$(patsubst %,binary-post-install/%,$(DEB_PYTHON_SUGAR_PACKAGES)) :: binary-post-install/%:
	$(strip ! test -f $(DEB_DESTDIR)/usr/share/sugar/activities/*.activity/COPYING \
		|| ! diff -wq /usr/share/common-licenses/GPL-2 \
			$(DEB_DESTDIR)/usr/share/sugar/activities/*.activity/COPYING \
		|| ln -sfT ../../../common-licenses/GPL-2 \
			$(DEB_DESTDIR)/usr/share/sugar/activities/*.activity/COPYING)

reverse-config:: $(patsubst %,cleanpythonsugar-reverse-config/%,$(DEB_PYTHON_SUGAR_PACKAGES))
$(patsubst %,cleanpythonsugar-reverse-config/%,$(DEB_PYTHON_SUGAR_PACKAGES)) :: cleanpythonsugar-reverse-config/% :
	$(strip [ ! -e $(cdbs_cursrcdir)/MANIFEST.upstream ] \
		|| mv -f $(cdbs_cursrcdir)/MANIFEST.upstream \
			$(cdbs_cursrcdir)/MANIFEST)

$(patsubst %,cleanpythonsugar/%,$(DEB_PYTHON_SUGAR_PACKAGES)) :: cleanpythonsugar/% :
	-$(strip IFS="`printf '\n'`" find "$(cdbs_cursrcdir)/locale" \
		-type f \( -name '*.mo' -or -name 'activity.linfo' \) \
		| while read path; do \
			rm -f "$$path"; \
			rmdir --ignore-fail-on-non-empty \
				"`dirname "$$path"`"; \
		done)
	-rmdir --ignore-fail-on-non-empty "$(cdbs_cursrcdir)/locale"

endif
