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

# Autogenerated by pycargoebuild 0.15.0

EAPI=8

inherit cargo

#because of the weird downloaded artifact name
S=${WORKDIR}/${PN}-${P}

DESCRIPTION="cat for markdown: Show markdown documents in terminals"
HOMEPAGE="https://github.com/swsnr/mdcat"
SRC_URI="
	https://github.com/swsnr/mdcat/archive/refs/tags/${P}.tar.gz
	https://gitea.com/slash/schplaf-gentoo-overlay-files/raw/tag/${P}/${PN}/${P}-crates.tar.xz
	${CARGO_CRATE_URIS}
"

LICENSE="Apache-2.0 MPL-2.0"
# Dependent crate licenses
LICENSE+="
	Apache-2.0 Apache-2.0-with-LLVM-exceptions BSD-2 BSD MIT MPL-2.0
	Unicode-3.0 ZLIB
"
SLOT="0"
KEYWORDS="~amd64"
IUSE="bash-completion zsh-completion fish-completion doc"

BDEPEND="doc? ( dev-ruby/asciidoctor app-alternatives/gzip )"
DEPEND="net-misc/curl"

DOCS=( CHANGELOG.md LICENSE README.md sample/ screenshots/ scripts/ )
RUST_MIN_VER=""
RUST_MIN_VER="1.83.0"

pkg_setup() {
	rust_pkg_setup
}

src_unpack() {
	cargo_src_unpack
}

src_prepare() {
	eapply "${FILESDIR}"/${P}-pulldown-cmark-mdcat_osc.patch
	eapply_user
}

src_configure() {
	cargo_src_configure --offline
	#--frozen
}

src_install() {
	if use doc
	then
		mkdir ${D}/usr/share/man/man1/
		asciidoctor -b manpage -a reproducible -o ${D}/usr/share/man/man1/mdcat.1 mdcat.1.adoc
		ln -s ${D}/usr/share/man/man1/mdcat.1.bz2 ${D}/usr/share/man/man1/mdless.1.bz2
	fi
	#from the README:
	# "mdcat can be linked or copied to mdless; if invoked as mdless it automatically uses pagination."
	# A symlink or hardlink from mdless to mdcat (see above).
	ln -sf ${D}/usr/bin/${PN} ${D}/usr/bin/mdless
	einstalldocs
	TOBEINSTALLED_MDCAT=${D}/usr/bin/mdcat
	TOBEINSTALLED_MDLESS=${D}/usr/bin/mdless
	if use bash-completion
	then
		mkdir ${D}/usr/share/bash-completion/completions
		${TOBEINSTALLED_MDCAT} --completions bash > ${D}/usr/share/bash-completion/completions/${PN}
		${TOBEINSTALLED_MDLESS} --completions bash > ${D}/usr/share/bash-completion/completions/mdless
	fi
	if use zsh-completion
	then
		mkdir ${D}/usr/share/zsh/site-functions
		${TOBEINSTALLED_MDCAT} --completions zsh > ${D}/usr/share/zsh/site-functions/_${PN}
		${TOBEINSTALLED_MDLESS} --completions zsh > ${D}/usr/share/zsh/site-functions/_mdless
	fi
	if use fish-completion
	then
		mkdir ${D}/usr/share/vendor_completions.d
		${TOBEINSTALLED_MDCAT} --completions fish > ${D}/usr/share/vendor_completions.d/${PN}.fish
		${TOBEINSTALLED_MDLESS} --completions fish > ${D}/usr/share/vendor_completions.d/mdless.fish
	fi
	cargo_src_install
	exeinto "/usr/bin"
}