#!/usr/bin/make -f

include /usr/share/dpkg/pkg-info.mk

include /usr/share/cdbs/1/class/python-distutils.mk
include /usr/share/cdbs/1/rules/debhelper.mk

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

_ENV = $(strip \
 PYTHONPATH="$(CURDIR)/debian/tmp/usr/lib/python3/dist-packages")

# generate manpage with help2man from --help option of python script
_mkman = $(_ENV) \
 help2man $(if $3,--name "$(strip $3)") --no-info \
 --version-string="$(DEB_VERSION_UPSTREAM_REVISION)" \
 --output $2 $1 \
 || { $(_ENV) $1 --help; false; }

# Disable system daemon by default
DEB_DH_INSTALLINIT_ARGS_radicale = --no-enable

# check upstream testsuite
ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
build/python3-radicale build/radicale:: debian/stamp-pytest
debian/stamp-pytest:
	HOME="$(CURDIR)" py.test-3
	rm -rf .config
	touch $@
clean::
	rm -f debian/stamp-pytest
endif

# generate manpage based on --help option of script itself
manpages = debian/radicale.1
DEB_INSTALL_MANPAGES_radicale = $(manpages)
common-binary-indep:: $(manpages)
$(manpages): debian/%.1: $(cdbs_python_destdir)/usr/bin/% install/python3-radicale
	$(call _mkman, $<, $@, \
		a simple calendar server)
	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/

# 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

clean::
	rm -rf Radicale.egg-info
	rm -rf .eggs
