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

EAPI=8

PYTHON_COMPAT=( python3_{12..14} )

inherit bash-completion-r1 optfeature python-single-r1

DESCRIPTION="Simple chroot build environment manager for building RPMs"
HOMEPAGE="
	https://rpm-software-management.github.io/mock/
	https://github.com/rpm-software-management/mock/
"
SRC_URI="https://github.com/rpm-software-management/mock/releases/download/${P}-1/${P}.tar.gz"

LICENSE="GPL-2+"
SLOT="0"
KEYWORDS="~amd64"
IUSE="test"
REQUIRED_USE="${PYTHON_REQUIRED_USE}"
RESTRICT="!test? ( test )"

RDEPEND="
	${PYTHON_DEPS}
	$(python_gen_cond_dep '
		app-arch/rpm[caps,python,${PYTHON_SINGLE_USEDEP}]
		dev-python/backoff[${PYTHON_USEDEP}]
		dev-python/distro[${PYTHON_USEDEP}]
		dev-python/pyroute2[${PYTHON_USEDEP}]
		dev-python/requests[${PYTHON_USEDEP}]
		dev-python/rpmautospec-core[${PYTHON_USEDEP}]
		dev-python/templated-dictionary[${PYTHON_USEDEP}]
	')
	acct-group/mock
	|| ( app-containers/podman sys-apps/dnf5 )
	dev-util/distribution-gpg-keys
	dev-util/mock-core-configs
"
BDEPEND="
	${PYTHON_DEPS}
	$(python_gen_cond_dep 'dev-python/argparse-manpage[${PYTHON_USEDEP}]')
	test? ( $(python_gen_cond_dep '
		dev-python/jsonschema[${PYTHON_USEDEP}]
		dev-python/pytest[${PYTHON_USEDEP}]
	') )
"

src_prepare() {
	default

	sed -i -e "s|^VERSION\s*=.*|VERSION=\"${PV}\"|" \
		-e "s|^SYSCONFDIR\s*=.*|SYSCONFDIR=\"${EPREFIX}/etc\"|" \
		-e "s|^PYTHONDIR\s*=.*|PYTHONDIR=\"$(python_get_sitedir)\"|" \
		-e "s|^PKGPYTHONDIR\s*=.*|PKGPYTHONDIR=\"$(python_get_sitedir)/mockbuild\"|" \
		py/mockbuild/constants.py py/mock-parse-buildlog.py || die

	sed -i -e "s|^_MOCK_NVR = None$|_MOCK_NVR = \"${P}-1\"|" py/mock.py || die
	sed -i -e "s|@VERSION@|${PV}|" docs/mock.1 docs/mock-parse-buildlog.1 || die

	sed -i -e 's|"/bin/free"|"/usr/bin/free"|' py/mockbuild/plugins/hw_info.py || die

	# bug #928266: remove unnecessary completion target for us
	sed -i -e "s/ mock.py//" -e "s/ mock-parse-buildlog.py//" etc/bash_completion.d/mock || die
}

src_compile() {
	argparse-manpage --pyfile py/mock-hermetic-repo.py --function _argparser > mock-hermetic-repo.1 || die
}

src_install() {
	python_domodule py/mockbuild
	python_newscript py/mock.py mock
	python_newscript py/mock-hermetic-repo.py mock-hermetic-repo
	python_newscript py/mock-parse-buildlog.py mock-parse-buildlog
	dobin mockchain

	dobashcomp etc/bash_completion.d/mock
	bashcomp_alias mock mock-parse-buildlog
	dodoc docs/site-defaults.cfg
	doman docs/mock.1 docs/mock-parse-buildlog.1 mock-hermetic-repo.1

	insinto /etc/mock
	doins etc/mock/*
	insinto /etc/pki/mock
	doins etc/pki/*

	diropts -m0775 -o root -g mock
	keepdir /var/lib/mock
}

src_test() {
	PYTHONPATH=py epytest tests
}

pkg_postinst() {
	optfeature "chain mode support" app-arch/createrepo_c
	optfeature "pigz in root_cache plugin" app-arch/pigz
	optfeature "nspawn instead of chroot" sys-apps/systemd
	optfeature "lvm_root plugin support" sys-fs/lvm2
	optfeature "procenv plugin support" sys-process/procenv
}
