# 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..14} )
DISTUTILS_SINGLE_IMPL=1
inherit distutils-r1

DESCRIPTION="Ultralytics YOLO — object detection, segmentation, classification"
HOMEPAGE="https://github.com/ultralytics/ultralytics"
SRC_URI="https://github.com/ultralytics/${PN}/archive/refs/tags/v${PV}.tar.gz -> ${P}.tar.gz"

LICENSE="AGPL-3"
SLOT="0"
KEYWORDS="~amd64"
RESTRICT="test"

REQUIRED_USE="${PYTHON_REQUIRED_USE}"

RDEPEND="
	${PYTHON_DEPS}
	virtual/pytorch
	$(python_gen_cond_dep '
		dev-python/numpy[${PYTHON_USEDEP}]
		dev-python/matplotlib[${PYTHON_USEDEP}]
		dev-python/pillow[${PYTHON_USEDEP}]
		dev-python/pyyaml[${PYTHON_USEDEP}]
		dev-python/requests[${PYTHON_USEDEP}]
		dev-python/psutil[${PYTHON_USEDEP}]
		dev-python/tqdm[${PYTHON_USEDEP}]
		dev-python/scipy[${PYTHON_USEDEP}]
	')
	media-libs/opencv[python]
"

src_prepare() {
	# Remove version constraints and deps not in Gentoo from pyproject.toml.
	# Gentoo deps are declared in RDEPEND; this just unblocks the wheel build.
	sed -i \
		-e '/^\[project\]$/,/^\[/{' \
		-e '/torch.*sys_platform.*win32/d' \
		-e '/filelock/d' \
		-e '/polars/d' \
		-e '/nvidia-ml-py/d' \
		-e '/ultralytics-thop/d' \
		-e '/ultralytics-platform/d' \
		-e 's/"torch[^"]*"/"torch"/' \
		-e 's/"torchvision[^"]*"/"torchvision"/' \
		-e 's/"numpy[^"]*"/"numpy"/' \
		-e 's/"opencv-python[^"]*"/"opencv-python"/' \
		-e 's/"pillow[^"]*"/"pillow"/' \
		-e 's/"matplotlib[^"]*"/"matplotlib"/' \
		-e 's/"pyyaml[^"]*"/"pyyaml"/' \
		-e 's/"requests[^"]*"/"requests"/' \
		-e 's/"psutil[^"]*"/"psutil"/' \
		-e 's/"scipy[^"]*"/"scipy"/' \
		-e 's/"tqdm[^"]*"/"tqdm"/' \
		-e '}' \
		pyproject.toml || die "sed failed on pyproject.toml"
	# Remove tests dir that leaks into site-packages
	rm -rf tests
	distutils-r1_src_prepare
}