use alienfile;
use Path::Tiny;

# Prefer the system libgit2 if pkg-config can find it (>= 1.9.3).
# Falls back to building from the bundled source tarball.
#
# The floor is 1.9.3 for a bug, not for an API: libgit2 PR #7165 (released in
# 1.9.3) stopped the ssh transport from looping forever on
# LIBSSH2_ERROR_TIMEOUT. Below it, an ssh peer that accepts the connection and
# then goes silent parks the caller indefinitely -- libssh2 does its own reads
# and no libgit2 timeout option reaches that loop, so the caller cannot bound
# it either. Measured: against 1.9.0 a fetch was still blocked after 25 s with
# GIT_OPT_SET_SERVER_TIMEOUT set; against 1.9.3 the same fetch returns at the
# configured timeout. Debian trixie ships 1.9.0, so waiting for the
# distribution to catch up is not a plan -- a system lib below the fix falls
# through to the share build instead.

plugin 'PkgConfig' => (
  pkg_name        => 'libgit2',
  minimum_version => '1.9.3',
);

share {
  start_url 'file://' . Path::Tiny->cwd->child('share/libgit2-1.9.3.tar.gz')->stringify;
  plugin 'Fetch::Local';
  plugin 'Extract' => 'tar.gz';
  plugin 'Build::CMake';

  build [
    [ '%{cmake}',
      '-DCMAKE_INSTALL_PREFIX=%{.install.prefix}',
      '-DCMAKE_INSTALL_LIBDIR=lib',
      '-DCMAKE_BUILD_TYPE=Release',
      '-DBUILD_TESTS=OFF',
      '-DBUILD_CLAR=OFF',
      '-DBUILD_EXAMPLES=OFF',
      '-DUSE_SSH=ON',
      '-DUSE_HTTPS=OpenSSL',
      '-DREGEX_BACKEND=builtin',
      '%{.install.extract}',
    ],
    '%{make}',
    '%{make} install',
  ];
};
