# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
_luaversions="5.1 5.2 5.3"
pkgname=lua-compat53
_rockname=compat53
_pkgname=lua-compat-5.3
pkgver=0.13
pkgrel=0
pkgdesc="Compatibility module providing Lua-5.3-style APIs for Lua"
url="https://github.com/keplerproject/lua-compat-5.3"
arch="all"
license="MIT"
subpackages="$pkgname-dev"
for _v in $_luaversions; do
	makedepends="$makedepends lua$_v-dev luarocks$_v"
	subpackages="$subpackages lua$_v-${pkgname#lua-}:_package"
done
source="$pkgname-$pkgver.tar.gz::https://github.com/keplerproject/$_pkgname/archive/v$pkgver.tar.gz"
builddir="$srcdir/$_pkgname-$pkgver"

build() {
	local lver; for lver in $_luaversions; do
		msg "Building for Lua $lver..."

		luarocks-$lver \
			CC="$CC" \
			CFLAGS="$CFLAGS -fPIC" \
			LUA_INCDIR="$(pkg-config --variable=includedir lua$lver)" \
			LUA_LIBDIR="$(pkg-config --variable=libdir lua$lver)" \
			make --tree=./build rockspecs/compat53-scm-0.rockspec
	done
}

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

_package() {
	local lver="${subpkgname:3:3}"
	pkgdesc="$pkgdesc $lver"
	depends="lua$lver"
	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/lua/$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
}

dev() {
	default_dev

	local incdir51="$subpkgdir/usr/include/lua5.1"
	local incdir52="$subpkgdir/usr/include/lua5.2"

	cd "$builddir"

	install -D -m644 lprefix.h -t "$incdir51"
	install -D -m644 c-api/* -t "$incdir51"/c-api/

	mkdir -p "$incdir52"/c-api
	cp -l "$incdir51"/*.h "$incdir52"/
	cp -l "$incdir51"/c-api/* "$incdir52"/c-api/
}

sha512sums="
d09141a516b0c047362b9be11a74dcdb7408bba8f39bf3b568dc088fbe115e4e2d2715f6fc7f616f3891bb0d61970d09aa71c1cada23a67fbd4762822385aea9  lua-compat53-0.13.tar.gz
"
