# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=lua-http
pkgver=0.2
pkgrel=0
pkgdesc="HTTP Library for Lua that supports HTTP(S) 1.0, 1.1 and 2.0"
url="https://github.com/daurnimator/lua-http"
arch="noarch"
license="MIT"
depends="lua-cqueues lua-basexx lua-fifo lua-lpeg lua-lpeg_patterns lua-ossl"
checkdepends="lua-busted lua5.1-compat53 lua5.2-compat53 lua5.1-bit32"
makedepends=""
subpackages=""
source="$pkgname-$pkgver.tar.gz::https://github.com/daurnimator/$pkgname/archive/v$pkgver.tar.gz"
builddir="$srcdir/$pkgname-$pkgver"

case "$CARCH" in
	# luajit is not available for s390x
	s390x) _luajit="";;
	# FIXME: Some tests fail with "Address not available".
	# and ppc64le hangs
	aarch64 | armhf | ppc64le) options="!check";;
	*) checkdepends="$checkdepends luajit" _luajit="jit";;
esac

_luaversions="5.1 5.2 5.3"
for _v in $_luaversions; do
	checkdepends="$checkdepends lua$_v lua$_v-busted"
	subpackages="$subpackages lua$_v-${pkgname#lua-}:_subpackage"
done

check() {
	cd "$builddir"

	local lver; for lver in $_luaversions $_luajit; do
		msg "Testing on lua$lver"
		lua$lver /usr/bin/busted
	done
}

package() {
	mkdir -p "$pkgdir"
}

_subpackage() {
	local lver="${subpkgname:3:3}"
	pkgdesc="$pkgdesc (for Lua $lver)"
	depends="lua$lver ${depends//lua-/lua$lver-}"
	install_if="$pkgname=$pkgver-r$pkgrel lua$lver"
	local lmod_dir="$subpkgdir/usr/share/lua/$lver"
	local rockdir="$subpkgdir/usr/lib/luarocks/rocks-$lver/$pkgname/$pkgver-1"

	if [ "$lver" != "5.3" ]; then
		depends="$depends lua$lver-compat5.3"
	fi
	if [ "$lver" = "5.1" ]; then
		depends="$depends lua5.1-bit32"
	fi

	mkdir -p "$lmod_dir"
	cp -r "$builddir"/http "$lmod_dir"/

	mkdir -p "$rockdir"
	echo 'rock_manifest = {}' > "$rockdir"/rock_manifest
}

sha512sums="ed71ac09277e8602e3d4954541f06c09ef444db20e5b4e39a17b9ed4893046b1442969902c0ba84fc4c3c922a6d1c8ebf853fceda261b12bf10ea9aa24389587  lua-http-0.2.tar.gz"
