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

EAPI=8

DISTUTILS_USE_PEP517=setuptools
PYTHON_COMPAT=( python3_{12..15} )

inherit distutils-r1 pypi

# upstream git commit for the test-suite data files (the 0.333 release
# code itself is not tagged/pushed yet)
COMMIT="d9aa3d8ba4f51bbaf832037083d8b81e1f404a14"

DESCRIPTION="Python library for reading and editing GROMACS topology files"
HOMEPAGE="https://gitlab.com/KomBioMol/gromologist"
SRC_URI+="
	test? ( https://gitlab.com/KomBioMol/${PN}/-/archive/${COMMIT}/${PN}-${COMMIT}.tar.bz2 )
"

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

RDEPEND="dev-python/numpy[${PYTHON_USEDEP}]"
BDEPEND="
	test? (
		sci-chemistry/gromacs
		dev-python/pytest[${PYTHON_USEDEP}]
	)
"

src_unpack() {
	default

	# The PyPI sdist ships the test suite without its data files
	# (upstream has no MANIFEST.in for them); take them from the upstream
	# git archive.  The released code itself is not in the git repo yet,
	# so the sdist stays the primary source.
	if use test; then
		cp "${WORKDIR}/${PN}-${COMMIT}/tests/"{pentapeptide.pdb,pentapeptide.top,merged.rtp} "${S}/tests/" || die
	fi
}

src_prepare() {
	# GROMACS force field addon (FFA) support: amber2ffa/amber2gmFF writers
	# and the gml-amber2ffa/gml-amber2gmx scripts; see files/ for the patch
	# derived from the upstream gromacs-ffa branch
	use ffa && PATCHES+=( "${FILESDIR}/${P}-ffa.patch" )

	default
}

src_install() {
	distutils-r1_src_install

	# setuptools does not embed the license in the wheel (no license_files
	# declaration), so install it ourselves
	dodir /usr/share/licenses/${PN}
	cp "${S}/LICENSE" "${ED}/usr/share/licenses/${PN}/LICENSE" || die
}

python_test() {
	# Tests open their fixture files by relative path, so run them from
	# tests/; the package is imported from the (pure Python) source tree.
	cd "${S}/tests" || die
	PYTHONPATH="${S}" epytest
}

pkg_postinst() {
	einfo "The optional tkinter GUI module (gromologist.gromologist_gui) needs a"
	einfo "Python interpreter built with USE=\"tk\"."
}