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

EAPI=8

inherit desktop xdg

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

DESCRIPTION="Arduino IDE - graphical IDE for Arduino boards (Electron-based)"
HOMEPAGE="https://www.arduino.cc/"
SRC_URI="https://downloads.arduino.cc/arduino-ide/arduino-ide_${PV}_Linux_64bit.zip"

LICENSE="AGPL-3"
SLOT="0"
KEYWORDS="~amd64"

# Runtime dependencies for Electron-based application
RDEPEND="
	app-accessibility/at-spi2-core: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
	sys-libs/glibc
	x11-libs/cairo
	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
"

BDEPEND="app-arch/unzip"

RESTRICT="strip"

QA_PREBUILT="opt/${MY_PN}/*"

src_unpack() {
	default
	
	# The zip extracts to a subdirectory, we need to find it
	cd "${WORKDIR}" || die
	
	# Find the extracted Arduino IDE directory
	local extracted_dir
	extracted_dir=$(find . -maxdepth 1 -type d ! -path . -name "*arduino*" | head -n1)
	
	if [[ -z "${extracted_dir}" ]]; then
		die "Could not find extracted Arduino IDE directory"
	fi
	
	# Rename it to what Portage expects (${P})
	if [[ "${extracted_dir}" != "./${P}" ]]; then
		einfo "Renaming ${extracted_dir} to ${P}"
		mv "${extracted_dir}" "${P}" || die "Failed to rename directory"
	fi
}

src_install() {
	# Install to /opt
	insinto /opt/${MY_PN}
	doins -r "${S}"/*
	
	# Make main executable
	fperms +x /opt/${MY_PN}/arduino-ide
	
	# Make chrome binaries executable
	if [[ -f "${ED}/opt/${MY_PN}/chrome-sandbox" ]]; then
		fperms +x /opt/${MY_PN}/chrome-sandbox
	fi
	
	if [[ -f "${ED}/opt/${MY_PN}/chrome_crashpad_handler" ]]; then
		fperms +x /opt/${MY_PN}/chrome_crashpad_handler
	fi
	
	# Make all .so files executable
	local so_file
	while IFS= read -r -d '' so_file; do
		fperms +x "${so_file#${ED}}"
	done < <(find "${ED}/opt/${MY_PN}" -name "*.so*" -print0)
	
	# Make arduino-cli executable (critical for Arduino IDE to work)
	if [[ -f "${ED}/opt/${MY_PN}/resources/app/lib/backend/resources/arduino-cli" ]]; then
		fperms +x /opt/${MY_PN}/resources/app/lib/backend/resources/arduino-cli
	fi
	
	# Make clangd executable (for code completion)
	if [[ -f "${ED}/opt/${MY_PN}/resources/app/lib/backend/resources/clangd" ]]; then
		fperms +x /opt/${MY_PN}/resources/app/lib/backend/resources/clangd
	fi
	
	# Make any other binaries in backend/resources executable
	local backend_file
	while IFS= read -r -d '' backend_file; do
		# Skip .json, .txt, .template files
		if [[ "${backend_file}" != *.json ]] && \
		   [[ "${backend_file}" != *.txt ]] && \
		   [[ "${backend_file}" != *.template ]] && \
		   [[ "${backend_file}" != *.html ]] && \
		   [[ "${backend_file}" != *.css ]]; then
			fperms +x "${backend_file#${ED}}"
		fi
	done < <(find "${ED}/opt/${MY_PN}/resources/app/lib/backend/resources" -type f -print0 2>/dev/null)
	
	# Try to find and install icon
	local icon_file
	for icon_file in \
		"${ED}/opt/${MY_PN}/resources/app/resources/icons/512x512.png" \
		"${ED}/opt/${MY_PN}/resources/icons/512x512.png" \
		"${ED}/opt/${MY_PN}/icon.png"; do
		if [[ -f "${icon_file}" ]]; then
			newicon -s 512 "${icon_file}" arduino-ide.png
			break
		fi
	done
	
	# Create desktop entry
	make_desktop_entry \
		"/opt/${MY_PN}/arduino-ide %U" \
		"Arduino IDE" \
		"arduino-ide" \
		"Development;IDE;Electronics" \
		"MimeType=text/x-arduino;x-scheme-handler/arduino;\nStartupWMClass=arduino-ide"
	
	# Create symlink in /usr/bin
	dosym ../../opt/${MY_PN}/arduino-ide /usr/bin/arduino-ide
}

pkg_postinst() {
	xdg_pkg_postinst
	
	elog "Arduino IDE has been installed to /opt/${MY_PN}"
	elog "You can launch it by running 'arduino-ide' or from your application menu."
	elog ""
	elog "Note: This is a binary package built with Electron."
	elog "For first-time users, you may need to add your user to the 'uucp' or 'dialout'"
	elog "group to access serial ports:"
	elog "  usermod -a -G uucp <username>"
	elog ""
	elog "On some systems, you may also need:"
	elog "  usermod -a -G dialout <username>"
}

pkg_postrm() {
	xdg_pkg_postrm
}