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

EAPI=8

# TODO
PYTHON_COMPAT=( python3_14 )

inherit python-single-r1

MY_P="astrometry.net-${PV}"

DESCRIPTION="Automatic recognition of astronomical images"
HOMEPAGE="https://astrometry.net/"
# since the link is hard to find:
# https://github.com/dstndstn/astrometry.net
SRC_URI="https://astrometry.net/downloads/${MY_P}.tar.gz"

S="${WORKDIR}/${MY_P}"

LICENSE="BSD GPL-3+"
SLOT="0"
KEYWORDS="~amd64"
REQUIRED_USE="${PYTHON_REQUIRED_USE}"

DEPEND="
	x11-libs/cairo
	media-libs/netpbm
	media-libs/libpng
	media-libs/libjpeg-turbo
	virtual/zlib
	app-arch/bzip2
	dev-python/numpy
	dev-lang/swig
	dev-python/fitsio
	sci-libs/cfitsio
	sci-libs/gsl
"
RDEPEND="
	${DEPEND}
    ${PYTHON_DEPS}
"
BDEPEND="
	virtual/pkgconfig
	doc? (
		dev-python/numpydoc
	)
"

HTML_DOCS="${S}/doc/html/"
IUSE="doc"

# TODO: python?
# TODO: missing SONAME libastrometry.so
# TODO: deal with upstream's autoconfig? (-march=znver4? with -march=native
# from my config?)
# TODO: optional dependencies?
# TODO: use flags
# TODO: system GSL
# TODO: make extra
# TODO: make py
# TODO: tests? (one of them seem to segfault)
# TODO: index data handling

src_compile() {
	emake SYSTEM_GSL=1 all
	if use doc; then
		emake SYSTEM_GSL=1 doc
	fi
}

src_install() {
	# lib install dir: multilib-strict check fail unless lib64
	# py base link dir: a symlink path prefix
	emake INSTALL_DIR="${D}/usr" \
		ETC_INSTALL_DIR="${D}/etc" \
		MAN1_INSTALL_DIR="${D}/usr/share/man/man1" \
		DOC_INSTALL_DIR="${D}/usr/share/doc/${PF}" \
		DATA_INSTALL_DIR="${D}/usr/share/astrometry/data" \
		EXAMPLE_INSTALL_DIR="${D}/usr/share/astrometry/example" \
		PY_BASE_INSTALL_DIR="${D}$(python_get_sitedir)/astrometry" \
		PY_BASE_LINK_DIR="../..$(python_get_sitedir)/astrometry" \
		LIB_INSTALL_DIR="${D}/usr/lib64/" \
		FINAL_DIR="/usr/share/astrometry" \
		SYSTEM_GSL="yes" \
		install
	if use doc; then
		einstalldocs
	fi
	python_fix_shebang "${D}/usr/bin/"
	python_fix_shebang "${D}$(python_get_sitedir)/astrometry/"
	python_optimize
	# TODO: the extra file added by this is also attempted to be loaded, but
	# fails and continues anyways
	keepdir "/usr/share/astrometry/data"
}