# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgbase=curl
pkgname=('curl' 'libcurl' 'libcurl-devel')
pkgver=8.16.0
pkgrel=1
pkgdesc="Multi-protocol file transfer utility"
arch=('i686' 'x86_64')
url="https://curl.se/"
msys2_repository_url="https://github.com/curl/curl"
msys2_references=(
  "cpe: cpe:/a:curl:curl"
  "cpe: cpe:/a:curl:libcurl"
  "cpe: cpe:/a:haxx:curl"
  "cpe: cpe:/a:haxx:libcurl"
  "cpe: cpe:/a:libcurl:libcurl"
)
license=('spdx:MIT')
depends=('ca-certificates')
makedepends=('brotli-devel' 'heimdal-devel' 'libidn2-devel' 'autotools' 'libnghttp3-devel' 'libngtcp2-devel'
             'libunistring-devel' 'libnghttp2-devel' 'libpsl-devel' 'libssh2-devel' 'openssl-devel' 'zlib-devel' 'libzstd-devel' 'gcc')
source=("https://github.com/curl/curl/releases/download/${pkgbase}-${pkgver//./_}/${pkgbase}-${pkgver}.tar.xz"{,.asc}
        curl-7.60.0-gssapi-static-libs.patch
        0001-wcurl-force-cygwin-curl.patch)
sha256sums=('40c8cddbcb6cc6251c03dea423a472a6cea4037be654ba5cf5dec6eb2d22ff1d'
            'SKIP'
            'fbe5f247669ddd20506080f93b6e41d3b8c5b53082edcd09159f174d53203450'
            '2dc51428fac6c2332682e61f5c6b30be0f22217f66ea6e272842abe0559278a9')
validpgpkeys=('27EDEAF22F3ABCEB50DB9A125CC908FDB71E12C2')   # Daniel Stenberg

prepare() {
  cd "${pkgname}-${pkgver}"
  patch -p1 -i ${srcdir}/curl-7.60.0-gssapi-static-libs.patch
  patch -p1 -i ${srcdir}/0001-wcurl-force-cygwin-curl.patch

  autoreconf -fiv
}

build() {
  cd "${pkgname}-${pkgver}"

  declare -a extra_config
  if check_option "debug" "y"; then
    extra_config+=("--enable-debug")
  fi

  ./configure \
    --build=${CHOST} \
    --prefix=/usr \
    --enable-shared \
    --enable-static \
    "${extra_config[@]}" \
    --enable-optimize \
    --enable-ipv6 \
    --disable-ares \
    --with-brotli \
    --with-gssapi \
    --with-libidn2 \
    --without-librtmp \
    --with-libssh2 \
    --with-openssl \
    --with-nghttp3 \
    --with-ngtcp2 \
    --with-zlib \
    --with-libpsl \
    --with-ca-bundle=/usr/ssl/certs/ca-bundle.crt

  make
  make DESTDIR="${srcdir}/dest" install
}

package_curl() {
  depends=('ca-certificates' 'libcurl' 'libnghttp3' 'libngtcp2'
           'libunistring' 'libnghttp2' 'libpsl' 'openssl' 'zlib')
  replaces=('wcurl')
  provides=('wcurl')
  conflicts=('wcurl')

  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/*.exe ${pkgdir}/usr/bin/
  cp ${srcdir}/dest/usr/bin/wcurl ${pkgdir}/usr/bin/
  cp -rf ${srcdir}/dest/usr/share ${pkgdir}/usr/

  install -Dm644 ${srcdir}/${pkgname}-${pkgver}/COPYING "${pkgdir}/usr/share/licenses/${pkgname}/COPYING"
}

package_libcurl() {
  pkgdesc="Multi-protocol file transfer library (runtime)"
  depends=('brotli' 'ca-certificates' 'heimdal-libs' 'libidn2' 'libnghttp3' 'libngtcp2'
          'libunistring' 'libnghttp2' 'libpsl' 'libssh2' 'openssl' 'zlib' 'libzstd')
  groups=('libraries')

  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/*.dll ${pkgdir}/usr/bin/
}

package_libcurl-devel() {
  pkgdesc="Libcurl headers and libraries"
  depends=("libcurl=${pkgver}" 'brotli-devel' 'heimdal-devel' 'libidn2-devel' 'libnghttp3-devel' 'libngtcp2-devel'
           'libunistring-devel' 'libnghttp2-devel' 'libpsl-devel' 'libssh2-devel' 'openssl-devel' 'zlib-devel')
  options=('staticlibs')
  groups=('development')

  mkdir -p ${pkgdir}/usr/bin
  cp -f ${srcdir}/dest/usr/bin/*-config ${pkgdir}/usr/bin/
  cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/lib ${pkgdir}/usr/
}

