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

EAPI=8

inherit go-module systemd

GIT_COMMIT=ec3720f9fea0be9b1f85ed40c1c4c5b493c1784f

DESCRIPTION="Prometheus exporter for apache metrics"
HOMEPAGE="https://github.com/Lusitaniae/apache_exporter"

SRC_URI="https://github.com/Lusitaniae/apache_exporter/archive/v${PV}.tar.gz -> ${P}.tar.gz"
SRC_URI+=" https://github.com/gentoo-golang-dist/${PN}/releases/download/v${PV}/${P}-vendor.tar.xz"

LICENSE="MIT"
SLOT="0"
KEYWORDS="~amd64 ~x86"

DEPEND="
	acct-group/apache_exporter
	acct-user/apache_exporter
"
RDEPEND="${DEPEND}"
BDEPEND=">=dev-lang/go-1.26.0"

src_compile() {
	local go_ldflags=(
		-X github.com/prometheus/common/version.Version=${PV}
		-X github.com/prometheus/common/version.Revision=${GIT_COMMIT}
		-X github.com/prometheus/common/version.Branch=master
		-X github.com/prometheus/common/version.BuildUser=gentoo
		-X github.com/prometheus/common/version.BuildDate="$(date +%F-%T)"
	)
	ego build -ldflags "${go_ldflags[*]}" -tags netgo -o ${PN} .
}

src_test() {
	ego test -tags netgo ./...
}

src_install() {
	dobin ${PN}
	dodoc {README,CHANGELOG}.md

	newinitd "${FILESDIR}"/${PN}.initd ${PN}
	newconfd "${FILESDIR}"/${PN}.confd ${PN}
	systemd_newunit "${FILESDIR}"/${PN}-r1.service ${PN}.service

	insinto /etc/logrotate.d
	newins "${FILESDIR}/${PN}.logrotated" "${PN}"

	keepdir /var/log/apache_exporter
	fowners -R ${PN}:${PN} /var/log/apache_exporter
}