#!/usr/bin/make -f

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

# TODO: Use only java-common (not java-helper) when bug#814783 is fixed in stable)
-include /usr/share/javahelper/java-vars.mk
-include /usr/share/java/java_defaults.mk

# the equivalent of "uwsgi-core --dot-h | md5sum" at runtime
abi = $(firstword $(shell echo | cat uwsgi.h - | md5sum))

# resolve supported architectures for arch-varying build-dependencies
# (in maintainer mode only: uses network and messes with control file)
ifneq (,$(DEB_MAINTAINER_MODE))
$(shell sh debian/pkgarchs.sh glusterfs-common > debian/ARCHS_glusterfs)
$(shell sh debian/pkgarchs.sh python-greenlet-dev > debian/ARCHS_greenlet)
$(shell sh debian/pkgarchs.sh openjdk-17-jdk > debian/ARCHS_openjdk17)
$(shell sh debian/pkgarchs.sh librados-dev > debian/ARCHS_rados)
$(shell sh debian/pkgarchs.sh gccgo > debian/ARCHS_gccgo)
endif
UWSGI_ARCHLISTS = glusterfs greenlet openjdk17 rados gccgo
$(foreach list,$(UWSGI_ARCHLISTS),\
	$(eval UWSGI_ARCHS_$(list) := $(shell cat debian/ARCHS_$(list))))
ifneq (,$(DEB_MAINTAINER_MODE))
debian/control::
	perl -sgpi \
	 -e 'BEGIN { %re = (' \
	 -e ' glusterfs => qr/glusterfs/,' \
	 -e ' greenlet  => qr/(gevent|greenlet)/,' \
	 -e ' openjdk17 => qr/jdk/,' \
	 -e ' rados     => qr/rados/,' \
	 -e ' gccgo     => qr/gccgo/,' \
	 -e ')}' \
	 -e 'while ( my ($$key, $$pat) = each %re ) {' \
	 -e ' my $$list = $${$$key};' \
	 -e ' s/^# autogenerated:.+\n\s+\S*$$pat.*\[\K[^\]\n]*/$$list/gm;' \
	 -e ' s/^Package:.*$$pat.*\n# autogenerated:.+\nArchitecture:\s+\K[^\n]*/$$list/gm;' \
	 -e '};' \
	 -- $(foreach list,$(UWSGI_ARCHLISTS),-$(list)='$(UWSGI_ARCHS_$(list))') \
	 -- $@
endif

# Common variables
# ================

UWSGI_ENV = CFLAGS="$(CFLAGS)" CPPFLAGS="$(CPPFLAGS)" LDFLAGS="$(LDFLAGS)"

UWSGI_BUILDER = uwsgiconfig.py -v

# Transform plugin's package name to plugin's name:
# * delete dots
# * replace '-' with '_'
# * strip trailing openjdk delimiter
pkg_name_to_plugin_name = $(strip \
	$(subst openjdk_,openjdk,$(subst -,_,$(subst .,,$(1)))))

UWSGI_PLUGIN_INFILES := $(wildcard debian/*__PLUGIN__*.in)
UWSGI_PLUGINLANGS = python java lua ruby

UWSGI_KINDS_python = python3
UWSGI_SRCPLUGINS_LANG_SELF += python
UWSGI_SRCPLUGINS_python = asyncio gevent greenlet tornado

# TODO: avoid non-default java kinds until spearate package uwsgi-java added
UWSGI_KINDS_java = openjdk-17
UWSGI_DEFAULTKIND_java = openjdk-17
UWSGI_SRCPLUGINS_java = jvm jwsgi ring servlet

UWSGI_KINDS_lua = lua5.1
UWSGI_DEFAULTKIND_lua = lua5.1
UWSGI_SRCPLUGINS_LANG_SELF += lua

# Ruby kinds resolved as per packaging team policy (see bug#755779)
# TODO: simplify when ruby-all-dev is in stable
# TODO: maybe simplify to call "dh_ruby --print-supported" when stable
UWSGI_DEFAULTKIND_ruby := $(notdir $(shell readlink -f /usr/bin/ruby))
UWSGI_KINDS_ruby := $(strip \
	$(if $(wildcard /usr/lib/ruby/vendor_ruby/ruby_debian_dev.rb),\
		$(shell ruby -rruby_debian_dev \
			-e 'include RubyDebianDev;' \
			-e 'SUPPORTED_RUBY_VERSIONS.select! do |version, binary|;' \
				-e 'puts version;' \
			-e 'end'),\
		$(UWSGI_DEFAULTKIND_ruby)))
# dirty hack: use only default kind even where multiple should work
# TODO: support multiple Ruby kinds (requires dynamic control file sections)
UWSGI_KINDS_ruby := $(filter $(UWSGI_DEFAULTKIND_ruby),$(UWSGI_KINDS_ruby))
UWSGI_KIND_ruby := $(UWSGI_KINDS_ruby)$(strip $(if $(strip \
	$(filter 1,$(words $(UWSGI_KINDS_ruby)))),\
		,\
		$(error ERROR: multiple Ruby kinds not supported)))
UWSGI_SRCPLUGINS_ruby = rack

UWSGI_PLUGIN_PACKAGES = $(filter uwsgi-plugin-%,$(DEB_PACKAGES))

UWSGI_SRCPLUGINS_AVAILABLE := $(sort $(notdir $(wildcard plugins/*)))
UWSGI_SRCPLUGINS_ZEROMQ = emperor_zeromq logzmq mongrel2
UWSGI_SRCPLUGINS_ARCH_gccgo = gccgo
# TODO: enable PyPy when supported by CDBS
UWSGI_SRCPLUGINS_TODO = pypy
# plugins not yet ready for production use
UWSGI_SRCPLUGINS_EXPERIMENTAL = libffi libtcc router_spnego
# plugins irrelevant/unusable for Debian
UWSGI_SRCPLUGINS_ALIEN = airbrake alarm_speech matheval mongodb \
    stackless emperor_mongodb gridfs mongodblog mono php \
	stats_pusher_mongodb v8
UWSGI_SRCPLUGINS_DEMO = cplusplus dumbloop dummy example exception_log
UWSGI_SRCPLUGINS_UNKNOWN = legion_cache_fetch objc_gc pyuwsgi ruby19
UWSGI_SRCPLUGINS_EXCLUDE = $(UWSGI_SRCPLUGINS_ALIEN) \
	$(UWSGI_SRCPLUGINS_DEMO) $(UWSGI_SRCPLUGINS_EXPERIMENTAL) \
	$(UWSGI_SRCPLUGINS_TODO) $(UWSGI_SRCPLUGINS_UNKNOWN)
UWSGI_SRCPLUGINS_WANTED = $(filter-out $(UWSGI_SRCPLUGINS_EXCLUDE),\
	$(UWSGI_SRCPLUGINS_AVAILABLE))
UWSGI_SRCPLUGINS_ADDON_LANG = $(call pkg_name_to_plugin_name,\
	$(UWSGI_SRCPLUGINS_LANG_SELF) \
	$(foreach lang,$(UWSGI_PLUGINLANGS),$(UWSGI_SRCPLUGINS_$(lang))))
UWSGI_SRCPLUGINS_ADDON_MISC = alarm_curl alarm_xmpp curl_cron \
	emperor_pg fiber gccgo geoip glusterfs graylog2 ldap psgi \
	rados rbthreads router_access sqlite3 xslt
# tun/tap is unavailable on the Hurd
# systemd is available only on linux
# tuntap plugin broken on kFreeBSD: https://github.com/unbit/uwsgi/issues/695
UWSGI_SRCPLUGINS_SKIP = \
	$(foreach list,$(UWSGI_ARCHLISTS),\
		$(if $(filter-out $(UWSGI_ARCHS_$(list)),$(DEB_HOST_ARCH)),\
			$(UWSGI_SRCPLUGINS_ARCH_$(list)))) \
	$(if $(filter hurd,$(DEB_HOST_ARCH_OS)),\
		tuntap) \
	$(if $(filter kfreebsd,$(DEB_HOST_ARCH_OS)),\
		tuntap) \
	$(if $(filter-out linux,$(DEB_HOST_ARCH_OS)),\
		systemd_logger)
# coroae is installed with psgi package
UWSGI_SRCPLUGINS_SKIPINSTALL = coroae
UWSGI_SRCPLUGINS_CORE = $(filter-out \
	$(UWSGI_SRCPLUGINS_ADDON_LANG) $(UWSGI_SRCPLUGINS_ADDON_MISC) \
	$(UWSGI_SRCPLUGINS_SKIP),\
	$(UWSGI_SRCPLUGINS_WANTED))
UWSGI_SRCPLUGINS_CORE_INSTALL = $(filter-out \
	$(UWSGI_SRCPLUGINS_SKIPINSTALL),\
	$(UWSGI_SRCPLUGINS_CORE))

# types of packaging scripts to use (tried from left to right)
# * pri: binary and library
# * sec: only binary
# * aux: only library
# * *-py: same as above but for (potentially) multiple python flavors
# * openjdk: non-java code linked against OpenJDK
# * python: python hooks
# * single: only one flavor exist
UWSGI_PKGSCRIPTS_DEFAULT = sec
UWSGI_PKGSCRIPTS_jvm = openjdk pri
UWSGI_PKGSCRIPTS_jwsgi = openjdk aux
UWSGI_PKGSCRIPTS_ring = openjdk aux
UWSGI_PKGSCRIPTS_servlet = openjdk aux
UWSGI_PKGSCRIPTS_lua = pri
UWSGI_PKGSCRIPTS_rack = pri
UWSGI_PKGSCRIPTS_rbthreads = single aux
UWSGI_PKGSCRIPTS_fiber = single aux
UWSGI_PKGSCRIPTS_python = python pri-py pri
UWSGI_PKGSCRIPTS_gevent = python aux-py aux
UWSGI_PKGSCRIPTS_greenlet = python aux-py aux
UWSGI_PKGSCRIPTS_tornado = python aux-py aux
UWSGI_PKGSCRIPTS_asyncio = python aux-py aux

# Stem-based variables
# ====================

pkg_name = $(cdbs_curpkg)

plugin_name = $(call pkg_name_to_plugin_name,$*)
plugin_is_lang = $(strip $(or \
	$(foreach lang,$(UWSGI_PLUGINLANGS),\
		$(if $(filter $(UWSGI_KINDS_$(lang)),$*),$(lang)))))
plugin_implies_lang = $(strip $(or \
	$(if $(filter rbthreads fiber,$*),\
		ruby)))
plugin_has_lang = $(strip $(foreach lang,$(UWSGI_PLUGINLANGS),\
	$(foreach srcplugin,$(UWSGI_SRCPLUGINS_$(lang)),\
		$(foreach kind,$(UWSGI_KINDS_$(lang)),\
			$(if $(filter $(srcplugin)-$(kind),$*),\
				$(lang))))))
plugin_lang = $(strip $(or \
	$(plugin_is_lang),\
	$(plugin_implies_lang),\
	$(plugin_has_lang)))
plugin_stem = $(strip $(or \
	$(plugin_is_lang),\
	$(foreach kind,$(UWSGI_KINDS_$(plugin_has_lang)),\
		$(patsubst %-$(kind),%,$(filter %-$(kind),$*)))))
plugin_kind = $(strip $(or \
	$(if $(plugin_is_lang),\
		$*),\
	$(foreach kind,$(UWSGI_KINDS_$(plugin_has_lang)),\
		$(if $(filter %-$(kind),$*),\
			$(kind)))))
plugin_src = $(strip \
	$(call pkg_name_to_plugin_name,$(or \
		$(plugin_stem),\
		$*)))
plugin_lang_stem = $(strip \
	$(if $(filter java,$(plugin_lang)),\
		$(firstword $(subst -, ,$(plugin_kind)))))
plugin_lang_kind = $(strip \
	$(if $(filter java python,$(plugin_lang)),\
		$(lastword $(subst -, ,$(plugin_kind)))))
# workaround for #868752
#plugin_flavors = $(strip \
#	$(if $(filter python,$(plugin_lang)),\
#		$(cdbs_$(plugin_lang_kind:python=python2)_allflavors)))
plugin_flavors = $(plugin_defaultflavor)
# workaround end
plugin_defaultflavor = $(strip \
	$(if $(filter python,$(plugin_lang)),\
		$(cdbs_$(plugin_lang_kind:python=python2)_defaultflavor)))
plugin_interpreter = $(strip $(or \
	$(if $(filter ruby,$(plugin_lang)),\
		/usr/bin/$(strip $(or \
			$(plugin_kind),\
			$(UWSGI_DEFAULTKIND_ruby))))))
plugin_alternatives_name = uwsgi-plugin-$(strip $(or \
	$(if $(filter python,$(plugin_is_lang)),\
		$(plugin_lang_kind)),\
	$(if $(filter python,$(plugin_has_lang)),\
		$(plugin_stem)-$(plugin_lang_kind)),\
	$(plugin_stem),\
	$(plugin_name)))
plugin_alternatives_stem = $(strip $(or \
	$(if $(filter python,$(plugin_is_lang)),\
		$(plugin_lang_kind)),\
	$(plugin_stem)))
plugin_alternatives_priority = $(strip \
	$(if $(filter $(plugin_kind),$(UWSGI_DEFAULTKIND_$(plugin_lang))),\
		75,\
		35))
plugin_flavor_prefix = $(strip \
	$(if $(filter python,$(plugin_is_lang)),\
		,\
		$(plugin_stem)_))

# per-infile variables, relying on $(infile) and optional $(plugin_src)
plugin_pkgtypes = $(or $(UWSGI_PKGSCRIPTS_$(plugin_src)),$(UWSGI_PKGSCRIPTS_DEFAULT))
plugin_has_binary = $(filter pri sec,$(plugin_pkgtypes))
# plugin_infile picks first existing infile variant:
# 1. $(infile).$(UWSGI_PKGSCRIPTS_$(plugin_src))
# 2. $(infile).$(UWSGI_PKGSCRIPTS_DEFAULT)
# 3. $(infile)
plugin_infile = $(firstword $(wildcard \
	$(foreach suffix,$(plugin_pkgtypes),\
		$(addsuffix .$(suffix),$(infile))) \
	$(infile) \
	))

# Package-specific variables
# ==========================

DEB_COMPRESS_EXCLUDE_uwsgi-core = .ini .lua .pl .png .psgi .py .ru .ws .xml \
                                  .yml
DEB_COMPRESS_EXCLUDE_uwsgi-extra = .class .java .rb .c

DEB_DH_STRIP_ARGS_ALL := $(strip $(subst COND,(<< 2.0.26-3~),\
 --dbgsym-migration='uwsgi-dbg COND'))

DEB_INSTALL_DOCS_ALL += $(DEB_SRCDIR)/CONTRIBUTORS

DEB_DH_INSTALL_ARGS_uwsgi-core = \
	$(addsuffix _plugin.so,$(UWSGI_SRCPLUGINS_CORE_INSTALL)) \
	usr/lib/uwsgi/plugins

DEB_DH_INSTALL_ARGS_uwsgi-plugin-psgi = coroae_plugin.so usr/lib/uwsgi/plugins

# FIXME: install in separate package uwsgi-java
DEB_DH_INSTALL_ARGS_uwsgi-plugin-jvm-$(UWSGI_DEFAULTKIND_java) = plugins/jvm/uwsgi.jar /usr/share/java

DEB_DH_GENCONTROL_ARGS_uwsgi-core = -- -V'uwsgi:Provides=uwsgi-abi-$(abi)'

# Rules
# =====

clean::
	$(cdbs_python3_defaultruntime) $(UWSGI_BUILDER) --clean
	find -type f -name '*.pyc' -delete
	find -type d -name __pycache__ -delete
	find -type f \( -name '*.class' -or -name '*.jar' \) -delete
	rm -f debian/stamp-*

# add per-package hook to expand packaging files as patch before configure
apply-patches:: \
 $(patsubst %,debian/expand-files/%,$(DEB_ALL_PACKAGES))
$(patsubst %,debian/expand-files/%,$(DEB_ALL_PACKAGES)): \
 debian/expand-files/%: \
 | debian/expand-files
debian/expand-files: post-patches
	mkdir debian/expand-files
clean::
	rm -rf debian/expand-files

# uwsgi-core package
# ------------------

substvars-list-encode = perl -0 -F'/\s+/' -ane 'print " * ", join("\$${Newline} * ",@F)'
uwsgi-coreplugins := $(call pkg_name_to_plugin_name,$(UWSGI_SRCPLUGINS_CORE_INSTALL))
uwsgi-corepluginlist := $(shell echo $(uwsgi-coreplugins) | $(substvars-list-encode))
build/uwsgi-core:: bin/uwsgi-core
bin/uwsgi-core: bin/uwsgi-core-local
	$(UWSGI_ENV) $(cdbs_python3_defaultruntime) $(UWSGI_BUILDER) \
		--build debian/buildconf/uwsgi-core.ini
	
	set -e; \
	for PLUGIN_NAME in $(UWSGI_SRCPLUGINS_CORE); do \
		$(UWSGI_ENV) $(cdbs_python3_defaultruntime) $(UWSGI_BUILDER) \
		--plugin plugins/$${PLUGIN_NAME} \
		debian/buildconf/uwsgi-plugin.ini \
		$${PLUGIN_NAME}; \
	done
install/uwsgi-core::
	echo 'uwsgi:corepluginlist=$(uwsgi-corepluginlist)' >> debian/$(cdbs_curpkg).substvars

uwsgi-rubyplugins = $(foreach kind,$(UWSGI_KINDS_ruby),\
	$(patsubst %,%-$(kind),$(UWSGI_SRCPLUGINS_ruby)))
uwsgi-rubyplugins += rbthreads fiber
uwsgi-rubypkghint = $(patsubst %,uwsgi-plugin-%,$(uwsgi-rubyplugins))
uwsgi-rubypkghint += uwsgi-plugins-all uwsgi-app-integration-plugins
$(patsubst %,install/%,$(uwsgi-rubypkghint))::
	echo 'uwsgi:RubyKinds=$(UWSGI_KINDS_ruby)' >> debian/$(cdbs_curpkg).substvars
	echo 'uwsgi:RubyKind=$(UWSGI_KIND_ruby)' >> debian/$(cdbs_curpkg).substvars
	echo 'uwsgi:RubyDefaultkind=$(UWSGI_DEFAULTKIND_ruby)' >> debian/$(cdbs_curpkg).substvars

# move installed examples into subdirs by type
ex_types = conffile psgi rack router wsapi wsgi
ex_conffile = mega.xml multi.* sites.xml uwsgi.xml vassals werkzeug_strict.yml werkzeug.*
ex_rack = config.ru config2.ru fibers.*
ex_router = router.lua uwsgirouter*
ex_psgi = mojoapp.pl
ex_wsapi = *.ws
ex_wsgi = mjpeg_stream.py multiapp.py simple_app.py simple_app_wsgi2.py taskqueue.py
ex_drop = bootstrap* config*.lua config??.ru corostream.pl debug.ini
ex_drop += flaskpost.py heavytest.* info_uwsgi.php logic.ini
ex_drop += protected.ini welcome.ini welcome3.py
binary-post-install/uwsgi-core::
	set -e; \
	cd debian/$(cdbs_curpkg)/usr/share/doc/$(cdbs_curpkg)/examples; \
	$(foreach type, $(ex_types),mkdir $(type); mv -t $(type) $(ex_$(type));) \
	rm $(ex_drop)

install/uwsgi-core::
	help2man \
		--name 'fast (pure C), self-healing, developer-friendly WSGI server' \
		--section 1 \
		--no-info \
		bin/uwsgi-core \
		> debian/uwsgi-core.1

# uwsgi-plugin-% packages
# -----------------------

# setup hook for building plugins
$(patsubst %,build/%,$(UWSGI_PLUGIN_PACKAGES)):: \
 build/uwsgi-plugin-%: \
 bin/uwsgi-core-local debian/stamp-uwsgi-plugin-%

bin/uwsgi-core-local:
	$(UWSGI_ENV) $(cdbs_python3_defaultruntime) $(UWSGI_BUILDER) \
		--build debian/buildconf/uwsgi-plugin.ini

#  * eval'ing JAVA_HOME needed to resolve JVM_SERVER_DIR
$(patsubst %,debian/stamp-%,$(UWSGI_PLUGIN_PACKAGES)): \
 debian/stamp-uwsgi-plugin-%:
	$(if $(filter java,$(plugin_lang)),\
		$(eval JAVA_HOME = /usr/lib/jvm/java-$(plugin_lang_kind)-$(plugin_lang_stem)-$(DEB_HOST_ARCH)))
	$(if $(filter java,$(plugin_lang)),\
		UWSGICONFIG_JVM_INCLUDES='-I$(JAVA_HOME)/include $(_jvm_osinclude:%=-I$(JAVA_HOME)/include/%)') \
	$(if $(filter java,$(plugin_lang)),\
		UWSGICONFIG_JVM_LIBPATH='$(JVM_SERVER_DIR)') \
	$(if $(filter lua,$(plugin_lang)),\
		UWSGICONFIG_LUAPC=$(plugin_kind)) \
	$(if $(filter ruby,$(plugin_lang)),\
		UWSGICONFIG_RUBYPATH=$(plugin_interpreter)) \
	$(if $(filter python,$(plugin_lang)),\
		$(foreach flavor,$(plugin_flavors),\
			$(UWSGI_ENV) $(call cdbs_expand_pythonruntime,,$(flavor)) \
				$(UWSGI_BUILDER) \
				--plugin plugins/$(plugin_src) \
				debian/buildconf/uwsgi-plugin.ini \
				$(plugin_flavor_prefix)$(subst .,,$(flavor));),\
		$(UWSGI_ENV) $(cdbs_python3_defaultruntime) $(UWSGI_BUILDER) \
			--plugin plugins/$(plugin_src) \
			debian/buildconf/uwsgi-plugin.ini \
			$(plugin_name))
	touch $@

# resolve packaging scripts and man pages
# ---------------------------------------

$(patsubst %,debian/expand-files/%,$(UWSGI_PLUGIN_PACKAGES)): \
 debian/expand-files/uwsgi-plugin-%:
	set -e; \
	$(foreach infile,$(UWSGI_PLUGIN_INFILES),\
		$(if $(shell test -s "$(plugin_infile)" && echo "not empty"),\
			sed \
				-e 's/@@pkg_name@@/$(pkg_name)/g' \
				-e 's/@@plugin_name@@/$(plugin_name)/g' \
				-e 's/@@plugin_stem@@/$(plugin_stem)/g' \
				-e 's/@@plugin_alternatives_name@@/$(plugin_alternatives_name)/g' \
				-e 's/@@plugin_alternatives_stem@@/$(plugin_alternatives_stem)/g' \
				-e 's/@@plugin_alternatives_priority@@/$(plugin_alternatives_priority)/g' \
				-e 's/@@plugin_lang_kind@@/$(plugin_lang_kind)/g' \
				-e 's/@@plugin_flavors_dotless@@/$(subst .,,$(plugin_flavors))/g' \
				-e 's/@@plugin_defaultflavor_dotless@@/$(subst .,,$(plugin_defaultflavor))/g' \
				< $(plugin_infile) \
				> $(subst __PLUGIN__,$*,$(basename $(infile)));))\
	$(if $(filter python,$(plugin_lang)),\
		$(foreach flavor,$(plugin_flavors),\
			$(foreach infile,install $(if $(plugin_has_binary),links manpages),sed \
				-e "s/@@plugin_name@@/$(plugin_flavor_prefix)$(subst .,,$(flavor))/g" \
				< debian/uwsgi-plugin-__PLUGIN__.$(infile).in \
				>> debian/$(cdbs_curpkg).$(infile);))\
		chmod 755 debian/$(cdbs_curpkg).rtupdate;\
		echo "debian/$(cdbs_curpkg).rtupdate usr/share/$(plugin_lang_kind)/runtime.d" \
			>> debian/$(cdbs_curpkg).install )
	touch $@

$(patsubst %,install/%,$(UWSGI_PLUGIN_PACKAGES)):: \
 install/uwsgi-plugin-%:
	set -e; \
	$(if $(plugin_has_binary),$(if $(filter python,$(plugin_lang)),\
		$(foreach flavor,$(plugin_flavors), \
			ln -sf uwsgi-core-local bin/uwsgi_$(plugin_flavor_prefix)$(subst .,,$(flavor));\
			help2man \
				--name 'fast (pure C)$(comma) self-healing$(comma) developer-friendly WSGI server' \
				--section 1 \
				--no-info \
				bin/uwsgi_$(plugin_flavor_prefix)$(subst .,,$(flavor)) \
				> debian/uwsgi_$(plugin_flavor_prefix)$(subst .,,$(flavor)).1;),\
		ln -sf uwsgi-core-local bin/uwsgi_$(plugin_name);\
		help2man \
			--name 'fast (pure C)$(comma) self-healing$(comma) developer-friendly WSGI server' \
			--section 1 \
			--no-info \
			bin/uwsgi_$(plugin_name) \
			> debian/uwsgi_$(plugin_name).1))

# Python module packages
# ----------------------

# put aside a copy of plugins code before configure
common-configure-arch common-configure-indep:: debian/stamp-codemirror
debian/stamp-codemirror:: post-patches
	rm -rf plugins-src
	cp -a plugins plugins-src
	touch $@
clean::
	rm -f debian/stamp-codemirror

ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
common-build-indep:: debian/test-shellcheck
endif
debian/test-shellcheck:
	find debian \
		-type f -not -empty -regextype egrep \
		-regex '.*(postinst|prerm|rtupdate|init\.d).*' \
		-exec shellcheck -e SC1090,SC1091,SC2034,SC2043 -s sh {} +
	shellcheck -e SC1091,SC2001,SC2034,SC2162 -s bash debian/uwsgi-files/init/*
	touch $@
clean::
	rm -f debian/test-shellcheck

# source package
# --------------

binary-post-install/uwsgi-src::
	find debian/$(cdbs_curpkg)/usr/src/uwsgi \
		-type f \( \
			-name '*.class' -or -name '*.jar' \
			-or -name '*.key' -or -name '*.dll' \
		\) -delete

# Generate manpages during build
common-post-build-arch:: debian/stamp-manpage-build
debian/stamp-manpage-build:
	pod2man debian/debhelper/dh_uwsgi > debian/debhelper/dh_uwsgi.1
	touch $@
