# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=wget2
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.2.0
pkgrel=2
pkgdesc='Updated version of popular wget URL retrieval tool (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://www.gnu.org/software/wget/'
msys2_repository_url="https://gitlab.com/gnuwget/wget2"
msys2_references=(
  "cpe: cpe:/a:gnu:wget"
)
license=('spdx:GPL-3.0-or-later AND LGPL-3.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-brotli"
         "${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-gettext-runtime"
         "${MINGW_PACKAGE_PREFIX}-gpgme"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-libidn2"
         "${MINGW_PACKAGE_PREFIX}-libpsl"
         "${MINGW_PACKAGE_PREFIX}-nghttp2"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-pcre2"
         "${MINGW_PACKAGE_PREFIX}-xz"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-gettext-tools"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-doxygen")
checkdepends=("${MINGW_PACKAGE_PREFIX}-libmicrohttpd")
source=("https://ftp.gnu.org/gnu/wget/${_realname}-${pkgver}.tar.lz"{,.sig}
        "mingw-dllexport.patch"
        "002-fix-build-with-gettext-0.25.patch"
        "003-fix-build-with-clang.patch")
sha256sums=('ffa5e49db90c9ddc0c830b66e473630c679b1b0a26a53d24981d4f0efa1c90b6'
            'SKIP'
            'e6cb1e6088a98bc7da3cef9d6e97f11700bda564d0c8d21fa04f915518b0ed2d'
            'eea85a7e561a760b43a5750a67d73400955e0ce790faa3379d6d000deb65e4c2'
            '01ce53c26852c90b89cecef16be0d55fd2b13b2a784b3f1668996086372a049c')
validpgpkeys=('1CB27DBC98614B2D5841646D08302DB6A2670428') # Tim Rühsen <tim.ruehsen@gmx.de>

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Nbp1 -i "${srcdir}/mingw-dllexport.patch"
  patch -Nbp1 -i "${srcdir}/002-fix-build-with-gettext-0.25.patch"
  patch -Nbp1 -i "${srcdir}/003-fix-build-with-clang.patch"
  autoreconf -fiv
}

build() {
  # Weak symbols are broken with GCC + binutils
  # https://github.com/msys2/MINGW-packages/issues/11743
  export gl_cv_have_weak=no

  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --with-ssl=openssl \
    --with-bzip2 \
    --with-lzma

  make
}

check() {
  cd "${srcdir}"/build-${MSYSTEM}
  make check
}

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

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