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

EAPI=8

inherit go-module

DESCRIPTION="Command line interface for LibreSpeed speed test backends"
HOMEPAGE="https://github.com/librespeed/speedtest-cli"

SRC_URI="
	https://github.com/librespeed/speedtest-cli/archive/refs/tags/v${PV}.tar.gz -> ${P}.tar.gz
	https://files.jacksonchen666.com/gentoo/librespeed-cli-${PV}-vendor.tar.xz
"

# TODO: LGPL-3 or later?
LICENSE="LGPL-3"
# deps license checked as of 7573b65ebc89a4cf463334dbdfab5b3edc706149
LICENSE+=" Apache-2.0 MIT BSD BSD-2"
SLOT="0"
KEYWORDS="~amd64 ~arm64"

#DEPEND=""
RDEPEND="${DEPEND}"
# go version from go.mod
BDEPEND=">=dev-lang/go-1.18"
RESTRICT="mirror"

src_unpack() {
	# unpacks everything, including SRC_URI tarballs
	go-module_src_unpack
	# upstream uses "speedtest-cli", this ebuild uses librespeed-cli to
	# disambiguate from speedtest.net
	#
	# vendor tarball also uses the librespeed-cli in the folder name, so
	# that has to match i guess
	mv "${S}/vendor" "speedtest-cli-${PV}" || die
	rmdir "${P}" || die # remove dir to make sure it renames rather than moves lol
	mv "speedtest-cli-${PV}" "${P}" || die
}

src_compile() {
	DEFS_PATH="github.com/librespeed/speedtest-cli"
	ego build -ldflags "-w -s -X \"${DEFS_PATH}/defs.ProgName=${PN}\" \
		-X \"${DEFS_PATH}/defs.ProgVersion=${PV}\"
		-X \"${DEFS_PATH}/defs.BuildDate=$(date -u "+%Y-%m-%d %H:%M:%S %Z")\"" \
		-o librespeed-cli -trimpath main.go
}

src_install() {
	default

	dobin librespeed-cli
}