# Contributor: Leo <thinkabit.ukim@gmail.com>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer:
pkgname=lua-luv
_rockname=luv
pkgver=1.45.0.0
_pkgver=${pkgver%.*}-${pkgver##*.}
pkgrel=0
pkgdesc="Bare libuv bindings for Lua"
url="https://github.com/luvit/luv"
arch="all"
license="Apache-2.0"
makedepends="
	cmake
	libuv-dev
	lua-compat53-dev
	ninja
	"
subpackages="
	$pkgname-static
	$pkgname-dev
	libluv
	"
source="https://github.com/luvit/luv/archive/$_pkgver/lua-luv-$_pkgver.tar.gz
	disable-udp-test.patch
	"
builddir="$srcdir/luv-$_pkgver"

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

_luaversions="5.1 5.4"
for _v in $_luaversions; do
	makedepends="$makedepends lua$_v-dev"
	subpackages="$subpackages lua$_v-${pkgname#lua-}:_subpackage"
done

build() {
	# shared module loop
	local lver; for lver in $_luaversions $_luajit; do
		cmake -B "build-$lver" -G Ninja \
			-DCMAKE_INSTALL_PREFIX=/usr \
			-DWITH_LUA_ENGINE=Lua \
			-DWITH_SHARED_LIBUV=ON \
			-DLUA_BUILD_TYPE=System \
			-DLUA_INCLUDE_DIR="$(pkg-config --variable=includedir "lua$lver")" \
			-DLUA_COMPAT53_DIR="/usr/include/lua${lver/jit/5.1}" \
			-DBUILD_MODULE=ON \
			-DBUILD_SHARED_LIBS=ON \
			-DBUILD_STATIC_LIBS=OFF
		cmake --build "build-$lver"
	done
	# separate static because cmake tracks all installed and makes it impossible to split
	cmake -B build-static -G Ninja \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DWITH_LUA_ENGINE=Lua \
		-DWITH_SHARED_LIBUV=ON \
		-DLUA_BUILD_TYPE=System \
		-DLUA_INCLUDE_DIR="$(pkg-config --variable=includedir "lua5.1")" \
		-DLUA_COMPAT53_DIR="/usr/include/lua5.1" \
		-DBUILD_MODULE=ON \
		-DBUILD_SHARED_LIBS=OFF \
		-DBUILD_STATIC_LIBS=ON
	cmake --build build-static
}

check() {
	local lver; for lver in $_luaversions $_luajit; do
		msg "Running tests with lua$lver"
		LUA_CPATH="build-${lver/jit/5.1}/lib?.so;;" lua$lver tests/run.lua
	done
}

package() {
	DESTDIR="$pkgdir" cmake --install build-static
	DESTDIR="$pkgdir" cmake --install build-${_luajit:-"5.1"}

	rm -rf "$pkgdir"/usr/lib/lua
}

dev() {
	default_dev

	local lver; for lver in $_luaversions; do
		mkdir -p "$subpkgdir/usr/include/lua$lver"
		ln -s ../luv "$subpkgdir"/usr/include/lua$lver/
	done
}

# This is needed for neovim.
libluv() {
	pkgdesc="$pkgdesc (shared library)"

	amove usr
}

_subpackage() {
	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"

	install -D -m755 "$builddir"/build-$lver/luv.so -t "$subpkgdir"/usr/lib/lua/$lver/

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

sha512sums="
50460573377bc9386a90aa75f7d4938571b04f74ca6f63e32fa2b2f8951e799465abb78b4270f22be511c4d328ce582c38ec3e3da1061310fcfee630b647854d  lua-luv-1.45.0-0.tar.gz
de720e872a45aaa9d104b7646f5aee63e44d5ccfcd4b74c804a71c762dd5fe9e9f735bb82aa45e1d4631bf065f6a407fd09fbed7fc29e02e8aa756077fb71d94  disable-udp-test.patch
"
