# Contributor: qaqland <qaq@qaq.land>
# Maintainer: qaqland <qaq@qaq.land>
pkgname=xmake
pkgver=2.9.6
pkgrel=0
pkgdesc="A cross-platform build utility based on Lua"
url="https://xmake.io/"
arch="all"
license="Apache-2.0"
options="net"
depends="bash"
makedepends="
	grep
	readline-dev
	ncurses-dev
	linux-headers
	libsv-dev
	lua5.4-dev
	lz4-dev
	xxhash-dev
	libtbox-dev
	"
checkdepends="unzip 7zip cmake"
subpackages="
	$pkgname-doc
	$pkgname-bash-completion
	$pkgname-zsh-completion
	$pkgname-fish-completion
	"

# xmake use patched lua-cjson, external will cause test fail
_lua_cjson=ddcecf3b24b71421e7b4a2962f1fbcc0297e0c1e

source="
	$pkgname-$pkgver.tar.gz::https://github.com/xmake-io/xmake/archive/refs/tags/v$pkgver.tar.gz
	$pkgname-core-lua-cjson-$_lua_cjson.tar.gz::https://github.com/xmake-io/xmake-core-lua-cjson/archive/$_lua_cjson.tar.gz
	01-pkgconfig-libsv.patch
	02-system-include.patch
	03-internal-lua-cjson.patch
	"

prepare() {
	default_prepare

	# remove online toolchain test
	local online_tests
	online_tests=$(grep --include=xmake.lua -rwl tests/projects -e 'add_requires')
	for xmake_lua in $online_tests; do
		local test_lua="${xmake_lua/xmake.lua/test.lua}"
		[ -f "$test_lua" ] && rm "$test_lua"
	done

	mv -Tv "$srcdir"/xmake-core-lua-cjson-$_lua_cjson core/src/lua-cjson/lua-cjson

	# xmake "source tbox/xmake.sh" even use external libtbox
	mkdir -p core/src/tbox/tbox/src
	touch core/src/tbox/tbox/src/xmake.sh
}

build() {
	# this configure is not from autotools but github.com/xmake-io/xmake.sh
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--external=yes \
		--kind=shared
	make
}

check() {
	cp build/xmake xmake/xmake
	cp scripts/xrepo.sh xmake/xrepo
	PATH="$PATH:$builddir/xmake"
	xmake lua -v -D tests/run.lua
	xrepo --version
}

package() {
	make DESTDIR="$pkgdir" install

	install -Dm644 scripts/man/*1 -t "$pkgdir"/usr/share/man/man1/

	install -Dm644 xmake/scripts/completions/register-completions.bash \
		"$pkgdir"/usr/share/bash-completion/completions/xmake
	install -Dm644 xmake/scripts/completions/register-completions.zsh \
		"$pkgdir"/usr/share/zsh/site-functions/_xmake
	install -Dm644 xmake/scripts/completions/register-completions.fish \
		"$pkgdir"/usr/share/fish/vendor_completions.d/xmake.fish
}

sha512sums="
433b468619b40cba2d9cbbb51c07372d98c80b2f6538e09c074447ccb30160d2f82ff4734f814faeb8549266dfd49c1e13d81da596a7abdf71c690ef2c7204d0  xmake-2.9.6.tar.gz
3f1caa76b501eadc00d55c8a9e332576d7719007d5f3a1bd5d3f1609b01917d9206849454a9d2adb1d60963191e53a89c6ea5a1996851af5ff6bedc708df88a9  xmake-core-lua-cjson-ddcecf3b24b71421e7b4a2962f1fbcc0297e0c1e.tar.gz
26131e4a84ff159db4a75c3095e0b0f04ad909ecb00296f2e2e10a8db5b01a53603ad2067a56b4b76453a527269b8f77dd24435b87940e929ed6a904aa064aa7  01-pkgconfig-libsv.patch
735560bb8a148818b3c1efeac612c2873c473fd6727e749de78053442fda09919e51292f1aae71e49e5b22ab432da0a981d2d7505bd6e25ae02e4b40b6084279  02-system-include.patch
1df3a3ddf30209556715939e57783f230a56bbaf03a3ded839718c6639b3de0e1161056dcd76f25827cee72d6765973d1e8b112121482cad0050a8eac86dfa79  03-internal-lua-cjson.patch
"
