# Contributor: Guilherme Felipe da Silva <gfsilva.eng@gmail.com>
# Maintainer: Will Sinatra <wpsinatra@gmail.com>
pkgname=lua-jsonschema
_rockname=${pkgname#lua-}
pkgver=0.9.9
pkgrel=1
pkgdesc="Pure Lua JSON schema validator for Lua/LuaJIT"
url="https://github.com/api7/jsonschema"
license="MIT"
arch="noarch"
depends="lua-net-url lua-rex-pcre2"
makedepends="luarocks"
options="!check" # Test dependencies not available
source="$_rockname-$pkgver.tar.gz::https://github.com/api7/jsonschema/archive/v$pkgver.tar.gz
	lrex-pcre2.patch
	"
builddir="$srcdir/$_rockname-$pkgver"
_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() {
	local lver; for lver in $_luaversions; do
		msg "Building for Lua $lver..."

		luarocks-$lver \
			LUA_INCDIR="$(pkg-config --variable=includedir lua$lver)" \
			LUA_LIBDIR="$(pkg-config --variable=libdir lua$lver)" \
			make --tree="./build" \
			--deps-mode=none \
			"rockspec/$_rockname-master-"*".rockspec"

		rm "./build/lib/luarocks/rocks-$lver/manifest"
	done
}

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

_subpackage() {
	local lver="${subpkgname:3:3}"
	pkgdesc="$pkgdesc (for Lua $lver)"
	depends="lua$lver"
	install_if="$pkgname=$pkgver-r$pkgrel lua$lver"

	local path; for path in "lib/luarocks/rocks-$lver" "share/lua/$lver"; do
		mkdir -p "$subpkgdir/usr/${path%/*}"
		mv "$builddir/build/$path" "$subpkgdir/usr/$path/"
	done
}

sha512sums="
4ef32a2f291a0c795782841a8d1ccc4c9004cf3eacf971f014261755b91186df177f586ccce847c18694fbcd376460d6695ee506f850177589588ed09cd17e33  jsonschema-0.9.9.tar.gz
964869f52ae5ce6e9a5723eb0a720c1cc30b6f1b5c2289cabd77056e085a0ef3b033175b43c09588f926a9bbc686f049310d85e5a39c7e516ebe9fbde5529b79  lrex-pcre2.patch
"
