# Contributor: Guilherme Felipe da Silva <gfsilva.eng@gmail.com>
# Maintainer: Will Sinatra <wpsinatra@gmail.com>
pkgname=lua-etlua
_rockname="${pkgname#lua-}"
pkgver=1.3.0
pkgrel=4
pkgdesc="Embedded Lua templates"
url="https://github.com/leafo/etlua"
license="MIT"
arch="noarch !ppc64le !riscv64 !loongarch64"
makedepends="luarocks"
checkdepends="lua-busted luajit moonscript"
source="$_rockname-$pkgver.tar.gz::https://github.com/leafo/$_rockname/archive/v$pkgver.tar.gz
	fix_unpack.patch"
builddir="$srcdir/$_rockname-$pkgver"

_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"
	checkdepends="$checkdepends lua$_v 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"
}

_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="
132a5d7d898b791242abc3b95f47f534d0b0e2a3f9848efb95be04f92049b18c919af8c2ac162e11f78b502d346e78a73935c1ad720f5ffbb1efe90355b39a9a  etlua-1.3.0.tar.gz
01b6d2dba2af217c932c0e6696d9f644369f1e71db2264bb88e442845dccf2387fc11d16e9a8ef508d148264f4fa27980ff7c46e831714e65c25cfc0fcd913a3  fix_unpack.patch
"
