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

EAPI=8

inherit go-module

DESCRIPTION="Matrix server written in Go"
HOMEPAGE="https://matrix.org https://codefloe.com/pat-s/zendrite"

LICENSE="AGPL-3 Apache-2.0 BSD MIT MPL-2.0 ZLIB"
SLOT="0"
KEYWORDS="~amd64"
IUSE=""

DEPEND=""
RDEPEND="acct-user/dendrite acct-group/dendrite"
BDEPEND=""
RDEPEND="!net-matrix/dendrite"

go-module_set_globals

SRC_URI="https://codefloe.com/pat-s/zendrite/archive/v${PV}.tar.gz -> ${P}.tar.gz
	https://files.anton.molyboha.me/gentoo-files/net-matrix/zendrite/${P}-deps.tar.xz"
S="${WORKDIR}/zendrite"
#GO_IMPORTPATH="github.com/matrix-org/dendrite"
#EGO_PN="${GO_IMPORTPATH}/cmd/..."

#OUT_GOPATH="${S}/go-path"

#src_unpack() {
#	go-module_src_unpack || die
#}

src_compile() {
	#env GOPATH="${OUT_GOPATH}":/usr/lib/go-gentoo GOCACHE="${T}"/go-cache CGO_ENABLED=1 \
	#	go build -trimpath -v -x -work -o bin/ "${S}"/cmd/... || die
	GOBIN="${S}/bin" \
		ego install "${S}/cmd/..."
}

src_test() {
	GOBIN="${S}/bin" \
		ego test "${S}/cmd/..."
}

# TODO
src_install() {
	local f
	for f in $(ls "${S}/bin/") ; do
		dobin "${S}/bin/${f}"
	done

	dodir "/etc/dendrite"
	insinto /etc/dendrite
	doins "${S}/zendrite-sample.yaml"
	newinitd "${FILESDIR}"/zendrite-2.2.0.initd zendrite
	newconfd "${FILESDIR}"/zendrite-2.2.0.confd zendrite

	keepdir "/var/log/dendrite"
	fowners dendrite:dendrite "/var/log/dendrite"

	einfo "For compatibility with dendrite, the installation directory names"
	einfo "keep the word 'dendrite' in the name: /etc/dendrite, /var/lib/dendrite,"
	einfo "/var/log/dendrite"
	einfo ""
	einfo "If you are upgrading from dendrite, copy your old"
	einfo "    ${EPREFIX}/etc/dendrite/dendrite.yaml"
	einfo "into"
	einfo "    ${EPREFIX}/etc/dendrite/zendrite.yaml"
	einfo "and then update if necessary."
	einfo ""
	einfo "If this is a new installation of zendrite, you most likely still need"
	einfo "to do a few configuration steps and/or install some other programs. See"
	einfo "https://codefloe.com/pat-s/zendrite/src/branch/main/website/src/content/docs/installation"
	einfo "for detailed instructions."
	einfo "The sample config has been installed into"
	einfo "    ${EPREFIX}/etc/dendrite/zendrite-sample.yaml"
	einfo "Copy it into ${EPREFIX}/etc/dendrite/zendrite.yaml"
	einfo "and edit to your liking."
	einfo "The OpenRC service is called 'zendrite'"
}