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

EAPI=8

inherit desktop unpacker xdg

DESCRIPTION="A Rust-rewrite of latest Minecraft version"
HOMEPAGE="https://playhorizon.dev/"
SRC_URI="https://github.com/horizon-rust/horizon/releases/download/launcher%40${PV}/horizon-launcher-linux-x86_64.tar.gz -> ${P}.tar.gz"

LICENSE="MIT"
SLOT="0"
KEYWORDS="amd64"

S="${WORKDIR}"

src_unpack() {
    default
}

src_install() {
    insinto /opt/horizon-launcher
    doins -r *

    local binary="/opt/horizon-launcher/horizon-launcher"
    if [[ -f "${ED}${binary}" ]]; then
        fperms +x "${binary}"
    fi

    dodir /usr/bin
    cat <<-_EOF > "${ED}/usr/bin/horizon-launcher"
#!/bin/sh
export HORIZON_DATA_DIR="\${XDG_DATA_HOME:-"\$HOME/.local/share"}/horizon"
mkdir -p "\$HORIZON_DATA_DIR"

exec /opt/horizon-launcher/horizon-launcher "\$@"
_EOF

    fperms +x /usr/bin/horizon-launcher

    insinto /usr/share/applications
    cat <<-_EOF > "${ED}/usr/share/applications/horizon-launcher.desktop"
[Desktop Entry]
Type=Application
Name=Horizon Launcher
Comment=A Rust-rewrite of latest Minecraft version
Exec=/usr/bin/horizon-launcher
Icon=horizon-launcher
Terminal=false
Categories=Game;
_EOF
}

pkg_postinst() {
    xdg_desktop_database_update
    xdg_icon_cache_update

    elog "Horizon launcher is installed and ready to use, but application entry is not made."
    elog "You have to run 'horizon-launcher' from terminal to make one "
    elog "also you need to own Minecraft on your Microsoft account to use this!"
}

pkg_postrm() {
    xdg_desktop_database_update
    xdg_icon_cache_update
}