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

EAPI=8

inherit systemd

MY_PN=${PN%-bin}
MY_P=${MY_PN}-${PV}
MY_URI="https://github.com/vitaly-zdanevich/telegram_channel_to_static_website/releases/download/v${PV}/${MY_PN}"

DESCRIPTION="Prebuilt Telegram channel to self-contained Zola static website generator"
HOMEPAGE="
	https://vitaly-zdanevich.github.io/telegram_channel_to_static_website/
	https://github.com/vitaly-zdanevich/telegram_channel_to_static_website
"
SRC_URI="
	amd64? (
		${MY_URI}-x86_64-unknown-linux-musl.tar.gz
			-> ${MY_P}-x86_64-unknown-linux-musl.tar.gz
	)
	arm64? (
		${MY_URI}-aarch64-unknown-linux-musl.tar.gz
			-> ${MY_P}-aarch64-unknown-linux-musl.tar.gz
	)
"
S="${WORKDIR}"

# tg2zola and its statically linked musl and Rust dependencies are included.
LICENSE="MIT"
LICENSE+="
	0BSD Apache-2.0 Apache-2.0-with-LLVM-exceptions BSD BSD-2 Boost-1.0
	CDLA-Permissive-2.0 ISC LGPL-2.1+ MIT MPL-2.0 Unicode-3.0 Unlicense ZLIB
	public-domain
"
SLOT="0"
KEYWORDS="-* ~amd64 ~arm64"

RDEPEND="
	!www-apps/tg2zola
	acct-group/tg2zola
	acct-user/tg2zola
"

QA_PREBUILT="usr/bin/${MY_PN}"

src_install() {
	dobin ${MY_PN}

	exeinto /usr/libexec/${MY_PN}
	doexe "${FILESDIR}/${MY_PN}-sync"

	insinto /etc/${MY_PN}
	doins "${FILESDIR}/${MY_PN}.conf"
	fowners root:tg2zola /etc/${MY_PN}/${MY_PN}.conf
	fperms 0640 /etc/${MY_PN}/${MY_PN}.conf

	newinitd "${FILESDIR}/${MY_PN}.initd" ${MY_PN}
	systemd_dounit "${FILESDIR}/${MY_PN}.service"
	systemd_dounit "${FILESDIR}/${MY_PN}.timer"

	keepdir /var/lib/${MY_PN}
	fowners tg2zola:tg2zola /var/lib/${MY_PN}
	fperms 0750 /var/lib/${MY_PN}

	dodoc README.md
}

pkg_postinst() {
	if [[ -z ${REPLACING_VERSIONS} ]]; then
		elog "This package installs the prebuilt static musl binary built with default features (without MTProto)."
		elog "Install www-apps/tg2zola[mtproto] if you need the optional MTProto backend."
		elog "Configure CHANNELS in /etc/tg2zola/tg2zola.conf for automatic daily regeneration."
		elog "OpenRC: rc-update add tg2zola default && rc-service tg2zola start"
		elog "systemd: systemctl enable --now tg2zola.timer"
		elog "Install Zola (e.g. www-apps/zola from ::guru) to build the generated sites."
	fi
}