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

EAPI=8

inherit desktop

HOMEPAGE="https://www.blockbench.net/"
DESCRIPTION="A low poly 3D model editor"
SRC_URI="https://github.com/JannisX11/blockbench/releases/download/v${PV}/Blockbench_${PV}.AppImage -> ${P}.AppImage"

S="${WORKDIR}"

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

src_unpack() {
    cp "${DISTDIR}/${P}.AppImage" "${T}/${P}.AppImage" || die
    chmod +x "${T}/${P}.AppImage" || die
    cd "${WORKDIR}" || die
    "${T}/${P}.AppImage" --appimage-extract || die "Failed to extract the content from AppImage"
}

src_install() {
    insinto /opt/blockbench
    doins -r squashfs-root/*

    local exe_path=$(find "${D}/opt/blockbench" -type f -name "blockbench" -o -name "Blockbench" ! -name "*.so" | head -n 1)
    if [[ -n "${exe_path}" ]]; then
          chmod +x "${exe_path}"
          local exe_name=$(basename "${exe_path}")

          cat <<EOF > "${T}/blockbench"
#!/bin/sh
exec /opt/blockbench/${exe_name} "\$@"
EOF
          dobin "${T}/blockbench"
    fi

    local icon_path=$(find "${D}/opt/blockbench" -type f -name "blockbench.png" -o -name "icon.png" | head -n 1)
    if [[ -n "${icon_path}" ]]; then
          newicon -s 256 "${icon_path}" blockbench.png
    fi

    make_desktop_entry blockbench "Blockbench" blockbench "Graphics;3DGraphics;"
}