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

EAPI=8

inherit systemd

DESCRIPTION="Remote Desktop Protocol server"
HOMEPAGE="https://www.xrdp.org https://github.com/neutrinolabs/xrdp"
SRC_URI="https://github.com/neutrinolabs/${PN}/releases/download/v${PV}/${P}.tar.gz"

LICENSE="Apache-2.0 MIT"
SLOT="0"
KEYWORDS="~amd64"
IUSE="fuse imlib ipv6 jpeg mp3 opus +pam systemd x264"

RDEPEND="
	dev-libs/openssl:0=
	x11-libs/libX11
	x11-libs/libXfixes
	x11-libs/libXrandr
	fuse? ( sys-fs/fuse:3 )
	imlib? ( media-libs/imlib2:= )
	jpeg? ( media-libs/libjpeg-turbo:= )
	mp3? ( media-sound/lame )
	opus? ( media-libs/opus )
	pam? ( sys-libs/pam )
	systemd? ( sys-apps/systemd )
	x264? ( media-libs/x264:= )
"
DEPEND="${RDEPEND}"
BDEPEND="virtual/pkgconfig"

src_configure() {
	local myeconfargs=(
		--disable-fdkaac
		--disable-kerberos
		--disable-neutrinordp
		--disable-openh264
		--disable-pixman
		--disable-rdpsndaudin
		--disable-smartcard
		--disable-ulalaca
		--disable-utmp
		--disable-vsock
		--disable-xrdpvr
		--without-freetype2
		$(use_enable fuse)
		$(use_with imlib imlib2)
		$(use_enable ipv6)
		$(use_enable jpeg)
		$(use_enable jpeg tjpeg)
		$(use_enable mp3 mp3lame)
		$(use_enable opus)
		$(use_enable pam)
		$(use_with systemd systemdsystemunitdir "$(systemd_get_systemunitdir)")
		$(use_enable x264)
		--with-pamconfdir="${EPREFIX}"/etc/pam.d
	)

	econf "${myeconfargs[@]}"
}

src_install() {
	default

	if ! use systemd; then
		# Upstream ships an LSB init script (instfiles/init.d/xrdp) that
		# sources /lib/lsb/init-functions, which OpenRC does not provide:
		# `/etc/init.d/xrdp status` dies with "No such file or directory".
		# Replace it with proper OpenRC init scripts.
		rm -f "${ED}"/etc/init.d/xrdp || die
		newinitd "${FILESDIR}/xrdp.initd" xrdp || die
		newinitd "${FILESDIR}/xrdp-sesman.initd" xrdp-sesman || die
	fi
}

pkg_postinst() {
	if use systemd; then
		elog "Enable the xrdp service with: systemctl enable --now xrdp"
	else
		elog "Enable the xrdp service with: rc-update add xrdp default"
		elog "(this also starts the xrdp-sesman service it depends on)"
	fi
}