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

EAPI=8

inherit autotools pam rebar systemd

# Must match the version (and SRI hashes) used by the mod_invites templates, see
# tools/dl_invites_page_deps.sh.
BOOTSTRAP_PV=5.3.8

DESCRIPTION="Robust, scalable and extensible XMPP server"
HOMEPAGE="https://www.ejabberd.im/ https://github.com/processone/ejabberd/"
SRC_URI="
	https://github.com/processone/${PN}/archive/refs/tags/${PV}.tar.gz -> ${P}.tar.gz
	https://github.com/twbs/bootstrap/releases/download/v${BOOTSTRAP_PV}/bootstrap-${BOOTSTRAP_PV}-dist.zip
"

LICENSE="GPL-2+ MIT"
SLOT="0"
KEYWORDS="~amd64 ~arm ~sparc ~x86"
IUSE="captcha debug lua mssql mysql odbc pam postgres redis roster-gw selinux
	sip sqlite +stun zlib"
REQUIRED_USE="mssql? ( odbc )"

RESTRICT="test"

# Note that we use --enable-system-deps, which causes the build to fail if a
# dependency listed in rebar.config is not installed.
DEPEND="
	>=dev-lang/erlang-27[odbc?,ssl]
	acct-group/ejabberd
	>=acct-user/ejabberd-2[pam?]
	>=dev-erlang/cache_tab-1.0.34
	>=dev-erlang/eimp-1.0.27
	>=dev-erlang/erlydtl-0.15.0
	>=dev-erlang/fast_tls-1.1.26
	>=dev-erlang/fast_xml-1.1.61
	>=dev-erlang/fast_yaml-1.0.40
	>=dev-erlang/idna-7.1.0
	<dev-erlang/idna-8
	>=dev-erlang/jose-1.11.13
	>=dev-erlang/mqtree-1.0.19
	>=dev-erlang/p1_acme-1.0.31
	>=dev-erlang/p1_oauth2-0.6.14
	>=dev-erlang/p1_utils-1.0.29
	>=dev-erlang/pkix-1.0.10
	>=dev-erlang/stringprep-1.0.34
	>=dev-erlang/xmpp-1.13.5
	>=dev-erlang/yconf-1.0.23
	lua? (
		>=dev-erlang/luerl-1.2
		<dev-erlang/luerl-1.3
	)
	mysql? ( >=dev-erlang/p1_mysql-1.0.28 )
	odbc? ( dev-db/unixODBC )
	pam? ( >=dev-erlang/epam-1.0.14 )
	postgres? ( >=dev-erlang/p1_pgsql-1.1.41 )
	redis? ( >=dev-erlang/eredis-1.7.1 )
	sip? ( >=dev-erlang/esip-1.0.61 )
	sqlite? (
		dev-db/sqlite:3
		>=dev-erlang/sqlite3-1.1.15
	)
	stun? ( >=dev-erlang/stun-1.2.23 )
	zlib? ( >=dev-erlang/ezlib-1.0.16 )
"
RDEPEND="
	${DEPEND}
	captcha? ( media-gfx/imagemagick[truetype,png] )
	selinux? ( sec-policy/selinux-jabber )
"
BDEPEND="app-arch/unzip"

DOCS=( CHANGELOG.md README.md )

PATCHES=(
	"${FILESDIR}/ejabberd-22.10-adjust-ejabberd.service.template-to-Gentoo.patch"
)

# Get path to ejabberd lib directory, without EPREFIX.
get_ejabberd_path() {
	echo "/usr/$(get_libdir)/${P}"
}

src_prepare() {
	default

	# Ship the bootstrap files used by mod_invites' landing page, instead
	# of letting the build system download them. The Makefile installs
	# them if they are present.
	local bootstrap_src="${WORKDIR}/bootstrap-${BOOTSTRAP_PV}-dist"
	local bootstrap_dst="priv/mod_invites/static/bootstrap"
	mkdir -p "${bootstrap_dst}"/{css,js} || die
	cp "${bootstrap_src}"/css/bootstrap.min.css "${bootstrap_dst}"/css/ || die
	cp "${bootstrap_src}"/js/bootstrap.min.js "${bootstrap_dst}"/js/ || die

	# Fix bug #591862. ERL_LIBS should point directly to ejabberd directory
	# rather than its parent which is default. That way ejabberd directory
	# takes precedence is module lookup.
	local ejabberd_erl_libs="${EPREFIX}$(get_ejabberd_path):${EPREFIX}$(get_erl_libs)"
	sed -i -e "s|^ERL_LIBS='{{libdir}}'$|ERL_LIBS='${ejabberd_erl_libs}'|" \
		ejabberdctl.template || die
	grep -q "^ERL_LIBS='${ejabberd_erl_libs}'$" ejabberdctl.template \
		|| die 'failed to set ERL_LIBS in ejabberdctl.template'

	eautoreconf
}

src_configure() {
	# The Makefile appends lib/ejabberd and log/ejabberd to localstatedir.
	econf \
		--localstatedir="${EPREFIX}/var" \
		--enable-user=${PN} \
		--with-rebar=rebar \
		--enable-system-deps \
		--disable-bootstrap \
		$(use_enable debug) \
		$(use_enable lua) \
		$(use_enable mssql) \
		$(use_enable mysql) \
		$(use_enable odbc) \
		$(use_enable pam) \
		$(use_enable postgres pgsql) \
		$(use_enable redis) \
		$(use_enable roster-gw roster-gateway-workaround) \
		$(use_enable sqlite) \
		$(use_enable sip) \
		$(use_enable stun) \
		$(use_enable zlib)
}

src_compile() {
	# Build the scripts (ejabberdctl.example, ejabberd.service) here, as
	# otherwise the 'install' target would generate ejabberdctl.example
	# with DESTDIR leaking into the paths.
	emake REBAR='rebar -v' src scripts
}

src_install() {
	default

	# Upstream installs this into the docdir.
	rm "${ED}/usr/share/doc/${PF}/COPYING" || die

	# Upstream only installs the SQLite schema (bug #627214).
	insinto "$(get_ejabberd_path)/priv/sql"
	use mssql && doins sql/mssql*.sql
	use mysql && doins sql/mysql*.sql
	use postgres && doins sql/pg*.sql

	use pam && pamd_mimic_system xmpp auth account

	newconfd "${FILESDIR}/${PN}.confd" "${PN}"
	newinitd "${FILESDIR}/${PN}-22.05.initd" "${PN}"
	systemd_dounit "${PN}.service"

	insinto /etc/logrotate.d
	newins "${FILESDIR}/${PN}-22.05.logrotate" "${PN}"

	keepdir /var/{lib,log}/ejabberd
}