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

_realname=curl
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         $([[ ${CARCH} == i686 ]] || echo \
           "${MINGW_PACKAGE_PREFIX}-${_realname}-gnutls" \
           "${MINGW_PACKAGE_PREFIX}-${_realname}-winssl"))
pkgver=8.8.0
pkgrel=2
pkgdesc="Command line tool and library for transferring data with URLs (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://curl.se/"
msys2_repository_url="https://github.com/curl/curl"
msys2_changelog_url="https://curl.se/changes.html"
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")
_cert_depends=("${MINGW_PACKAGE_PREFIX}-ca-certificates"
               "${MINGW_PACKAGE_PREFIX}-libssh2")
_openssl_depends=("${MINGW_PACKAGE_PREFIX}-openssl"
                  "${MINGW_PACKAGE_PREFIX}-nghttp2"
                  $([[ ${CARCH} == i686 ]] || echo "${MINGW_PACKAGE_PREFIX}-nghttp3"))
_gnutls_depends=("${MINGW_PACKAGE_PREFIX}-rtmpdump"
                 "${MINGW_PACKAGE_PREFIX}-gnutls")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${_cert_depends[@]}"
             "${_openssl_depends[@]}"
             $([[ ${CARCH} == i686 ]] || echo "${_gnutls_depends[@]}"))
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-c-ares"
         "${MINGW_PACKAGE_PREFIX}-brotli"
         "${MINGW_PACKAGE_PREFIX}-libidn2"
         "${MINGW_PACKAGE_PREFIX}-libpsl"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
source=("https://github.com/curl/curl/releases/download/${_realname}-${pkgver//./_}/${_realname}-${pkgver}.tar.xz"{,.asc}
        "pathtools.c"
        "pathtools.h"
        "0001-Make-cURL-relocatable.patch"
        "0002-nghttp2-static.patch"
        "0004-more-static-fixes.patch")
sha256sums=('0f58bb95fc330c8a46eeb3df5701b0d90c9d9bfcc42bd1cd08791d12551d4400'
            'SKIP'
            'ebf471173f5ee9c4416c10a78760cea8afaf1a4a6e653977321e8547ce7bf3c0'
            '1585ef1b61cf53a2ca27049c11d49e0834683dfda798f03547761375df482a90'
            '3651bd543cc574556ccea185bc6b519284ed55792b65b37ebac99a8d85c4b2a0'
            '3ee9c75a3046f86f91290c143170179230c9adc6eabfbb79eb26f708a165b719'
            '590eb65e90e756eaad993d52a101f29091ada2c742c5a607684e88fc5c560d54')
validpgpkeys=('27EDEAF22F3ABCEB50DB9A125CC908FDB71E12C2')  # Daniel Stenberg

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  test ! -d "${startdir}/../mingw-w64-pathtools" || {
    cmp "${startdir}/../mingw-w64-pathtools/pathtools.c" "${srcdir}/pathtools.c" &&
    cmp "${startdir}/../mingw-w64-pathtools/pathtools.h" "${srcdir}/pathtools.h"
  } || exit 1

  cd "${srcdir}/${_realname}-${pkgver}"
  cp -fHv "${srcdir}"/pathtools.[ch] lib/

  apply_patch_with_msg \
    0001-Make-cURL-relocatable.patch \
    0002-nghttp2-static.patch \
    0004-more-static-fixes.patch

  autoreconf -vfi
}

do_build() {
  _variant=$1

  _destdir="${srcdir}/build-${MSYSTEM}"
  if [ "${_variant}" != "-openssl" ]; then
    _destdir="${_destdir}${_variant}"
  fi

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

  local -a _variant_config
  if [ "${_variant}" = "-winssl" ]; then
    _variant_config+=("--with-schannel")
    _variant_config+=('--without-nghttp2')
    _variant_config+=("--without-ca-bundle")
    _variant_config+=("--without-ca-path")
    _variant_config+=("--without-librtmp")
  elif [ "${_variant}" = "-gnutls" ]; then
    _variant_config+=("--with-default-ssl-backend=gnutls")
    _variant_config+=("--with-gnutls")
    _variant_config+=("--with-schannel")
    _variant_config+=('--without-nghttp2')
    _variant_config+=("--with-ca-bundle=${MINGW_PREFIX}/etc/ssl/certs/ca-bundle.crt")
    _variant_config+=("--with-librtmp")
  elif [ "${_variant}" = "-openssl" ]; then
    _variant_config+=("--with-default-ssl-backend=openssl")
    _variant_config+=("--with-openssl")
    _variant_config+=("--with-ca-bundle=${MINGW_PREFIX}/etc/ssl/certs/ca-bundle.crt")
    _variant_config+=("--with-nghttp2=${MINGW_PREFIX}")
    _variant_config+=("--without-librtmp")
    if [[ ${CARCH} != i686 ]]; then
      _variant_config+=("--with-openssl-quic" "--with-nghttp3=${MINGW_PREFIX}")
    fi
  fi

  msg2 "Building static library for curl${_variant}"
  mkdir -p "${_destdir}-static" && cd "${_destdir}-static"
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --disable-pthreads \
    --without-random \
    --enable-static \
    --disable-shared \
    --enable-sspi \
    --enable-ldap \
    --enable-ldaps \
    --with-brotli \
    --with-ldap-lib=wldap32 \
    --with-libssh2 \
    --with-zstd \
    "${_variant_config[@]}" \
    "${extra_config[@]}"
  # there's a bug with zsh completion generation script and Windows.
  # curl has to be specified with the file extension.
  sed -i "s|\/curl > \$\@|\/curl\$\{EXEEXT\} > \$\@|" scripts/Makefile
  make

  msg2 "Building shared library for curl${_variant}"
  mkdir -p "${_destdir}-shared" && cd "${_destdir}-shared"
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --disable-pthreads \
    --without-random \
    --disable-static \
    --enable-shared \
    --enable-sspi \
    --enable-ldap \
    --enable-ldaps \
    --with-brotli \
    --with-ldap-lib=wldap32 \
    --with-libssh2 \
    --with-zstd \
    "${_variant_config[@]}" \
    "${extra_config[@]}"
  # there's a bug with zsh completion generation script and Windows.
  # curl has to be specified with the file extension.
  sed -i "s|\/curl > \$\@|\/curl\$\{EXEEXT\} > \$\@|" scripts/Makefile
  make
}

build() {
  do_build -openssl
  if [[ ${CARCH} != i686 ]]; then
    do_build -winssl
    do_build -gnutls
  fi
}

do_package() {
  cd "${srcdir}/build-${MSYSTEM}$1-static"
  make DESTDIR="${pkgdir}" install

  cd "${srcdir}/build-${MSYSTEM}$1-shared"
  make DESTDIR="${pkgdir}" install

  local PREFIX_DEPS=$(cygpath -am ${MINGW_PREFIX})
  sed -s "s|${PREFIX_DEPS}|${MINGW_PREFIX}|g" -i ${pkgdir}${MINGW_PREFIX}/bin/curl-config
  sed -s "s|${PREFIX_DEPS}|${MINGW_PREFIX}|g" -i ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/libcurl.pc

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}

package_curl() {
  depends=("${depends[@]}"
           "${_cert_depends[@]}"
           "${_openssl_depends[@]}")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-winssl"
             "${MINGW_PACKAGE_PREFIX}-${_realname}-gnutls")
  do_package
}

package_curl-winssl() {
  depends=("${depends[@]}"
           "${MINGW_PACKAGE_PREFIX}-libssh2-wincng")
  provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}"
             "${MINGW_PACKAGE_PREFIX}-${_realname}-gnutls")
  do_package -winssl
}

package_curl-gnutls() {
  provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
  depends=("${depends[@]}"
           "${_cert_depends[@]}"
           "${_gnutls_depends[@]}")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}"
             "${MINGW_PACKAGE_PREFIX}-${_realname}-winssl")
  do_package -gnutls
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
