# Maintainer: Lex Black <autumn-wind@web.de>
# Contributor: Thomas Berryhill (oats) <tb01110100 at gmail dot com>
# Contributor: Felix Yan <felixonmars at archlinux dot org>
# Contributor: Bartłomiej Piotrowski <bpiotrowski at archlinux dot org>
# Contributor: Thomas Dziedzic <gostrc at gmail dot com>
# Contributor: Angel Velasquez <angvp at archlinux dot org>
# Contributor: Alexander Fehr <pizzapunk gmail com>
# Contributor: Daniel J Griffiths <ghost1227 at archlinux dot us>

pkgname=aria2-git
_pkgname=${pkgname%-git}
pkgver=1.37.0.r42.g9e727358
pkgrel=1
pkgdesc="Download utility that supports HTTP(S), FTP, BitTorrent, and Metalink"
arch=('i686' 'x86_64' 'aarch64')
url="https://${_pkgname}.github.io"
license=('GPL-2.0-or-later AND (OpenSSL OR LicenseRef-SSLeay)')
depends=('gnutls' 'libxml2' 'sqlite' 'c-ares' 'ca-certificates' 'libssh2')
makedepends=('git')
checkdepends=('cppunit')
conflicts=("${_pkgname}")
provides=("${_pkgname}=${pkgver%.*}")
source=("git+https://github.com/tatsuhiro-t/${_pkgname}.git")
sha256sums=('SKIP')

pkgver() {
	cd "${_pkgname}/"
	git describe --long | sed 's/\([^-]*-g\)/r\1/;s/-/./g;s/release.//'
}

prepare() {
	cd "${_pkgname}/"

	# Fix build against Nettle 4.0
	git cherry-pick -n be1caa2db4912df28d4d9236beb64e7a17b8cdfe

	autoreconf -i
}

build() {
	cd "${_pkgname}/"

	./configure \
		--prefix=/usr \
		--enable-libaria2 \
		--with-ca-bundle=/etc/ssl/certs/ca-certificates.crt

	make
}

check() {
	cd "${_pkgname}/"

	# https://github.com/aria2/aria2/issues/1476
	# Upstream states "I don't see any issues with aria2 code."
	make check || echo "Ignoring test failures"
}

package() {
	cd "${_pkgname}/"
	make DESTDIR="${pkgdir}" install

	# add bash completion
	install -d "${pkgdir}/usr/share/bash-completion/completions"
	install -m644 "${pkgdir}/usr/share/doc/${_pkgname}/bash_completion/aria2c" \
		"${pkgdir}/usr/share/bash-completion/completions"

	rm -rf "${pkgdir}/usr/share/doc/${pkgname}/bash_completion"
	install -Dm644 {COPYING,LICENSE.OpenSSL} -t "${pkgdir}/usr/share/licenses/${pkgname}/"
}
