#!/usr/bin/make -f

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

# encode list items for use in long description (see deb-substvars(5) )
substvars-list-encode = perl -0 -F'/\s+/' -ane 'print " * ", join("\$${Newline} * ",@F)'

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

UWSGI_BUILDER = python3 uwsgiconfig.py -v

UWSGI_SRCPLUGINS_AVAILABLE := $(sort $(notdir $(wildcard plugins/*)))
# plugins not yet ready for production use
UWSGI_SRCPLUGINS_EXPERIMENTAL = libffi libtcc router_spnego
# plugins irrelevant/unusable for Debian or built separately (by another
# dedicated source package.
UWSGI_SRCPLUGINS_ALIEN = airbrake alarm_speech matheval mongodb \
    stackless emperor_mongodb gridfs mongodblog mono php \
	stats_pusher_mongodb v8 gccgo jvm ring servlet jwsgi \
	glusterfs rados rack fiber rbthreads lua \
	python tornado asyncio greenlet gevent psgi coroae \
	pypy
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_UNKNOWN)
UWSGI_SRCPLUGINS_WANTED = $(filter-out $(UWSGI_SRCPLUGINS_EXCLUDE),\
	$(UWSGI_SRCPLUGINS_AVAILABLE))
UWSGI_SRCPLUGINS_ADDON = alarm_curl alarm_xmpp curl_cron \
	emperor_pg geoip graylog2 ldap \
	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 = \
	$(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)
UWSGI_SRCPLUGINS_CORE = $(filter-out \
	$(UWSGI_SRCPLUGINS_ADDON) \
	$(UWSGI_SRCPLUGINS_SKIP),\
	$(UWSGI_SRCPLUGINS_WANTED))

# generate documentation unless nodoc requested
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
MANPAGES = $(patsubst %,debian/uwsgi_%.1,$(UWSGI_SRCPLUGINS_ADDON)) \
 debian/uwsgi-core.1 \
 debian/debhelper/dh_uwsgi.1
endif

%:
	dh $@ --buildsystem=none

bin/uwsgi-%: debian/buildconf/uwsgi-%.ini
	$(UWSGI_ENV) $(UWSGI_BUILDER) --build $<

bin/uwsgi_%:
	ln -sf uwsgi-core bin/uwsgi_$*

%_plugin.so:
	$(UWSGI_ENV) $(UWSGI_BUILDER) \
		--plugin plugins/`basename $*` \
		debian/buildconf/uwsgi-plugin.ini
	if [ `dirname $@` != "." ]; then \
		mkdir -p `dirname $@` && mv `basename $@` $@; \
	fi

debian/%.1: bin/%
	help2man \
		--name 'fast (pure C), self-healing, developer-friendly WSGI server' \
		--section 1 \
		--no-info \
		$< > $@

debian/debhelper/%.1: debian/debhelper/%
	pod2man $< > $@

override_dh_auto_build-arch: \
 bin/uwsgi-core \
 $(addsuffix _plugin.so,\
  $(addprefix build/plugins-core/,$(UWSGI_SRCPLUGINS_CORE)) \
  $(UWSGI_SRCPLUGINS_ADDON))

execute_after_dh_auto_build-arch: $(MANPAGES)

override_dh_auto_test:
	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/*

override_dh_auto_clean:
	$(UWSGI_BUILDER) --clean

execute_after_dh_auto_clean:
	find -type f -name '*.pyc' -delete
	find -type d -name __pycache__ -delete

override_dh_install:
	dh_install -p uwsgi-src -X.key -X.dll
	dh_install --remaining-packages

override_dh_installdocs:
	dh_installdocs --all README CONTRIBUTORS

# 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
execute_after_dh_installexamples-arch:
	set -e; \
	cd debian/uwsgi-core/usr/share/doc/uwsgi-core/examples; \
	$(foreach type, $(ex_types),mkdir $(type); mv -t $(type) $(ex_$(type));) \
	rm $(ex_drop)

override_dh_compress:
	dh_compress -p uwsgi-core \
	 $(addprefix -X,.ini .lua .pl .png .psgi .py .ru .ws .xml .yml)
	dh_compress -p uwsgi-extra \
	 $(addprefix -X,.class .java .rb .c)
	dh_compress --remaining-packages

override_dh_strip:
	dh_strip --dbgsym-migration='uwsgi-dbg (<< 2.0.26-3~)'

override_dh_gencontrol:
	dh_gencontrol -p uwsgi-core -- \
	 -V'uwsgi:Provides=uwsgi-abi-$(abi)' \
	 -V'uwsgi:corepluginlist=$(shell \
	  echo $(UWSGI_SRCPLUGINS_CORE) | $(substvars-list-encode))'
	dh_gencontrol --remaining-packages
