#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-
# Copyright © 2010-2016, Jonas Smedegaard <dr@jones.dk>
# Description: Debian packaging script for Radicale
#
# 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/upstream-tarball.mk
include /usr/share/cdbs/1/rules/utils.mk
include /usr/share/cdbs/1/class/python-distutils.mk
include /usr/share/cdbs/1/rules/debhelper.mk

DEB_UPSTREAM_URL = https://github.com/Kozea/Radicale/archive
DEB_UPSTREAM_TARBALL_BASENAME = $(DEB_UPSTREAM_TARBALL_VERSION)

# workaround: python-distutils.mk by default adds unusual option -a
DEB_PYTHON_CLEAN_ARGS =

# Avoid unneeded build-dependency on python-dev
CDBS_BUILD_DEPENDS_class_python-distutils_python =

# Needed for upstream build
CDBS_BUILD_DEPENDS +=, python-setuptools, python3-setuptools

# Needed for upstream testsuite
CDBS_BUILD_DEPENDS +=, python-pytest

# check upstream testsuite
ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
build/python-radicale build/radicale:: debian/stamp-pytest
debian/stamp-pytest:
	py.test
	touch $@
clean::
	rm -f debian/stamp-pytest
endif

# generate manpage based on --help option of script itself
#  * TODO: Drop PYTHONPATH wildcard when cdbs 0.4.131 is in oldstable
CDBS_BUILD_DEPENDS +=, help2man
manpages = debian/radicale.1
DEB_INSTALL_MANPAGES_radicale = $(manpages)
common-binary-indep:: $(manpages)
$(manpages): debian/%.1: bin/% install/python-radicale
	export PYTHONPATH="$(wildcard $(cdbs_python_destdir)/usr/lib/python$(or $(cdbs_python_nondefault_version),$(cdbs_python_current_version))/*-packages/)"; \
	  help2man \
		--name="a simple calendar server" \
		--version-string="$(DEB_NOEPOCH_VERSION)" \
		--no-info \
		--output=$@ \
		$(cdbs_python_destdir)/usr/$< \
		|| { $(cdbs_python_destdir)/usr/$< --help; false; }
	find "$(cdbs_python_destdir)/usr/lib" -name '*.pyc' -delete
	find "$(cdbs_python_destdir)/usr/lib" -type d -empty -delete
clean::
	rm -f $(manpages)
	rm -rf build/

# Ensure proper hash-bang in Python script (Python Policy 0.9 §3.1)
binary-fixup/radicale::
	egrep -r -l -Z '^#! ?/usr/bin/(env )?python[[:print:]]*$$' \
		debian/$(cdbs_curpkg)/usr/bin/* \
		debian/$(cdbs_curpkg)/usr/share/radicale/* \
		| xargs -r -0 perl -pi -e 's,^#! ?/usr/bin/(env )?python[[:print:]]*$$,#!/usr/bin/python$(cdbs_python_nondefault_version),'

# fix unset execution bit on FCGI and WSGI scripts
binary-fixup/radicale::
	chmod -x \
		debian/$(cdbs_curpkg)/usr/share/radicale/radicale.fcgi \
		debian/$(cdbs_curpkg)/usr/share/radicale/radicale.wsgi
