# Contributor: Łukasz Jendrysik <scadu@yandex.com>
# Contributor: Kaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=uwsgi
pkgver=2.0.17
pkgrel=0
pkgdesc="uWSGI application container server"
url="http://projects.unbit.it/uwsgi/"
arch="all"
license="GPL-2.0"
depends="mailcap"
install="uwsgi.pre-install"
pkgusers="uwsgi"
pkggroups="uwsgi"
makedepends="
	attr-dev
	curl-dev
	geoip-dev
	jansson-dev
	libcap-dev
	linux-headers
	linux-pam-dev
	libxml2-dev
	libxslt-dev
	lua5.3-dev
	paxmark
	pcre-dev
	postgresql-dev
	python
	python2-dev
	python3-dev
	zeromq-dev
	zlib-dev
	"
source="http://projects.unbit.it/downloads/uwsgi-${pkgver}.tar.gz
	uwsgi.initd
	uwsgi.ini
	readme.emperor
	alpine.buildconf
	musl-fix-python.patch
	"
options="!check"
builddir="$srcdir/$pkgname-$pkgver"

# secfixes:
#   2.0.16-r0:
#     - CVE-2018-6758

# Plugins for Python 2 or without compile dependency on Python.
# You may specify a custom name for so lib and subpackage after a colon.
_plugins="
	alarm_curl
	cache
	carbon
	cgi
	cheaper_backlog2
	cheaper_busyness
	corerouter
	curl_cron
	dumbloop
	dummy
	echo
	emperor_amqp
	emperor_pg
	emperor_zeromq
	fastrouter
	forkptyrouter
	geoip
	gevent
	graylog2
	http
	legion_cache_fetch
	logcrypto
	logfile
	logpipe
	logsocket
	logzmq
	lua
	msgpack
	nagios
	notfound
	pam
	ping
	pty
	python
	rawrouter
	redislog
	router_basicauth
	router_cache
	router_expires
	router_hash
	router_http
	router_memcached
	router_metrics
	router_radius
	router_redirect
	router_redis
	router_rewrite
	router_static
	router_uwsgi
	rpc
	rrdtool
	rsyslog
	signal
	spooler
	sslrouter
	stats_pusher_file
	stats_pusher_socket
	stats_pusher_statsd
	symcall
	syslog
	transformation_chunked
	transformation_gzip
	transformation_offload
	transformation_template
	transformation_tofile
	tuntap
	ugreen
	webdav
	xslt
	zabbix
	zergpool
	"
# Plugins for Python 3.
# You may specify a custom name for so lib and subpackage after a colon.
_plugins3="gevent:gevent3 python:python3"

subpackages=""
for _p in $_plugins $_plugins3; do
	subpackages="$subpackages uwsgi-${_p#*:}:_plugin"
done

_desc_gevent="uWSGI plugin for gevent (Python 2)"
_desc_gevent3="uWSGI plugin for gevent (Python 3)"
_desc_python="uWSGI plugin for Python 2"
_desc_python3="uWSGI plugin for Python 3"


prepare() {
	default_prepare
	cp "$srcdir"/alpine.buildconf buildconf/alpine.ini
}

build() {
	cd "$builddir"

	msg "Building core"
	# ccache seems to trigger some weird bug on musl
	CC="gcc" python2 uwsgiconfig.py --build alpine

	export UWSGICONFIG_LUAPC="lua5.3"

	local item
	for item in $_plugins; do
		_build python "$item"
	done
	for item in $_plugins3; do
		_build python3 "$item"
	done
}

_build() {
	local python="$1"
	local plugin="${2%:*}"
	local myname="${2#*:}"

	msg "Building $plugin plugin ($myname)"
	$python uwsgiconfig.py --plugin plugins/$plugin alpine $myname
}

package() {
	cd "$builddir"

	install -m 755 -D uwsgi "$pkgdir"/usr/sbin/uwsgi
	install -m 644 -D "$srcdir"/readme.emperor "$pkgdir"/etc/uwsgi/conf.d/readme.emperor
	install -m 644 -D "$srcdir"/uwsgi.ini "$pkgdir"/etc/uwsgi/uwsgi.ini
	install -m 755 -D "$srcdir"/uwsgi.initd "$pkgdir"/etc/init.d/uwsgi

	install -m 775 -d -o root -g uwsgi "$pkgdir"/var/log/uwsgi

	# Disable emutramp/mprotect, this is needed for luajit and cffi.
	paxmark -em "$pkgdir"/usr/sbin/uwsgi
}

_plugin() {
	local name="${subpkgname#$pkgname-}"
	local desc="$(eval "echo \$_desc_$name")"
	pkgdesc="${desc:-"uWSGI plugin $name"}"
	depends="uwsgi"

	cd "$builddir"

	local requires=$(sed '$a print(" ".join(REQUIRES))' \
		plugins/$name/uwsgiplugin.py 2>/dev/null | python3 2>/dev/null)

	local req; for req in $requires; do
		if echo "$_plugins" | grep -qw "$req"; then
			depends="$depends $pkgname-$req"
		else
			error "There's no subpackage for requirement $req!"
			return 1
		fi
	done

	install -m 755 -D ${name}_plugin.so \
		"$subpkgdir"/usr/lib/uwsgi/${name}_plugin.so
}

sha512sums="639427fbb89a1c2610c1cafb6ff009398a3c0a8e27c3de3f00829428271ba97b64b1253368dd6150912cf44441052be2a63cbe81613bbe964be27ee2e570d2b2  uwsgi-2.0.17.tar.gz
63137d45437e00deb970d66e2abddf7f235636b748f6cf545f54ff5557066c2a71db5c4a3bab2edceb4e65d3d7fac887f1f4c0e77658127e02b64d3026780b4c  uwsgi.initd
370202e143d0e041a2e36905346080d3ca592c9a74705a8c4e142a1307d22b921c10e673d683ca284ea27d184474ed86edd7723a6e590d35a562ad7cf6afa529  uwsgi.ini
2798c9d2e49e29292ffe6a5a29b866301c78d5f322fc5d07dfa9569efdaa5220237a5f6744652fbdd957be29311ddaff190541333f042cbf0e907dfa98ce3e2a  readme.emperor
f3cff00926929a5bb40afafb65fd5228582af35fbf524562282020c4c4ae9c659231b2381f4b3cceb18e8f3f6c888c21bdd8ed4ddcd81e92fbc6a0891800ce38  alpine.buildconf
de68b16b44e554a79c073c9befa10566796316dbf4c375b4d6b633d80b0282694cca233f0a70f3d6570584324f14276826bbeb8f38b550c00087a05f9ba9227f  musl-fix-python.patch"
