# Copyright 2026 Anoncheg1
# Distributed under the terms of the GNU General Public License v3

EAPI=8

PYTHON_COMPAT=( python3_{11..14} )

inherit python-single-r1

DESCRIPTION="Micro file server"
HOMEPAGE="https://github.com/Anoncheg1/micro_file_server"
MFS_VERSION="${PV}"
SRC_URI="https://github.com/Anoncheg1/micro_file_server/archive/refs/tags/v${MFS_VERSION}.tar.gz -> ${PN}-${MFS_VERSION}.tar.gz"
S="${WORKDIR}/micro_file_server-${MFS_VERSION}"

LICENSE="AGPL-3.0-or-later"
SLOT="0"
KEYWORDS="~amd64"
IUSE="test +cryptography -gunicorn"

RDEPEND="
	${PYTHON_DEPS}
	$(python_gen_cond_dep '
		dev-python/flask[${PYTHON_USEDEP}]
		dev-python/jinja2[${PYTHON_USEDEP}]
		dev-python/werkzeug[${PYTHON_USEDEP}]
		cryptography? ( dev-python/cryptography[${PYTHON_USEDEP}] )
		gunicorn? ( www-servers/gunicorn[${PYTHON_USEDEP}] )
	')
"
DEPEND=""
BDEPEND="
	${PYTHON_DEPS}
	test? (
		$(python_gen_cond_dep '
			dev-python/pytest[${PYTHON_USEDEP}]
		')
	)
"

REQUIRED_USE="${PYTHON_REQUIRED_USE}"

src_test() {
	use test || return 0
	cd "${S}" || die
	local -x PYTHONPATH="${S}"
	epytest -p no:cacheprovider tests/test_main.py
}

src_install() {
	cd "${S}" || die

	mkdir -p "${T}/micro_file_server" || die
	cp micro_file_server/__main__.py "${T}/micro_file_server/" || die
	touch "${T}/micro_file_server/__init__.py" || die
	(
		cd "${T}" || die
		python_domodule micro_file_server
	)

	insinto "/usr/share/${PN}"
	doins -r .cert

	if use test; then
		insinto "/usr/share/${PN}/tests"
		doins tests/test_main.py
	fi

	cp packaging/guix/wrapper.py "${T}/micro-file-server" || die
	python_doscript "${T}/micro-file-server"

	doman micro-file-server.1
}