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

EAPI=8

PYTHON_COMPAT=( python3_{11..14} )
DISTUTILS_USE_PEP517=setuptools
PYPI_NO_NORMALIZE=1
inherit distutils-r1 optfeature shell-completion xdg-utils

DESCRIPTION="pass extension for importing data from most existing password managers"
HOMEPAGE="
	https://github.com/roddhjav/pass-import/
	https://pypi.org/project/pass-import/
"
# lacking test files in sdist
SRC_URI="
	https://github.com/roddhjav/pass-import/releases/download/v${PV}/${P}.tar.gz
		-> ${P}.gh.tar.gz
"

LICENSE="GPL-3"
SLOT="0"
KEYWORDS="~amd64"

RDEPEND="
	app-admin/pass
	dev-python/pyaml[${PYTHON_USEDEP}]
	dev-python/requests[${PYTHON_USEDEP}]
	dev-python/zxcvbn[${PYTHON_USEDEP}]
"
BDEPEND="
	test? (
		sys-apps/dbus
	)
"

EPYTEST_PLUGINS=()
distutils_enable_tests pytest

PATCHES=(
	"${FILESDIR}"/pass-import-3.5-dont-install-data.patch
)

python_prepare_all() {
	distutils-r1_python_prepare_all

	xdg_environment_reset
	mkdir -p "${XDG_DATA_HOME}"/keyrings || die
	cp tests/assets/db/gnome-keyring.keyring "${XDG_DATA_HOME}"/keyrings/pass-import.keyring || die
}

python_test() {
	EPYTEST_DESELECT=(
		# Tries to touch system networkmanager
		tests/test_open.py::TestOpen::test_open_networkmanager

		# FIXME
		tests/imports/test_parse.py::TestParse::test_import_gnome_keyring
	)
	EPYTEST_IGNORE=()

	if ! has_version dev-python/pykeepass; then
		EPYTEST_DESELECT+=(
			tests/imports/test_parse.py::TestParse::test_import_keepass
		)
	fi

	if ! has_version dev-python/jsonpath-ng; then
		EPYTEST_IGNORE+=(
			tests/filters/test_filter_jsonpath.py
		)
	fi

	local dbus_params=(
		$(dbus-daemon --session --print-address --fork --print-pid)
	)
	local -x DBUS_SESSION_BUS_ADDRESS=${dbus_params[0]}

	epytest

	kill "${dbus_params[1]}" || die
}

src_install() {
	distutils-r1_src_install

	doman share/man/man1/pass-import.1 share/man/man1/pimport.1
	dobashcomp share/bash-completion/completions/pass-import \
		share/bash-completion/completions/pimport
	dozshcomp share/zsh/site-functions/_pass-import \
		share/zsh/site-functions/_pimport

	# import.bash seems vestigial from when pass-import was a shell script
}

pkg_postinst() {
	#optfeature "Lastpass cli based import/export" app-admin/lpass
	optfeature "support XML based import" dev-python/defusedxml
	optfeature "Keepass import from KDBX file" dev-python/pykeepass
	optfeature "Gnome Keyring import" dev-python/secretstorage
	optfeature "AndOTP or Aegis encrypted import" dev-python/cryptography
	#optfeature "Detection of file decryption" dev-python/file-magic
	optfeature "Filter exports" dev-python/jsonpath-ng
}