# Contributor: Will Sinatra <wpsinatra@gmail.com>
# Maintainer: Will Sinatra <wpsinatra@gmail.com>
pkgname=lua-lapis-bayes
_rockname=lapis-bayes
pkgver=1.3.0
pkgrel=0
pkgdesc="a Naive Bayes classifier for use in Lua."
url="https://github.com/leafo/lapis-bayes"
arch="noarch !ppc64le !riscv64 !loongarch64" #blocked by lua-lapis, which requires lua-etlua
license="MIT"
makedepends="luarocks"
source="$pkgname-$pkgver.tar.gz::https://github.com/leafo/$_rockname/archive/v$pkgver.tar.gz
	fix_lua_ver.patch
	LICENSE"
builddir="$srcdir/$_rockname-$pkgver"
depends="lua-lapis"
checkdepends="lua-tableshape lua-busted"
subpackages="$pkgname-doc"

_luaversions="5.1 5.2 5.3 5.4"
for _v in $_luaversions; do
	makedepends="$makedepends lua$_v-dev luarocks$_v"
	subpackages="$subpackages lua$_v-${pkgname#lua-}:_subpackage"
	depends="$depends lua$_v lua$_v-lapis"
	checkdepends="$checkdepends lua$_v-tableshape lua$_v-busted"
done

prepare() {
	default_prepare
}

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 \
			"$_rockname-dev-1.rockspec"

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

check() {
	mkdir .bin

	local lver; for lver in $_luaversions; do
		msg "Testing on lua$lver..."
		ln -sf "/usr/bin/lua$lver" ".bin/lua"
		LUA_PATH="$builddir/?.lua;;" \
		PATH="$builddir/.bin:$PATH" \
		lua "/usr/bin/busted"
	done
}

package() {
	mkdir -p "$pkgdir"

	install -Dm644 "$srcdir"/LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
}

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

	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

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

sha512sums="
c03e7210fce16e40a767cd2482ffe895aa661c19f4c5b3efa95d7eab7ec83357a074a21adb4d0708ec988b050e528d7a9b30706596dea94fda285bdd5d259503  lua-lapis-bayes-1.3.0.tar.gz
9414141ee2a8c8e3888c3b63986f753913f138de1edcdd8647b602f2bbfb1e495c545de4347a86c2c8eecefa9140bb83b648b3b3fe07a1adedde8937c2d0761e  fix_lua_ver.patch
8d23c78a24f99ffc27adbc75402368ea7445034c31124966fed9ccf7e0fbf31358b6ff09719a390452145c33040174b3cf8cb89e3eb54804eb7d6c2c86abff2f  LICENSE
"
