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

# Based on AUR package by SCDevel <root@scdevel.net>
# Source: https://launcher.hytale.com/version/release/launcher.json

EAPI=8

inherit desktop xdg-utils

DESCRIPTION="Hytale Launcher (Native Linux, Self-Updating)"
HOMEPAGE="https://hytale.com"

# Version format: YYYYMMDD
# Commit hash from launcher.json (update when bumping)
MY_COMMIT="17f7b59"
MY_DATE="${PV:0:4}.${PV:4:2}.${PV:6:2}"

SRC_URI="https://launcher.hytale.com/builds/release/linux/amd64/hytale-launcher-${MY_DATE}-${MY_COMMIT}.zip -> ${P}.zip"

S="${WORKDIR}"

LICENSE="all-rights-reserved"
SLOT="0"
KEYWORDS="~amd64"
RESTRICT="bindist mirror strip"

RDEPEND="
	x11-misc/xdg-utils
	dev-libs/glib:2
	media-libs/libpng
	net-libs/libsoup:3.0
	net-libs/webkit-gtk:4.1
	sys-libs/glibc
	x11-libs/gdk-pixbuf:2
	x11-libs/gtk+:3
	sys-apps/iproute2
"
BDEPEND="app-arch/unzip"

QA_PREBUILT="opt/${PN}/hytale-launcher"

src_install() {
	# Install main binary
	exeinto /opt/${PN}
	doexe hytale-launcher

	# Install wrapper script
	dobin "${FILESDIR}"/hytale-launcher

	# Install nautilus crash workaround
	exeinto /opt/${PN}/nautilus-fix
	newexe "${FILESDIR}"/nautilus-fix.sh nautilus

	# Install desktop entry and icon
	domenu "${FILESDIR}"/com.hypixel.HytaleLauncher.desktop
	doicon -s scalable "${FILESDIR}"/com.hypixel.HytaleLauncher.png

	# Install license
	dodoc "${FILESDIR}"/LICENSE
}

pkg_postinst() {
	xdg_desktop_database_update
	xdg_icon_cache_update

	elog "========================================="
	elog "Hytale Launcher has been installed!"
	elog "========================================="
	elog ""
	elog "On first run, the launcher will be copied to:"
	elog "  ~/.local/share/hytale-launcher/bin/"
	elog ""
	elog "Requirements:"
	elog "  - IPv6 must NOT be disabled (required by Hytale authentication)"
	elog "  - System clock must be synchronized (for authentication)"
	elog ""
	elog "To check IPv6 status: cat /proc/sys/net/ipv6/conf/all/disable_ipv6"
	elog "  (0 = enabled, 1 = disabled)"
	elog ""
	elog "To check clock sync: timedatectl show -p NTPSynchronized --value"
	elog "  (should return 'yes')"
	elog ""
	elog "Known issue workaround for Java VM initialization error:"
	elog "  hytale-launcher -d ~/.local/share/Hytale/install/temp"
	elog ""
	elog "This is an unofficial community package."
	elog "Not affiliated with Hypixel Studios."
	elog "========================================="
}

pkg_postrm() {
	xdg_desktop_database_update
	xdg_icon_cache_update

	elog "========================================="
	elog "Hytale Launcher has been removed."
	elog ""
	elog "User data is NOT automatically removed:"
	elog "  ~/.local/share/hytale-launcher/"
	elog "  ~/.local/share/Hytale/"
	elog ""
	elog "Delete these manually if no longer needed."
	elog "========================================="
}