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

pkgbase=curl
pkgname=('curl' 'libcurl' 'libcurl-devel')
pkgver=7.78.0
pkgrel=1
pkgdesc="Multi-protocol file transfer utility"
arch=('i686' 'x86_64')
url="https://curl.haxx.se"
license=('MIT')
depends=('ca-certificates')
makedepends=('brotli-devel' 'heimdal-devel' 'libcrypt-devel' 'libidn2-devel'
             'libunistring-devel' 'libnghttp2-devel' 'libpsl-devel' 'libssh2-devel' 'openssl-devel' 'zlib-devel' 'libzstd-devel') #  'libcares-devel'
options=('!libtool' 'strip' '!debug')
source=("https://github.com/curl/curl/releases/download/${pkgbase}-${pkgver//./_}/${pkgbase}-${pkgver}.tar.xz"{,.asc}
        curl-7.55.1-msys2.patch
        curl-7.58.0-libpsl-static-libs.patch
        curl-7.60.0-gssapi-static-libs.patch
        0001-more-static-fixes.patch)
sha256sums=('be42766d5664a739c3974ee3dfbbcbe978a4ccb1fe628bb1d9b59ac79e445fb5'
            'SKIP'
            'e5ba067afcfd726403cf7e6cb2349ff3fe6fe093c2853a4196a8f57f9f56d8d6'
            'ad3d76013c2dd683c44ad4cdc5108ea5218056c87b66f6ed2a90502e785a39af'
            'd58a94556c031e550403ed13691305983bf83493f15fb8c35615e59bf265bbf7'
            '870a91d09480cd0d1dc74119376350fc430e5223e2d4b3c0606af17793ecc310')
validpgpkeys=('27EDEAF22F3ABCEB50DB9A125CC908FDB71E12C2'   # Daniel Stenberg
              '914C533DF9B2ADA2204F586D78E11C6B279D5C91')  # Daniel Stenberg (old key)

prepare() {
  cd "${pkgname}-${pkgver}"
  patch -p1 -i ${srcdir}/curl-7.58.0-libpsl-static-libs.patch
  patch -p1 -i ${srcdir}/curl-7.55.1-msys2.patch
  patch -p1 -i ${srcdir}/curl-7.60.0-gssapi-static-libs.patch

  # https://github.com/curl/curl/discussions/6324
  patch -p1 -i ${srcdir}/0001-more-static-fixes.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-symbol-hiding \
    --disable-ares \
    --with-brotli \
    --with-gssapi \
    --with-libidn2 \
    --without-librtmp \
    --with-libssh2 \
    --with-openssl \
    --with-zlib \
    --with-libpsl \
    --with-ca-bundle=/usr/ssl/certs/ca-bundle.crt \
    ac_cv_func_setmode=yes

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

package_curl() {
  depends=('ca-certificates' 'libcurl' 'libcrypt'
           'libunistring' 'libnghttp2' 'libpsl' 'openssl' 'zlib')

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

  # license
  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' 'libcrypt' 'libidn2'
          'libunistring' 'libnghttp2' 'libpsl' 'libssh2' 'openssl' 'zlib' 'libzstd') #'libcares'
  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' 'libcrypt-devel' 'libidn2-devel'
           'libunistring-devel' 'libnghttp2-devel' 'libpsl-devel' 'libssh2-devel' 'openssl-devel' 'zlib-devel') #'libcares-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/
}

