# Copyright 2026 Nadeŭka <me+oss@nadevko.cc>
# Distributed under the terms of the GNU General Public License v2
# Based on www-client/firefox-bin-152.0.7
EAPI=8

inherit desktop linux-info optfeature pax-utils xdg

: ${WMCLASS:=firefox}
: ${MOZ_APP_ARGS:='--name=floorp-bin'}
[[ -v MOZ_GMP_PLUGIN_LIST ]] ||
	MOZ_GMP_PLUGIN_LIST=( gmp-gmpopenh264 gmp-widevinecdm )

DESCRIPTION='Floorp is a new Firefox-based, privacy-preserving browser'
HOMEPAGE='
	https://floorp.app
	https://github.com/Floorp-Projects/Floorp
'

_SRC_URI_BASE="https://github.com/Floorp-Projects/Floorp/releases/download/v${PV}/floorp-linux"
SRC_URI="
	amd64? ( ${_SRC_URI_BASE}-x86_64.tar.xz )
	arm64? ( ${_SRC_URI_BASE}-aarch64.tar.xz )
"
S="${WORKDIR}"/floorp
LICENSE='MPL-2.0 GPL-2 LGPL-2.1'

SLOT=0
KEYWORDS='amd64 ~arm64'
IUSE='+gmp-autoupdate wayland'
RESTRICT=strip

QA_PREBUILT='opt/floorp-bin/*'

RDEPEND='
	|| (
		media-libs/libpulse
		media-sound/apulse
	)
	>=app-accessibility/at-spi2-core-2.46.0:2
	>=dev-libs/glib-2.26:2
	media-libs/alsa-lib
	media-libs/fontconfig
	>=media-libs/freetype-2.4.10
	media-video/ffmpeg
	sys-apps/dbus
	virtual/freedesktop-icon-theme
	>=x11-libs/cairo-1.10[X]
	x11-libs/gdk-pixbuf:2
	>=x11-libs/gtk+-3.11:3[X,wayland?]
	x11-libs/libX11
	x11-libs/libXcomposite
	x11-libs/libXcursor
	x11-libs/libXdamage
	x11-libs/libXext
	x11-libs/libXfixes
	x11-libs/libXi
	x11-libs/libXrandr
	x11-libs/libXrender
	x11-libs/libxcb
	>=x11-libs/pango-1.22.0
'

pkg_setup() {
	CONFIG_CHECK='~SECCOMP'
	WARNING_SECCOMP="CONFIG_SECCOMP not set! This system will be unable \
		to play DRM-protected content"

	linux-info_pkg_setup
}

floorp_make_maintainer_prefs() {
	cp "${FILESDIR}"/maintainer-prefs.js "${1}"

	if ! use gmp-autoupdate; then
		for plugin in "${MOZ_GMP_PLUGIN_LIST[@]}" ; do
			einfo "Disabling auto-update for '${plugin}' plugin ..."
			echo 'pref("media.'"${plugin}"'.autoupdate", false);' >> "${1}" ||
				die "failed to disable autoupdate for ${plugin} media plugin"
		done
	fi

	doins "${1}"
}

floorp_make_wrapper() {
	cp "${FILESDIR}"/floorp-bin "${1}"
	local exports=MOZ_APP_LAUNCHER

	if has_version -r media-sound/apulse[-sdk]; then
		ewarn 'media-sound/apulse[-sdk] detected!'
		ewarn 'www-client/floorp-bin wrapper will patch LD_LIBRARY_PATH to force'
		ewarn 'load apulse instead of any existing sound server'

		{
			echo -n 'LD_LIBRARY_PATH="'
			echo -n "${EPREFIX}"/usr/$(get_libdir)/apulse
			echo '${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}"'
			exports+=' LD_LIBRARY_PATH'
		} >> "${1}"
	fi

	{
		echo "MOZ_APP_LAUNCHER='${MOZ_APP_LAUNCHER}'"
		echo "export ${exports}"
		echo "exec '${MOZILLA_FIVE_HOME}/floorp-bin' ${MOZ_APP_ARGS}"' "${@}"'
		echo
	} >> "${1}"

	dobin "${1}"
}

floorp_make_desktop_entry() {
	sed \
		-e "s|@MOZ_APP_EXEC@|${MOZ_APP_LAUNCHER}|g" \
		-e "s|@WMCLASS@|${WMCLASS}|g" \
		"${FILESDIR}"/floorp-bin.desktop > "${1}" ||
		die 'Failed to patch floorp-bin.desktop'
	domenu "${1}"
}

floorp_make_icons() {
	local size
	for i in browser/chrome/icons/default/default*.png; do
		size=${i##*/default}
		size=${size%.png}
		newicon -s ${size} "${i}" floorp-bin.png
	done
}

src_install() {
	local MOZILLA_FIVE_HOME=/opt/floorp-bin
	local MOZ_APP_LAUNCHER="${EPREFIX}"/usr/bin/floorp-bin

	dodir "${MOZILLA_FIVE_HOME}"
	cp -a . "${ED}${MOZILLA_FIVE_HOME}"
	touch "${ED}${MOZILLA_FIVE_HOME}"/is-packaged-app

	insinto "${MOZILLA_FIVE_HOME}"/distribution
	doins "${FILESDIR}"/policies.json

	insinto "${MOZILLA_FIVE_HOME}"/browser/defaults/pref
	floorp_make_maintainer_prefs "${T}"/maintainer-prefs.js

	floorp_make_wrapper "${T}"/floorp-bin
	floorp_make_desktop_entry "${T}"/floorp-bin.desktop
	floorp_make_icons

	pax-mark m "${ED}${MOZILLA_FIVE_HOME}"/{floorp,floorp-bin,glxtest,\
		pingsender,updater,vulkantest}
	einstalldocs
}

pkg_postinst() {
	xdg_pkg_postinst

	if ! use gmp-autoupdate; then
		elog 'USE='-gmp-autoupdate' has disabled the following plugins from updating or'
		elog 'installing into new profiles:'
		for plugin in "${MOZ_GMP_PLUGIN_LIST[@]}"; do
			elog "\t ${plugin}"
		done
	fi

	optfeature_header 'Optional programs for extra features:'

	optfeature 'speech syntesis (text-to-speech) support' \
		app-accessibility/speech-dispatcher
	optfeature 'fallback mouse cursor theme e.g. on WMs' \
		gnome-base/gsettings-desktop-schemas
	optfeature 'desktop notifications' \
		x11-libs/libnotify
	optfeature 'desktop integration' \
		sys-apps/xdg-desktop-portal
	optfeature 'pdf and web pages printing' \
		net-print/cups
	optfeature 'spell checking dictionaries support' \
		app-text/hunspell
	optfeature 'automatic text hyphenation support' \
		app-text/hyphen
	optfeature 'location detection via Wi-Fi' \
		net-misc/networkmanager
	use wayland && optfeature 'wayland screen sharing' \
		media-video/pipewire
}