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

EAPI=8

inherit go-module systemd

DESCRIPTION="Lightweight log shipper for Logstash and Elasticsearch"
HOMEPAGE="https://www.elastic.co/beats/filebeat https://github.com/elastic/beats"

# go mod vendor stamps every file with the run's wall-clock mtime and tar records
# them, so two generations of the same version differ in hash. Upload the tarball
# before running `ebuild ... manifest`, or the digest will not match what is at
# the URL; pkgcheck reports DeadUrl until the upload lands.
# To generate the vendor tarball:
#   tar -xf ${P}.tar.gz
#   cd beats-${PV}
#   rm -r x-pack
#   go mod vendor
#   cd ..
#   tar --sort=name --mtime=@0 --owner=0 --group=0 --numeric-owner \
#       -cf - beats-${PV}/vendor | xz -9e >"${P}-vendor.tar.xz"
SRC_URI="
	https://github.com/elastic/beats/archive/v${PV}.tar.gz -> ${P}.tar.gz
	https://github.com/vklimovs/portage-overlay/releases/download/${P}-vendor.tar.xz/${P}-vendor.tar.xz
"
S="${WORKDIR}/beats-${PV}"

LICENSE="Apache-2.0 BSD BSD-2 CC-BY-3.0 CC0-1.0 EPL-2.0 ISC MIT MPL-2.0 public-domain PYTHON ZLIB"
SLOT="0"
KEYWORDS="~amd64"
# Pin comes from .go-version at the beats tag, not go.mod.
BDEPEND=">=dev-lang/go-1.26.7"

src_prepare() {
	default
	rm -r x-pack || die
}

src_compile() {
	cd filebeat || die
	ego build -mod=vendor -trimpath -buildmode=pie -o filebeat .
}

src_install() {
	dobin filebeat/filebeat
	insinto /etc/${PN}
	doins filebeat/${PN}.yml
	keepdir /var/lib/${PN}
	newinitd "${FILESDIR}/${PN}.initd" ${PN}
	systemd_dounit "${FILESDIR}/${PN}.service"
}