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

EAPI=8

DISTUTILS_USE_PEP517=uv-build
PYTHON_COMPAT=( python3_{12..15} )

inherit distutils-r1

DESCRIPTION="Convert Amber/GAFF topology files into GROMACS, CHARMM and Gaussian formats"
HOMEPAGE="
	https://alanwilter.github.io/acpype/
	https://acpype.readthedocs.io/
	https://github.com/alanwilter/acpype/
"
SRC_URI="https://github.com/alanwilter/${PN}/archive/refs/tags/${PV}.tar.gz -> ${P}.tar.gz"

LICENSE="GPL-3"
SLOT="0"
KEYWORDS="~amd64"
IUSE="test"
RESTRICT="!test? ( test )"

# antechamber/tleap drive the conversion at run time; openbabel's python module
# handles the non-mol2 inputs (SMILES, PDB, ...)
RDEPEND="
	sci-chemistry/ambertools
	sci-chemistry/openbabel[python]
	dev-python/rich[${PYTHON_USEDEP}]
	dev-python/typer[${PYTHON_USEDEP}]
"
BDEPEND="
	test? (
		sci-chemistry/ambertools
		dev-python/pytest[${PYTHON_USEDEP}]
	)
"

PATCHES=(
	"${FILESDIR}/${P}-skip-charmmgen-tests.patch"
)

src_prepare() {
	default

	# Upstream wheels vendor ~110 MB of AmberTools binaries under
	# src/acpype/amber_{linux,macos} as a pip-install fallback.  ACPYPE only
	# consults the bundle when no antechamber is on PATH
	# (utils.bundled_amber_dir() returns None if the directory is absent),
	# and we depend on the tree's sci-chemistry/ambertools anyway, so ship
	# without it (bundled-deps policy).
	rm -r src/acpype/amber_linux src/acpype/amber_macos || die
}

src_install() {
	distutils-r1_src_install

	# uv_build does not embed the license in the wheel
	dodir /usr/share/licenses/${PN}
	cp "${S}/LICENSE" "${ED}/usr/share/licenses/${PN}/LICENSE" || die
}

python_test() {
	# Upstream pytest addopts force --cov (pytest-cov, with a coverage gate)
	# and --dist/-n (pytest-xdist); run the suite plain instead of pulling
	# those plugins in just to run tests.
	epytest -o addopts=""
}

pkg_postinst() {
	einfo "Output format .charmm additionally needs the charmmgen helper, which"
	einfo "AmberTools does not ship; ACPYPE reports it as unavailable in that"
	einfo "case. The .gmx, .out and .ac outputs work with sci-chemistry/ambertools."
}