# 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 - official AI assistant application for Linux"
HOMEPAGE="https://claude.ai"
# Official first-party .deb published by Anthropic via their APT repository.
SRC_URI="https://downloads.claude.ai/claude-desktop/apt/stable/pool/main/c/${MY_PN}/${MY_PN}_${PV}_amd64.deb -> ${P}.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/libXtst
	x11-libs/pango
	x11-misc/xdg-utils
"

QA_PREBUILT="*"

src_install() {
	# Official .deb ships the app under usr/lib/claude-desktop/ (electron
	# launcher 'claude-desktop' + resources/app.asar). Install to /opt/.
	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)
	if [[ -f "${ED}/opt/${MY_PN}/chrome-sandbox" ]]; then
		fperms 4755 "/opt/${MY_PN}/chrome-sandbox"
	fi

	# Wrapper script with Wayland and proxy support
	cat > "${T}/${MY_PN}" <<-'WRAPPER' || die
		#!/bin/bash
		INSTALL_DIR="/opt/claude-desktop"
		export ELECTRON_OZONE_PLATFORM_HINT="${ELECTRON_OZONE_PLATFORM_HINT:-auto}"

		# 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/claude-desktop" \
		    ${WAYLAND_DISPLAY:+--enable-features=WaylandWindowDecorations} \
		    $PROXY_ARGS "$@"
	WRAPPER
	dobin "${T}/${MY_PN}"

	# Official desktop file. Anthropic ships it as com.anthropic.Claude.desktop
	# (reverse-DNS id, not ${MY_PN}.desktop), and it declares
	# MimeType=x-scheme-handler/claude; — the handler the browser needs to hand
	# the claude:// sign-in callback back to the app. Exec=claude-desktop %U
	# resolves to our wrapper on PATH. Install it verbatim: the old lookup for
	# ${MY_PN}.desktop never matched, so it fell through to make_desktop_entry,
	# which drops the MimeType and leaves claude:// unregistered (xdg-open finds
	# no handler -> sign-in never completes).
	if [[ -f "${S}/usr/share/applications/com.anthropic.Claude.desktop" ]]; then
		domenu "${S}/usr/share/applications/com.anthropic.Claude.desktop"
	else
		die "upstream desktop file com.anthropic.Claude.desktop not found; upstream layout changed"
	fi

	# Icons (hicolor 16-256)
	local size icon_path
	for size in 16 32 48 128 256; 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 the official Anthropic Claude Desktop for Linux (beta)."
	elog ""
	elog "Upstream: https://claude.ai"
}