# Contributor: Daniel Sabogal <dsabogalcc@gmail.com>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
#
# TODO: Try to trim the base package to include only common syntax files etc.
pkgname=neovim
pkgver=0.10.0
pkgrel=1
pkgdesc="Vim-fork focused on extensibility and agility"
url="https://neovim.io/"
arch="all"
license="Apache-2.0 AND Vim"
depends="lua5.1-lpeg"
makedepends="
	cmake
	gettext-dev
	gperf
	libtermkey-dev
	libuv-dev
	libvterm-dev
	lua-luv-dev
	lua5.1-mpack
	msgpack-c-dev
	samurai
	tree-sitter-dev
	unibilium-dev
	"
# Build neovim with lua instead of luajit on s390x
# See https://github.com/neovim/neovim/issues/7879
case "$CARCH" in
	ppc64le | riscv64 | s390x | loongarch64) makedepends="$makedepends lua5.1-dev lua5.1-bitop" ;;
	*) makedepends="$makedepends luajit-dev" ;;
esac
subpackages="$pkgname-lang $pkgname-doc"
options="!check" # unit and functional tests fail
source="https://github.com/neovim/neovim/archive/v$pkgver/neovim-$pkgver.tar.gz
	make-tree-sitter-optional.patch
	nodoc.txt
	"
# secfixes:
#   0.3.6-r0:
#     - CVE-2019-12735
#   0.2.0-r0:
#     - CVE-2017-5953
#     - CVE-2017-6349
#     - CVE-2017-6350
#   0.1.6-r1:
#     - CVE-2016-1248

build() {
	local cmakeoptions=
	case "$CARCH" in
		ppc64le | riscv64 | s390x | loongarch64) cmakeoptions="-DPREFER_LUA=ON" ;;
	esac

	# DCI_BUILD: See https://github.com/neovim/neovim/issues/22932
	cmake -B build -G Ninja \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_INSTALL_LIBDIR=lib \
		-DENABLE_JEMALLOC=FALSE \
		-DENABLE_LTO=TRUE \
		-DCMAKE_VERBOSE_MAKEFILE=TRUE \
		-DCI_BUILD=OFF \
		$cmakeoptions
	cmake --build build
}

check() {
	msg "Running unit tests"
	cmake --build build --target test/unittest
	msg "Running functional tests"
	cmake --build build --target test/functionaltest
}

package() {
	DESTDIR="$pkgdir" cmake --install build

	# Tree-sitter grammars are packaged separately and installed into
	# /usr/lib/tree-sitter.
	ln -s ../../../lib/tree-sitter "$pkgdir"/usr/share/nvim/runtime/parser
}

doc() {
	default_doc

	pkgdesc="$pkgdesc (documentation and help pages)"
	replaces="$pkgname"
	local docdir="usr/share/nvim/runtime/doc"

	mkdir -p "$subpkgdir"/$docdir
	mv "$pkgdir"/$docdir/*.txt "$subpkgdir"/$docdir/

	# When user execute ":help <pattern>", neovim will open nodoc.txt
	# with information why docs are not available and how to install them.
	install -D "$srcdir"/nodoc.txt \
		"$pkgdir"/$docdir/nodoc.txt
	local f; for f in "$subpkgdir/$docdir"/*.txt; do
		ln -s nodoc.txt "$pkgdir/$docdir/$(basename $f)"
	done
}

sha512sums="
878cb58729a18179b38303bd9cf79e38997c87946665ac4e7b76d87e15f5d001028fd1454ce30b20773b20a4fe929dfa399fc2ec7f6db0774a4e864c9e8586c1  neovim-0.10.0.tar.gz
660c83a863b8467a7f19febe4cae6f3ea08f985d5fda0c7466e00936c7b8113bf53077d4bd9a983744f131a68346da2f098425695f4c7c7fdd3244b26ee76927  make-tree-sitter-optional.patch
72ab288f53acddc088c567aafe8c5afa6835325fab7879e782d1d62f87a662f3a6bac123c450debbae1b32336cc60b2830b429838ee3dfcc7524773b5069f4f0  nodoc.txt
"