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

EAPI=8

inherit go-module systemd

DESCRIPTION="Prometheus exporter for MySQL server metrics"
HOMEPAGE="https://github.com/prometheus/mysqld_exporter"
SRC_URI="https://github.com/prometheus/mysqld_exporter/archive/v${PV}.tar.gz -> ${P}.tar.gz
	https://www.inode64.com/dist/${P}-vendor.tar.xz"

LICENSE="Apache-2.0"
# Dependent licenses
LICENSE+=" BSD BSD-2 MIT MPL-2.0"
SLOT="0"
KEYWORDS="~amd64"

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

DOCS=( CHANGELOG.md CONTRIBUTING.md README.md )

src_compile() {
	# A release tarball has no git hash, and livecheck would leave a
	# hard-coded one stale: the revision is the tag.
	local go_ldflags=(
		-X github.com/prometheus/common/version.Version=${PV}
		-X github.com/prometheus/common/version.Revision=v${PV}
		-X github.com/prometheus/common/version.Branch=HEAD
		-X github.com/prometheus/common/version.BuildUser=gentoo
		-X github.com/prometheus/common/version.BuildDate="$(date +%Y%m%d-%T)"
	)
	ego build -ldflags "${go_ldflags[*]}" -o bin/${PN} .
	./bin/${PN} --help-man > ${PN}.1 || die
}

src_test() {
	ego test ./...
}

src_install() {
	dobin bin/${PN}
	einstalldocs
	doman ${PN}.1

	# The home, /var/lib/mysqld_exporter, belongs to acct-user, which sets its
	# mode: installing it here too would switch its mode on every reinstall.
	keepdir /var/log/mysqld_exporter
	fowners ${PN}:${PN} /var/log/mysqld_exporter

	systemd_dounit "${FILESDIR}"/${PN}.service
	newinitd "${FILESDIR}"/${PN}.initd ${PN}
	newconfd "${FILESDIR}"/${PN}.confd ${PN}

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

pkg_postinst() {
	if [[ -z ${REPLACING_VERSIONS} ]]; then
		elog "Create \"${EROOT}/var/lib/mysqld_exporter/.my.cnf\" to read MySQL credentials from file."
	fi
}