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

EAPI=8

inherit desktop xdg

DESCRIPTION="An offline collection of Homestuck and its related works"
HOMEPAGE="https://github.com/GiovanH/unofficial-homestuck-collection"

NODE_PV="18.20.8"

SRC_URI="
	https://github.com/GiovanH/unofficial-homestuck-collection/archive/refs/tags/v${PV}.tar.gz -> ${P}.tar.gz
	amd64? ( https://nodejs.org/dist/v${NODE_PV}/node-v${NODE_PV}-linux-x64.tar.xz )
"
S="${WORKDIR}/${P}"

LICENSE="GPL-3+"
SLOT="0"
KEYWORDS="~amd64"

RESTRICT="network-sandbox test"

BDEPEND="
	sys-apps/yarn
	dev-python/setuptools
	dev-build/make
	app-arch/tar
	dev-vcs/git
"

RDEPEND="
	media-libs/alsa-lib
	app-accessibility/at-spi2-core
	x11-libs/gtk+:3
	x11-libs/libX11
	x11-libs/libxcb
	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/libXScrnSaver
	x11-libs/libXtst
	dev-libs/glib:2
	dev-libs/nspr
	dev-libs/nss
	x11-libs/cairo
	media-libs/mesa
	x11-libs/pango
	net-print/cups
	x11-libs/gdk-pixbuf:2
	x11-misc/shared-mime-info
	dev-libs/expat
	sys-libs/libxcrypt[compat]
	x11-libs/libdrm
	sys-apps/dbus
"

src_prepare() {
	default

	if [[ ! -d .git ]]; then
		git init
		git config user.name "Gentoo"
		git config user.email "gentoo@gentoo.local"
		git add -A
		git commit -m "Gentoo sandbox initialization commit"
	fi
}

src_compile() {
	export SHARP_IGNORE_GLOBAL_LIBVIPS=true
	export PATH="${WORKDIR}/node-v${NODE_PV}-linux-x64/bin:${PATH}"

	einfo "Building with bundled Node $(node --version) (PATH override)"

	yarn install --frozen-lockfile --ignore-engines ||
		die "yarn install failed"

	if [[ -f Makefile ]]; then
		emake build || die "emake build failed"
	else
		yarn electron:build --ignore-engines ||
			die "yarn electron:build failed"
	fi
}

src_install() {
	local dest="/usr/lib/${PN}"
	local builddir="${S}/dist_electron/linux-unpacked"

	[[ -d "${builddir}" ]] || die "Build directory not found: ${builddir}"

	insinto "${dest}"
	doins -r "${builddir}"/*
	fperms +x "${dest}/${PN}"

	if [[ -f "${builddir}/LICENSES.chromium.html" ]]; then
		dosym \
			"${dest}/LICENSES.chromium.html" \
			"/usr/share/licenses/${PN}/LICENSES.chromium.html"
	fi

	if [[ -f "${builddir}/LICENSE.electron.txt" ]]; then
		dosym \
			"${dest}/LICENSE.electron.txt" \
			"/usr/share/licenses/${PN}/LICENSE.electron.txt"
	fi

	cat >"${T}/${PN}" <<-EOF || die
		#!/bin/sh
		exec /usr/lib/${PN}/${PN} "\$@"
	EOF
	dobin "${T}/${PN}"

	local iconpath="${builddir}/resources/icons"
	[[ -d "${iconpath}" ]] || iconpath="${S}/build/icons"

	if [[ -d "${iconpath}" ]]; then
		local size
		for size in 16 24 32 48 64 128 256 512 1024; do
			if [[ -f "${iconpath}/${size}x${size}.png" ]]; then
				newicon -s "${size}" \
					"${iconpath}/${size}x${size}.png" \
					"${PN}.png"
			fi
		done
	fi

	if [[ -f "${S}/${PN}.desktop" ]]; then
		domenu "${S}/${PN}.desktop"
	else
		make_desktop_entry \
			"${PN}" \
			"Unofficial Homestuck Collection" \
			"${PN}" \
			"Game;AdventureGame" \
			"Comment=Unofficial Reader For Homestuck"
	fi
}