# 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
pkgver=1.9.1
pkgrel=1
epoch=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=(
  gcc-libs
  glibc
  llhttp
  zlib
)
makedepends=(
  cmake
  libssh2
  openssl
  pcre2
  python
)
provides=(libgit2.so)
source=($url/archive/v$pkgver/$pkgname-v$pkgver.tar.gz)
sha512sums=('3bec01704ad1acdb4f7e9454101c2a205b7e288a4dffaa5e1afc2b1f849fa3a42b961c532bed2669841925ab8f84fb35bb82a2df8039b1caf76c5779665032d9')
b2sums=('fac1ecb9ef1589049fe99d54984fb18692ecb4805fc02a1f49193a7d71de75f72b2143214f9239097867b196f90ca45a033b5e65d07ddf1a5b8f00068b1b784a')

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/"
}
