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

EAPI=8

inherit desktop unpacker xdg

MY_PN="${PN/-bin/}"

DESCRIPTION="Claude Desktop - AI assistant application for Linux (unofficial)"
HOMEPAGE="https://claude.ai https://github.com/leobuskin/unofficial-claude-desktop-linux"
SRC_URI="https://github.com/leobuskin/unofficial-claude-desktop-linux/releases/download/v${PV}/${MY_PN}_${PV}_amd64.deb"

S="${WORKDIR}"

LICENSE="all-rights-reserved"
SLOT="0"
KEYWORDS="-* ~amd64"
RESTRICT="mirror strip bindist"

RDEPEND="
	>=app-accessibility/at-spi2-core-2.46.0:2
	dev-libs/expat
	dev-libs/glib:2
	dev-libs/nspr
	dev-libs/nss
	media-libs/alsa-lib
	media-libs/mesa
	net-print/cups
	sys-apps/dbus
	x11-libs/cairo
	x11-libs/gdk-pixbuf:2
	x11-libs/gtk+:3
	x11-libs/libdrm
	x11-libs/libX11
	x11-libs/libxcb
	x11-libs/libXcomposite
	x11-libs/libXdamage
	x11-libs/libXext
	x11-libs/libXfixes
	x11-libs/libxkbcommon
	x11-libs/libXrandr
	x11-libs/pango
	x11-misc/xdg-utils
"

QA_PREBUILT="*"

src_install() {
	# .deb places app in usr/lib/claude-desktop/ — install to /opt/claude-desktop/
	mkdir -p "${ED}/opt/${MY_PN}" || die
	cp -r "${S}/usr/lib/${MY_PN}/"* "${ED}/opt/${MY_PN}" || die

	# Chrome sandbox: setuid root with restricted permissions (Electron pattern)
	local electron_dir="/opt/${MY_PN}/node_modules/electron/dist"
	if [[ -f "${ED}${electron_dir}/chrome-sandbox" ]]; then
		fperms 4755 "${electron_dir}/chrome-sandbox"
	fi

	# Wrapper script adapted from upstream usr/bin/claude-desktop
	cat > "${T}/${MY_PN}" <<-'WRAPPER' || die
		#!/bin/bash
		INSTALL_DIR="/opt/claude-desktop"
		export ELECTRON_FORCE_IS_PACKAGED=true

		# Proxy support
		PROXY_ARGS=""
		PROXY_URL="${https_proxy:-${HTTPS_PROXY:-${http_proxy:-${HTTP_PROXY:-}}}}"
		NO_PROXY_LIST="${no_proxy:-${NO_PROXY:-}}"
		if [ -n "$PROXY_URL" ]; then
		    PROXY_ARGS="--proxy-server=$PROXY_URL"
		    [ -n "$NO_PROXY_LIST" ] && PROXY_ARGS="$PROXY_ARGS --proxy-bypass-list=$NO_PROXY_LIST"
		fi

		exec "$INSTALL_DIR/node_modules/electron/dist/electron" \
		    "$INSTALL_DIR/app.asar" \
		    ${WAYLAND_DISPLAY:+--ozone-platform-hint=auto --enable-features=WaylandWindowDecorations} \
		    $PROXY_ARGS "$@"
	WRAPPER
	dobin "${T}/${MY_PN}"

	# Desktop file - fix paths
	if [[ -f "${S}/usr/share/applications/${MY_PN}.desktop" ]]; then
		sed -i \
			-e "s|Exec=.*|Exec=${MY_PN} %U|g" \
			-e "s|Icon=.*|Icon=${MY_PN}|g" \
			"${S}/usr/share/applications/${MY_PN}.desktop" || die
		domenu "${S}/usr/share/applications/${MY_PN}.desktop"
	else
		make_desktop_entry "${MY_PN}" "Claude Desktop" "${MY_PN}" "Utility;"
	fi

	# Icons (available: 16 24 32 48 64 128 256 512)
	local size icon_path
	for size in 16 24 32 48 64 128 256 512; do
		icon_path="${S}/usr/share/icons/hicolor/${size}x${size}/apps/${MY_PN}.png"
		if [[ -f "${icon_path}" ]]; then
			newicon -s ${size} "${icon_path}" ${MY_PN}.png
		fi
	done
}

pkg_postinst() {
	xdg_pkg_postinst

	elog "Claude Desktop has been installed to /opt/${MY_PN}"
	elog ""
	elog "To launch: ${MY_PN}"
	elog ""
	elog "This is an unofficial community repackage of Claude Desktop"
	elog "for Linux. It is not officially supported by Anthropic."
	elog ""
	elog "Upstream: https://github.com/leobuskin/unofficial-claude-desktop-linux"
	elog "Official: https://claude.ai"
}