# Copyright 2024-2025 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=8

RUST_MIN_VER="1.92"
CRATES=""

declare -A GIT_CRATES=(
	[citationberg]='https://github.com/typst/citationberg;0999ab79842d79c7aae665cf1c6eece5a4b9ef2e;citationberg-%commit%'
	[codex]='https://github.com/typst/codex;0426b6a97414064ad31d0b97d822566fe9df8b65;codex-%commit%'
	[hayagriva]='https://github.com/typst/hayagriva;292b88010d75e5fea8c8ff41fad250b2cd4d9a9a;hayagriva-%commit%'
	[typst-assets]='https://github.com/typst/typst-assets;3284e80cc102b402e4e3db1aa071f1eadae5e7ca;typst-assets-%commit%'
	[typst-dev-assets]='https://github.com/typst/typst-dev-assets;263883b3d0e070e82b17b2f7062df7579a7e76fb;typst-dev-assets-%commit%'
)

inherit cargo shell-completion

MY_TYPST_COMMIT="44b3f78ed37fedea75e911dde2269ef86c45316f"

DESCRIPTION="A markup-based typesetting system for the sciences"
HOMEPAGE="https://typst.app"

SRC_URI="
	https://github.com/${PN}/${PN}/archive/${MY_TYPST_COMMIT}.tar.gz -> ${P}.tar.gz
	https://dblsaiko.net/pub/dist/${P}-deps.tar.xz
	${CARGO_CRATE_URIS}
"
S="${WORKDIR}/${PN}-${MY_TYPST_COMMIT}"

LICENSE="Apache-2.0"
# Dependent crate licenses
LICENSE+="
	Apache-2.0 BSD-2 BSD CC0-1.0 ISC MIT MPL-2.0 UoI-NCSA Unicode-3.0
	Unicode-DFS-2016 ZLIB
"
SLOT="0"
KEYWORDS="~amd64 ~arm64"

RDEPEND="
	>=dev-libs/openssl-1.0.2o-r6:0=
"
DEPEND="${RDEPEND}"
BDEPEND="
	dev-vcs/git
"

QA_FLAGS_IGNORED="usr/bin/typst"

DOCS=( README.md )

src_compile() {
	export TYPST_COMMIT_SHA=$(gunzip < "${DISTDIR}/${P}.tar.gz" | git get-tar-commit-id)
	export TYPST_VERSION="${PV/_/+}"
	export GEN_ARTIFACTS="artifacts/"

	cargo_src_compile
}

src_install() {
	local ARTIFACTSDIR='crates/typst-cli/artifacts'
	cargo_src_install --path "${S}/crates/typst-cli"
	doman "${ARTIFACTSDIR}/${PN}"*.1
	dozshcomp "${ARTIFACTSDIR}/_${PN}"
	dofishcomp "${ARTIFACTSDIR}/${PN}.fish"
	newbashcomp "${ARTIFACTSDIR}/${PN}.bash" "${PN}"

	einstalldocs
}

src_test() {
	cargo_src_test --workspace
}