# -*- mode: makefile; coding: utf-8 -*-
# Copyright © 2008-2010 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 2, 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

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

# 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

# Convenience variable for e.g. conflicts/provides/replaces
DEB_SUGAR_SOURCE_PKGBASE ?= $(DEB_SOURCE_PACKAGE:%-$(firstword $(DEB_SUGAR_BRANCHES))=%)

# include both simple and versioned activity packages by default
DEB_PYTHON_SUGAR_PACKAGES ?= $(filter sugar-%-activity $(call cdbs_expand_branches,sugar-%-activity,$(DEB_SUGAR_BRANCHES), , ), $(DEB_PACKAGES))

# prepare sanity checks
cdbs_python_sugar_packages_pre := $(DEB_PYTHON_SUGAR_PACKAGES)
cdbs_python_sugar_pkgresolve_check = $(if $(call cdbs_streq,$(DEB_PYTHON_SUGAR_PACKAGES),$(cdbs_python_sugar_packages_pre)),,$(warning WARNING: 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),,$(warning WARNING: 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 expand Sugar branches for package dependencies
cdbs_sugar_allbranchdeps = $(call cdbs_expand_branches,$1,$(DEB_SUGAR_BRANCHES),$(comma) ,$(comma) )
cdbs_sugar_anybranchdeps = $(call cdbs_expand_branches,$1,$(DEB_SUGAR_BRANCHES),$(comma) , | )

CDBS_BUILD_DEPENDS_class_python-sugar ?= cdbs (>= 0.4.67~)
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 ?= $(if $(cdbs_python_arch_packages),python-all-dev,python-dev (>= 2.3.5-7)$(cdbs_python_nondefault_version:%=, python%-dev))
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,python-sugar python-sugar-toolkit), unzip
CDBS_BUILD_DEPENDS += $(comma) $(CDBS_BUILD_DEPENDS_class_python-sugar_sugar)

# warn early about late changes to DEB_PYTHON_SUGAR_PACKAGES
pre-build clean::
	$(cdbs_python_sugar_pkgresolve_check)

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

$(patsubst %,build/%,$(DEB_PYTHON_SUGAR_PACKAGES)) :: build/%:
	[ ! -e $(cdbs_cursrcdir)/MANIFEST ] || [ -e $(cdbs_cursrcdir)/MANIFEST.upstream ] || mv $(cdbs_cursrcdir)/MANIFEST $(cdbs_cursrcdir)/MANIFEST.upstream
	[ ! -e $(cdbs_cursrcdir)/MANIFEST.upstream ] || egrep -v '^locale/.*/(.*\.mo|activity\.linfo)$$' $(cdbs_cursrcdir)/MANIFEST.upstream > $(cdbs_cursrcdir)/MANIFEST
	set -e; for buildver in $(cdbs_python_build_versions); do \
		$(call cdbs_python_binary,python$$buildver) $(cdbs_cursrcdir)/setup.py build; \
	done
	[ ! -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
	set -e; for buildver in $(cdbs_python_build_versions); do \
		LANG=C $(call cdbs_python_binary,python$$buildver) $(cdbs_cursrcdir)/setup.py install --prefix="$(DEB_DESTDIR)/usr"; \
	done

# Replace superfluous COPYING files with symlinks
$(patsubst %,binary-post-install/%,$(DEB_PYTHON_SUGAR_PACKAGES)) :: binary-post-install/%:
	! test -f $(DEB_DESTDIR)/usr/share/sugar/activities/*.activity/COPYING \
	|| ! diff -q /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/% : 
	[ ! -e $(cdbs_cursrcdir)/MANIFEST.upstream ] || mv -f $(cdbs_cursrcdir)/MANIFEST.upstream $(cdbs_cursrcdir)/MANIFEST

$(patsubst %,cleanpythonsugar/%,$(DEB_PYTHON_SUGAR_PACKAGES)) :: cleanpythonsugar/% : 
	-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
