# Contributor: Guilherme Felipe da Silva <gfsilva.eng@gmail.com>
# Maintainer: Will Sinatra <wpsinatra@gmail.com>
pkgname=lua-net-url
_rockname=${pkgname#lua-}
pkgver=1.1
pkgrel=0
pkgdesc="URL and Query string parser, builder, normalizer for Lua"
url="https://github.com/golgote/neturl"
license="MIT"
arch="noarch"
makedepends="luarocks"
subpackages="$pkgname-doc"
options="!check" #test dependencies not available
source="$_rockname-$pkgver.tar.gz::https://github.com/golgote/neturl/archive/v$pkgver-1.tar.gz"
builddir="$srcdir/neturl-$pkgver-1"

_luaversions="5.1 5.2 5.3 5.4"
for _luaversion in $_luaversions; do
	makedepends="$makedepends lua$_luaversion-dev"
	subpackages="$subpackages lua$_luaversion-$_rockname:_subpackage"
done

build() {
	for _luaversion in $_luaversions; do
		msg "Building for Lua $_luaversion"
		"luarocks-$_luaversion" \
		     CC="$CC" \
		     CFLAGS="$CFLAGS -fPIC" \
		     LUA_INCDIR="$(pkg-config --variable=includedir "lua$_luaversion")" \
		     LUA_LIBDIR="$(pkg-config --variable=libdir "lua$_luaversion")" \
	             LUA_VERSION="$_luaversion" \
		     make --tree="./build-$_luaversion" \
		     "rockspec/$_rockname-$pkgver-"*".rockspec"
	done
}

package() {
	mkdir -p "$pkgdir/usr/share/doc/$pkgname/"
	cp -r "docs/" "$pkgdir/usr/share/doc/$pkgname/"
}

_subpackage() {
	_luaversion="${subpkgname%-"$_rockname"}"
	_luaversion="${_luaversion#lua*}"
	pkgdesc="$pkgdesc - for Lua $_luaversion"
	depends="$depends lua$_luaversion"
	install_if="lua$_luaversion $pkgname=$pkgver-r$pkgrel"
	local rockdir="$subpkgdir/usr/lib/luarocks/rocks-$_luaversion/$_rockname/$pkgver-1"

	for _luascript in "$builddir/build-$_luaversion/share/lua/$_luaversion/${_rockname%-url}/"*.lua; do
		install -D -m 644 \
		        -t "$subpkgdir/usr/share/lua/$_luaversion/${_rockname%-url}" \
		        "$_luascript"
	done

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

sha512sums="
06d87177e63a103416df0575de81bb21f7844b744649e650bf297264bdad1f8ac8393232798fd553f5754b455c53724f679557598e4a3e37246bce3cbe747061  net-url-1.1.tar.gz
"
