# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=lua-mmdb
_rockname=mmdblua
# Keep in sync with project's submodule.
_maxmind_gitrev=f4149c9fb5897f8dd40946ed5f70768033b4f571
pkgver=0.2
pkgrel=0
pkgdesc="Maxmind database parser for lua"
url="https://github.com/daurnimator/mmdblua"
arch="noarch"
license="MIT"
depends="lua"
checkdepends="lua-busted lua5.1-compat53 lua5.2-compat53"
source="https://github.com/daurnimator/mmdblua/archive/v$pkgver/lua-mmdb-$pkgver.tar.gz
	https://github.com/maxmind/MaxMind-DB/archive/$_maxmind_gitrev/MaxMind-DB-$_maxmind_gitrev.tar.gz
	"
builddir="$srcdir/$_rockname-$pkgver"

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

case "$CARCH" in
 ppc64le|riscv64|loongarch64) _luajit="" ;;
 *) checkdepends="$checkdepends luajit"; _luajit="jit" ;;
esac

prepare() {
	default_prepare

	rmdir spec/MaxMind-DB
	ln -s ../../MaxMind-DB-$_maxmind_gitrev spec/MaxMind-DB
}

check() {
	local lver; for lver in $_luaversions $_luajit; do
		msg "Testing on lua$lver"
		LUA_PATH='?/init.lua;;' lua$lver /usr/bin/busted
	done
}

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

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

	depends="lua$lver"
	case "$lver" in
		5.1 | 5.2) depends="$depends lua$lver-compat53";;
	esac

	install -m 644 -D "$builddir"/mmdb/init.lua \
		"$subpkgdir"/usr/share/lua/$lver/mmdb.lua

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

sha512sums="
f45dbff51fbf04f11d4cc7592aae4cc72c38a421476cb2c37829fd2c59953148f012ad977251b8c5f9a358d7dc7acbf2abd3bf9f11b0d018d1297831a0bd80e1  lua-mmdb-0.2.tar.gz
57a5cc563db20eba15c7cdefcab8f9c1d21bf00e36b042c71d610ceb00e6b548651d8b3b54f03f537f05303286fd92d18c2f8c757293f7e32b46dabcd883d274  MaxMind-DB-f4149c9fb5897f8dd40946ed5f70768033b4f571.tar.gz
"
