# Copyright 1999-2024 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=8

PYTHON_COMPAT=( python3_{9..12} )

inherit python-r1 systemd

DESCRIPTION="fetch url list and build squid acl"
HOMEPAGE="https://opnsense.org/"
SRC_URI="https://raw.githubusercontent.com/opnsense/plugins/refs/tags/${PV}/www/squid/src/opnsense/scripts/proxy/fetchACLs.py -> ${P}.py"
S=${WORKDIR}

LICENSE="BSD-2"
SLOT="0"
KEYWORDS="amd64"

REQUIRED_USE="${PYTHON_REQUIRED_USE}"

RDEPEND="net-proxy/squid
	${PYTHON_DEPS}
	dev-python/requests[${PYTHON_USEDEP}]
	dev-python/urllib3[${PYTHON_USEDEP}]"

src_unpack() {
	cp ${DISTDIR}/${P}.py ${WORKDIR}/
}

src_prepare() {
	sed -i \
		-e 's|/usr/local/etc/squid/externalACLs.conf|/etc/squid/externalACLs.conf|' \
		-e 's|/usr/local/etc/squid/acl|/var/lib/squid/acl|' \
		${P}.py || die

	cp ${P}.py fetchACLs.py
	eapply -p0 "${FILESDIR}/fetchACLs-github-download.patch"

	eapply_user
}


src_install() {
	dobin fetchACLs.py
	python_replicate_script "${D}"/usr/bin/fetchACLs.py
	newbin ${P}.py fetchACLs-OPNsense.py
	python_replicate_script "${D}"/usr/bin/fetchACLs-OPNsense.py
	systemd_dounit "${FILESDIR}"/fetchACLs.{service,timer}
	insinto /etc/squid
	doins "${FILESDIR}"/externalACLs.conf "${FILESDIR}"/squid.conf.*
	keepdir /var/lib/squid/acl
}
