# Maintainer: David Runge <dvzrv@archlinux.org>
# Maintainer: Lukas Fleischer <lfleischer@archlinux.org>
# Contributor: Hilton Medeiros <medeiros.hilton@gmail.com>
# Contributor: Dave Reisner <d@falconindy.com>

pkgname=libgit2
# NOTE: add the following packages to rebuild TODOs on soname change (although they do not link against libgit2.so):
# julia
epoch=1
pkgver=1.9.4
pkgrel=1
pkgdesc="A linkable library for Git"
arch=(x86_64 aarch64)
url="https://github.com/libgit2/libgit2"
license=(LicenseRef-GPL-2.0-only-with-linking-exception)
depends=(
  libgcc
  glibc
  llhttp
  zlib
)
makedepends=(
  cmake
  libssh2
  openssl
  pcre2
  python
)
provides=(libgit2.so)
source=($url/archive/v$pkgver/$pkgname-v$pkgver.tar.gz)
sha512sums=('85036ade3aca33c5283605ae9de21a7948ec5952bc8cd468aa024ca873851a56ab0ebe62f95c0da109df9163875e9687a377d3df69728d434cc258b3f845ef0c')
b2sums=('3d8c71d1371668423259b610c4387e538b7450a23d89206fde2421382f6c19430e607f6fb0a92333f8f6a15a8dd22da1924e8c495cf8ea1c9975f6df161764c0')

build() {
  local cmake_options=(
    -B build
    -D CMAKE_BUILD_TYPE=None
    -D CMAKE_INSTALL_PREFIX=/usr
    -D REGEX_BACKEND=pcre2
    -D USE_HTTP_PARSER=llhttp
    -D USE_SSH=ON
    -S $pkgname-$pkgver
    -W no-dev
  )
  cmake "${cmake_options[@]}"
  cmake --build build --verbose
}

check() {
  local ignored_tests=(
    'auth_clone|'
    'invasive|'
    'online|'
    'network_url_parse__hostname_implied_root_empty_port|'
    'network_url_parse__hostname_empty_port|'
    'network_url_parse__ipv4_implied_root_empty_port|'
    'network_url_parse__ipv4_empty_port|'
    'network_url_parse__ipv6_implied_root_empty_port|'
    'network_url_parse__ipv6_empty_port|'
    'proxy|'
    'proxy_auto_not_detected|'
    'ssh'
  )
  local ifs="$IFS"
  IFS=
  # NOTE: disable tests requiring the internet, relying on non-existent
  # resources, or those that are only compatible with the (modified) vendored
  # version of http-parser, but not with upstream http-parser
  ctest --test-dir build --output-on-failure -E "${ignored_tests[*]}"
  IFS="$ifs"
}

package() {
  depends+=(
    libssh2 libssh2.so
    openssl libcrypto.so libssl.so
    pcre2 libpcre2-8.so
  )

  DESTDIR="$pkgdir" cmake --install build
  install -vDm 644 $pkgname-$pkgver/{AUTHORS,README.md} -t "$pkgdir/usr/share/doc/$pkgname/"
  install -vDm 644 $pkgname-$pkgver/COPYING -t "$pkgdir/usr/share/licenses/$pkgname/"
}
