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

# Autogenerated by pycargoebuild 0.15.1

EAPI=8

CRATES="
"

LLVM_COMPAT=( {16..21} )

inherit cargo llvm-r2 linux-info

DESCRIPTION="High Performance Matrix Homeserver in Rust!"
HOMEPAGE="
	https://tuwunel.chat
	https://github.com/matrix-construct/tuwunel
"
SRC_URI="
	https://github.com/matrix-construct/${PN}/archive/refs/tags/v${PV}.tar.gz -> ${P}.tar.gz
	https://vimja.cloud/public.php/dav/files/z59eKDyLFokW2KK/${CATEGORY}/${PN}/${P}-vendor.tar.xz
"

# The repository's root directory is a "workspace root".
# It's easier and less error prone to work in src/main.
S="${WORKDIR}/${P}/src/main"

LICENSE="Apache-2.0"
# Dependent crate licenses
LICENSE+="
	Apache-2.0 BSD-2 BSD BlueOak-1.0.0 CDLA-Permissive-2.0 ISC MIT
	MPL-2.0 UoI-NCSA Unicode-3.0 Unlicense ZLIB
"
SLOT="0"
KEYWORDS="~amd64 ~arm64"
IUSE="jemalloc system-rocksdb gzip zstd lz4 bzip2 systemd brotli ldap io-uring"

# bzip2 looks difficult to un-bundle:
#   https://wiki.gentoo.org/wiki/Project:Rust/sys_crates#bzip2-sys
# lz4 cannot be un-bundled:
#   https://wiki.gentoo.org/wiki/Writing_Rust_ebuilds#Unbundling_C_libraries
COMMON_DEPEND="
	jemalloc? ( dev-libs/jemalloc:= )
	app-arch/snappy:=
	zstd? (
		app-arch/zstd:=
	)
	system-rocksdb? (
		dev-libs/rocksdb
	)
"
RDEPEND="
	${COMMON_DEPEND}
	acct-user/tuwunel
	app-misc/ca-certificates
"
# clang needed for bindgen
DEPEND="
	${COMMON_DEPEND}
	$(llvm_gen_dep '
		llvm-core/clang:${LLVM_SLOT}
		llvm-core/llvm:${LLVM_SLOT}
	')
"

# Required for pkg-config crate:
#   https://wiki.gentoo.org/wiki/Writing_Rust_ebuilds#pkg-config_crate
BDEPEND="virtual/pkgconfig"

# rust does not use *FLAGS from make.conf, silence portage warning
# update with proper path to binaries this crate installs, omit leading /
QA_FLAGS_IGNORED="usr/bin/${PN}"

# TODO
DOCS=( {APPSERVICES,CODE_OF_CONDUCT,DEPLOY,README,TURN}.md )

pkg_setup() {
	CONFIG_CHECK=""

	# IO-Uring requires kernel support.
	if use io-uring; then
		CONFIG_CHECK+=" ~IO_URING"
		ERROR_IO_URING="${P} requires io-uring in-kernel support."
	fi

	if [[ -n ${CONFIG_CHECK} ]]; then
		linux-info_pkg_setup
	fi

	llvm-r2_pkg_setup
	rust_pkg_setup
}

# See here for how this works and why it's needed:
#   https://wiki.gentoo.org/wiki/Writing_Rust_ebuilds#Using_a_vendor_tarball_like_in_Go_ebuilds
src_prepare() {
	ln -s "${WORKDIR}/vendor/" "${WORKDIR}/${P}/vendor" || die

	sed -i "${ECARGO_HOME}/config.toml" -e '/source.crates-io/d'  || die
	sed -i "${ECARGO_HOME}/config.toml" -e '/replace-with = "gentoo"/d'  || die
	sed -i "${ECARGO_HOME}/config.toml" -e '/local-registry = "\/nonexistent"/d'  || die

	cat "${WORKDIR}/vendor/vendor-config.toml" >> "${ECARGO_HOME}/config.toml" || die

	eapply_user
}

src_configure() {
	# Required for pkg-config crate:
	#   https://wiki.gentoo.org/wiki/Writing_Rust_ebuilds#pkg-config_crate
	export PKG_CONFIG_ALLOW_CROSS=1
	# Required to un-bundle jemalloc:
	#   https://wiki.gentoo.org/wiki/Writing_Rust_ebuilds#Common_-sys_crates
	export JEMALLOC_OVERRIDE="${ESYSROOT}/usr/$(get_libdir)/libjemalloc.so"

	if use zstd; then
		# Required to un-bundle zstd:
		#   https://wiki.gentoo.org/wiki/Project:Rust/sys_crates#zstd-sys
		export ZSTD_SYS_USE_PKG_CONFIG=1
	fi

	if use system-rocksdb; then
		# https://wiki.gentoo.org/wiki/Writing_Rust_ebuilds#Common_-sys_crates
		export SNAPPY_LIB_DIR="${ESYSROOT}/usr/$(get_libdir)"
		export ROCKSDB_INCLUDE_DIR="${ESYSROOT}/usr/include"
		export ROCKSDB_LIB_DIR="${ESYSROOT}/usr/$(get_libdir)"
	fi

	local myfeatures=(
		element_hacks
		media_thumbnail
		release_max_log_level
		url_preview

		$(usev io-uring io_uring)
		$(usev systemd)
		$(usev jemalloc)
		$(usev ldap)
		$(usev jemalloc jemalloc_conf)
		$(usev zstd zstd_compression)
		$(usev lz4 lz4_compression)
		$(usev gzip gzip_compression)
		$(usev bzip2 bzip2_compression)
		$(usev brotli brotli_compression)
	)

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

src_install() {
	cargo_src_install

	keepdir /var/lib/tuwunel
	fowners tuwunel:tuwunel /var/lib/tuwunel
	fperms 750 /var/lib/tuwunel

	keepdir /var/log/tuwunel
	fowners tuwunel:tuwunel /var/log/tuwunel
	fperms 750 /var/log/tuwunel

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

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

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

	# TODO: Add systemd service.
}
