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

EAPI=8

inherit desktop xdg

DESCRIPTION="kDrive desktop sync client for Infomaniak cloud storage"
HOMEPAGE="https://www.infomaniak.com/en/kdrive"
SRC_URI="https://download.storage.infomaniak.com/drive/desktopclient/kDrive-${PV}-amd64.AppImage"

S="${WORKDIR}"

LICENSE="GPL-3"
SLOT="0"
KEYWORDS="~amd64"
RESTRICT="mirror strip bindist"

RDEPEND="
	dev-libs/glib:2
	dev-libs/nss
	sys-apps/dbus
	x11-libs/libxcb
	x11-libs/libxkbcommon
	x11-libs/libX11
	x11-libs/libXcomposite
	x11-libs/libXdamage
	x11-libs/libXext
	x11-libs/libXfixes
	x11-libs/libXrandr
	x11-misc/xdg-utils
"

QA_PREBUILT="*"
QA_DESKTOP_FILE="usr/share/applications/kDrive.desktop"

# Install path without -bin suffix (Gentoo convention: -bin is package name, not directory)
KDRIVE_HOME="/opt/${PN/-bin/}"

src_unpack() {
	cp "${DISTDIR}/kDrive-${PV}-amd64.AppImage" "${S}/kDrive.AppImage" || die
	chmod +x "${S}/kDrive.AppImage" || die
	"${S}/kDrive.AppImage" --appimage-extract >/dev/null 2>&1 || die "AppImage extraction failed"
	rm "${S}/kDrive.AppImage" || die
}

src_prepare() {
	# Normalize permissions on extracted AppImage tree before installation.
	# doins -r always installs as 0644, but consistent source permissions
	# help with debugging and avoid surprises.
	find "${S}/squashfs-root" -type d -exec chmod a+rx {} + || die
	find "${S}/squashfs-root" -type f -exec chmod a+r {} + || die
	chmod a+x "${S}/squashfs-root/AppRun" || die

	local bin_dir
	for bin_dir in usr/bin usr/libexec; do
		if [[ -d "${S}/squashfs-root/${bin_dir}" ]]; then
			find "${S}/squashfs-root/${bin_dir}" -type f -exec chmod a+x {} + || die
		fi
	done
	find "${S}/squashfs-root" -type f -name '*.so*' -exec chmod a+x {} + || die

	default
}

src_install() {
	insinto "${KDRIVE_HOME}"
	doins -r squashfs-root/*

	# doins strips execute bits; restore them for the AppImage to work.
	fperms +x "${KDRIVE_HOME}/AppRun"

	local bin_dir
	for bin_dir in usr/bin usr/libexec; do
		if [[ -d "${ED}${KDRIVE_HOME}/${bin_dir}" ]]; then
			find "${ED}${KDRIVE_HOME}/${bin_dir}" -type f -exec chmod +x {} + || die
		fi
	done
	find "${ED}${KDRIVE_HOME}" -type f -name '*.so*' -exec chmod +x {} + || die

	# sync-exclude.lst is expected at the AppImage root by the kDrive binary
	dosym usr/bin/sync-exclude.lst "${KDRIVE_HOME}/sync-exclude.lst"

	# Chrome sandbox: setuid root with restricted permissions (discord pattern)
	if [[ -f "${ED}${KDRIVE_HOME}/chrome-sandbox" ]]; then
		fowners root "${KDRIVE_HOME}/chrome-sandbox"
		fperms 4711 "${KDRIVE_HOME}/chrome-sandbox"
	fi

	# Wrapper script
	cat > "${T}/kDrive" <<-EOF || die
		#!/bin/sh
		exec ${KDRIVE_HOME}/AppRun "\$@"
	EOF
	dobin "${T}/kDrive"

	# Desktop file
	if [[ -f squashfs-root/kDrive.desktop ]]; then
		sed -i \
			-e "s|Exec=.*|Exec=kDrive %U|g" \
			-e "s|Icon=.*|Icon=kDrive|g" \
			squashfs-root/kDrive.desktop || die
		domenu squashfs-root/kDrive.desktop
	elif [[ -f squashfs-root/kdrive.desktop ]]; then
		sed -i \
			-e "s|Exec=.*|Exec=kDrive %U|g" \
			-e "s|Icon=.*|Icon=kDrive|g" \
			squashfs-root/kdrive.desktop || die
		domenu squashfs-root/kdrive.desktop
	else
		make_desktop_entry "kDrive" "kDrive" "kDrive" "Network;FileTransfer;"
	fi

	# Icons from hicolor theme (upstream uses kdrive-win.png)
	local size icon_path
	for size in 16 32 48 64 128 256 512; do
		for icon_path in \
			squashfs-root/usr/share/icons/hicolor/${size}x${size}/apps/kDrive.png \
			squashfs-root/usr/share/icons/hicolor/${size}x${size}/apps/kdrive.png \
			squashfs-root/usr/share/icons/hicolor/${size}x${size}/apps/kdrive-win.png; do
			if [[ -f "${icon_path}" ]]; then
				newicon -s ${size} "${icon_path}" kDrive.png
				break
			fi
		done
	done

	# Fallback pixmap (root icon may be a symlink, dereference it)
	local root_icon
	for root_icon in squashfs-root/kDrive.png squashfs-root/kdrive.png squashfs-root/kdrive-win.png; do
		if [[ -e "${root_icon}" ]]; then
			newicon "$(realpath "${root_icon}")" kDrive.png
			break
		fi
	done
}

pkg_postinst() {
	xdg_pkg_postinst

	elog "kDrive has been installed to ${KDRIVE_HOME}"
	elog ""
	elog "To launch: kDrive"
	elog ""
	elog "Features:"
	elog "  - Real-time file synchronization with Infomaniak kDrive"
	elog "  - Selective folder sync"
	elog "  - LAN sync for faster local transfers"
	elog ""
	elog "First run: Sign in with your Infomaniak account"
	elog ""
	elog "Upstream: https://www.infomaniak.com/en/kdrive"
}