# Contributor: Michael Pirogov <vbnet.ru@gmail.com>
# Maintainer: Michael Pirogov <vbnet.ru@gmail.com>
pkgname=loki
pkgver=2.9.10
pkgrel=0
pkgdesc="Like Prometheus, but for logs"
pkgusers="loki"
pkggroups="grafana"
options="net"
url="https://github.com/grafana/loki"
license="Apache-2.0"
# limited by 64 bit
# loongarch64, riscv64: build failure
arch="all !armhf !armv7 !loongarch64 !riscv64 !x86"
makedepends="go bash"
checkdepends="tzdata"
subpackages="$pkgname-logcli $pkgname-promtail-openrc:promtail_openrc $pkgname-promtail $pkgname-openrc"
source="$pkgname-$pkgver.tar.gz::https://github.com/grafana/loki/archive/refs/tags/v$pkgver.tar.gz
	$pkgname.initd
	$pkgname.confd
	$pkgname-promtail.confd
	$pkgname-promtail.initd
	goflags.patch
	enable-cgo.patch
	"
install="$pkgname.pre-install"

export GOCACHE="${GOCACHE:-"$srcdir/go-cache"}"
export GOTMPDIR="${GOTMPDIR:-"$srcdir"}"
export GOMODCACHE="${GOMODCACHE:-"$srcdir/go"}"
export GOFLAGS="$GOFLAGS -modcacherw -trimpath"

build() {
	make \
		BUILD_IN_CONTAINER=false \
		GOMOD=readonly \
		GIT_REVISION="alpine-r$pkgrel" \
		GIT_BRANCH=main \
		IMAGE_TAG=$pkgver \
		logcli loki promtail
}

check() {
	# not stable test for fluent-bit
	# shell check suggests to break it
	# shellcheck disable=SC2046
	TZ=UTC GOGC=10 go test -p=2 $(go list ./... | grep -Ev 'fluent-bit')
}

package() {
	mkdir -p "$pkgdir"/etc/$pkgname
	cp "$builddir"/cmd/loki/loki-local-config.yaml "$pkgdir"/etc/$pkgname
	install -Dm0755 cmd/$pkgname/$pkgname -t "$pkgdir"/usr/bin
	install -Dm0755 cmd/logcli/logcli -t "$pkgdir"/usr/bin

	install -Dm644 "$srcdir"/$pkgname.confd "$pkgdir"/etc/conf.d/$pkgname
	install -Dm755 "$srcdir"/$pkgname.initd "$pkgdir"/etc/init.d/$pkgname

	install -Dm755 "$srcdir"/$pkgname-promtail.initd "$pkgdir"/etc/init.d/$pkgname-promtail
	install -Dm644 "$srcdir"/$pkgname-promtail.confd "$pkgdir"/etc/conf.d/$pkgname-promtail

	install -D clients/cmd/promtail/promtail-local-config.yaml -t "$pkgdir"/etc/$pkgname/
	install -Dm0755 clients/cmd/promtail/promtail -t "$pkgdir"/usr/bin/
}

logcli() {
	pkgdesc="Loki log client"

	amove usr/bin/logcli
}

promtail() {
	pkgdesc="Promtail client"

	amove etc/$pkgname/promtail-local-config.yaml
	amove usr/bin/promtail
}

promtail_openrc() {
	pkgdesc="Promtail client (Open RC init scripts)"
	install_if="openrc loki-promtail=$pkgver-r$pkgrel"

	amove etc/init.d/$pkgname-promtail
	amove etc/conf.d/$pkgname-promtail
}

sha512sums="
d9c1bbdbd7c04de6ea61de269a3eeb015e1342cfef5567b91a04324f586ce16d24462a9fe56b8c5c1376b677da229768653b41623b092aa4ca6a057d4667edfe  loki-2.9.10.tar.gz
469920d90daebf142db5dc5d3f2e972f59f5f5dc86684c4dcd5924a33ac7db305561006050ad42393296977ea27f1ce1cd78f78c71de351335167b41e4631a85  loki.initd
b4c281e191e7620ffe19d3fcc0b5669f54051a8108f0abc5b1aec2f2026452e02e3c26c3885e1e857cb6007bbc41c1f964e6d25d73bdfe170b67b63ecd9f14fd  loki.confd
f7d33f03d51c3cc5cda082e72f56ec821c60f70f42923a215652517835b66a68d7db3b246cd1a53ce0b2c003fe31f7e6cca06965ba792e92117d7e91abce829f  loki-promtail.confd
ff72431ba978880cc58914302b2ec1e48ef3d16b2a667dc82deaa3d2d890331170757dcac48ba0b145c988e70e85c240c94da335894d2c9d114fa811007f1111  loki-promtail.initd
302f14596207037fb7ee892e5c214e857c30cf358b00ae51be2e1639f7e0bbf005d63d16a6c9b3384174827d7ed5c70d873e08050c1635e8a8c5c930ca85df68  goflags.patch
6847f7e5f149c0dd99d7cef82bc4c50da66355511058d540fbafe8c0db5bc5827dad72f0f7545bc7f58b8e53317fdf8070d826bae56eb69b7e688aa910ca60fc  enable-cgo.patch
"
