# Copyright 2023 lmkra
# Distributed under the terms of the GNU General Public License v3

EAPI=8

inherit cargo git-r3

DESCRIPTION="High Performance Matrix Homeserver in Rust"
HOMEPAGE="https://github.com/matrix-construct/tuwunel"
EGIT_REPO_URI="https://github.com/matrix-construct/${PN}.git"
EGIT_BRANCH="main"

LICENSE="Apache-2.0"
SLOT="0"

S="${WORKDIR}/${P}"/src/main

IUSE="+brotli +element +gzip +io-uring jemalloc systemd +zstd debug"

RDEPEND="acct-user/matrix"
RDEPEND="
	acct-user/matrix
	!net-im/conduit
	!net-im/conduwuit
"
DEPEND="${RDEPEND}"
BDEPEND="
	llvm-core/clang
	>=dev-lang/rust-1.88.0:*
"
# skipped systemd support for now

QA_FLAGS_IGNORED="usr/bin/${PN}"

src_unpack() {
	git-r3_src_unpack
	cargo_live_src_unpack
}

src_configure() {
	local myfeatures=(
		media_thumbnail
		url_preview
		$(usev jemalloc)
		$(usev systemd)
		$(usev brotli brotli_compression)
		$(usev element element_hacks)
		$(usev gzip gzip_compression)
		$(usev io-uring io_uring)
		$(usev !debug release_max_log_level)
		$(usev zstd zstd_compression)
	)

	rust_pkg_setup
	cargo_src_configure --no-default-features --frozen
}

src_install() {
	cargo_src_install

	keepdir /var/{lib,log}/tuwunel
	fowners matrix:matrix /var/{lib,log}/tuwunel
	fperms 700 /var/{lib,log}/tuwunel

	newconfd "${FILESDIR}"/tuwunel.conf-r1 tuwunel
	newinitd "${FILESDIR}"/tuwunel.init-r1 tuwunel

	insinto /etc/tuwunel
	newins "${WORKDIR}/${P}"/tuwunel-example.toml tuwunel.toml

	insinto /etc/logrotate.d
	newins "${FILESDIR}"/tuwunel.logrotate-r1 tuwunel
}