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

EAPI=8
PYTHON_COMPAT=( python3_{11,12,13} )

inherit git-r3 meson python-single-r1

DESCRIPTION="Next generation OpenVPN client"
HOMEPAGE="https://openvpn.net"
EGIT_REPO_URI="https://codeberg.org/OpenVPN/openvpn3-linux.git"

if [[ ${PV} != 9999 ]]; then
	KEYWORDS="~amd64"
	EGIT_COMMIT="v${PV}"
fi

LICENSE="AGPL-3+"
SLOT="0"
IUSE="+dco selinux systemd"

DEPEND="${PYTHON_DEPS}
	acct-group/openvpn
	acct-user/openvpn
	dev-libs/jsoncpp:=
	sys-libs/libcap-ng:=
	app-arch/lz4:=
	dev-libs/glib:=
	dev-cpp/gdbuspp
	dev-libs/tinyxml2:=
	selinux? (
	        sys-libs/libselinux
	)
	systemd? ( sys-apps/systemd:= )
	dco? (
		>=dev-libs/protobuf-2.4.0:=
		>=dev-libs/libnl-3.2.29:=
	)"
RDEPEND="${DEPEND}
	$(python_gen_cond_dep 'dev-python/pyopenssl[${PYTHON_USEDEP}]')
	$(python_gen_cond_dep 'dev-python/pygobject[${PYTHON_USEDEP}]')
	$(python_gen_cond_dep 'dev-python/dbus-python[${PYTHON_USEDEP}]')
	systemd? ( $(python_gen_cond_dep 'dev-python/python-systemd[${PYTHON_USEDEP}]') )"
BDEPEND="${PYTHON_DEPS}
	dev-build/autoconf-archive
	dco? (
		<sys-kernel/linux-headers-6.16
	)"

src_prepare() {
	default
	sed -i -e "/openvpn2/,/openvpn3-as/{s/install: true/install: false/}" \
		src/python/meson.build \
		|| die "sed failed"
	#eapply_user
	#./update-version-m4.sh
	#eautoreconf
}

src_configure() {
	local emesonargs=(
		-Ddocdir="${ROOT}/usr/share/doc/${PF}"
		-Dopenvpn3_statedir=/var/lib/openvpn3
		-Ddco=$(usex dco enabled disabled)
		-Dselinux=$(usex selinux enabled disabled)
		-Dunit_tests=disabled
		-Dtest_programs=disabled
	)
	meson_src_configure
}

src_test() {
	./src/tests/unit/unit-tests || die
}

src_install() {
	meson_src_install
	python_optimize
	keepdir /var/lib/openvpn3/configs
}
