maintainer="leso-kn <info@lesosoftware.com>"
pkgname=paged-markdown-3-pdf
pkgver=0.1.3
pkgrel=0
pkgdesc="A command-line PDF renderer for the Paged Markdown 3 document-format for scientific writing"
url="https://codeberg.org/leso-kn/lua-paged-markdown-3"
# armhf, riscv64, s390x: firefox unavailable
arch="all !armhf !loongarch64 !riscv64 !s390x"
license="MIT"
_lrflags="LUA_INCDIR=/usr/include/luajit-2.1 --tree=$srcdir/.luarocks"
_lua=5.1
_luarocks=luarocks-$_lua
checkdepends="
	file
	firefox
	"
makedepends="
	build-base
	git
	luajit-dev
	luarocks$_lua
	"
depends="cmd:firefox qpdf"
subpackages="
	$pkgname-bash-completion
	$pkgname-fish-completion
	$pkgname-zsh-completion
	"
source="
	https://codeberg.org/leso-kn/lua-paged-markdown-3/archive/pdf-v$pkgver.tar.gz
	test.md
	luachild_5-fix-missing-headers-for-luajit.patch.noauto
	"

options="net"
builddir="$srcdir"/lua-paged-markdown-3

# https://github.com/pocomane/luachild/issues/4
_install_luachild() {
	$_luarocks $_lrflags install luarocks-build-lrocket
	_luarocks=$HOME/.local/bin/luarocks
	eval "$($_luarocks $_lrflags path)"

	rm -rf luachild-0.1*
	$_luarocks unpack luachild 0.1

	cd luachild-0.1*/luachild
	patch -p1 < "$srcdir"/luachild_5-fix-missing-headers-for-luajit.patch.noauto
	$_luarocks $_lrflags make luachild-0.1*.rockspec

	cd "$builddir"
}

build() {
	_install_luachild
	$_luarocks $_lrflags make --no-install rockspecs/paged-markdown-3-pdf-$pkgver-1.rockspec
}

check() {
	bin/pmd-pdf "$srcdir"/test.md test.pdf
	file test.pdf | grep 'PDF document'
}

package() {
	install -Dm755 bin/pmd-pdf "$pkgdir"/usr/bin/pmd-pdf

	mkdir -p \
		"$pkgdir"/usr/share/bash-completion/completions \
		"$pkgdir"/usr/share/fish/vendor_completions.d \
		"$pkgdir"/usr/share/zsh/site-functions

	bin/pmd-pdf --completion bash > "$pkgdir"/usr/share/bash-completion/completions/pmd-pdf
	bin/pmd-pdf --completion fish > "$pkgdir"/usr/share/fish/vendor_completions.d/pmd-pdf.fish
	bin/pmd-pdf --completion zsh > "$pkgdir"/usr/share/zsh/site-functions/_pmd-pdf
}
sha512sums="
e1761d599577f5e3d8bc466d7eb6cd7b4755be182d7bec70f7f75e201b9ad04660275ebe15c8c61c6b3d7d73a3f647da17c5375977befa9c667cd5f023af0d8a  pdf-v0.1.3.tar.gz
3a0b8910e3b96e0624793b5860e3438e28d8202076c17b7318b1e37fdfadac9d884319a8c51b748bab693092c11e282a3fd65f67ba56ed23fe44c082d80404da  test.md
be83d6a2a0432d266d524c870763d8f518d7e5b79f3fe9319f6ed4cde0be80d0f435a625c811c5fa8721d3395cc83ae321277f819ce72231184cc297c22e100d  luachild_5-fix-missing-headers-for-luajit.patch.noauto
"
