# 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 that break with our Gentoo package versions
	# Only strip from [project.dependencies], leave optional-dependencies alone
	sed -i '/^\[project\]$/,/^\[/{
		s/torch>=1.8.0/torch/
		/torch.*sys_platform.*win32/d
		s/torchvision>=0.9.0/torchvision/
		s/numpy>=1.23.0/numpy/
		s/opencv-python>=4.6.0/opencv-python/
		s/pillow>=7.1.2/pillow/
		s/matplotlib>=3.3.0/matplotlib/
		s/pyyaml>=5.3.1/pyyaml/
		s/requests>=2.23.0/requests/
		s/psutil>=5.8.0/psutil/
	}' pyproject.toml 2>/dev/null || true
	# Remove tests dir that leaks into site-packages
	rm -rf tests
	distutils-r1_src_prepare
}