# Maintainer: Alexey Pavlov <Alexpux@gmail.com>
# Contributor: Paul Moore <p.f.moore@gmail.com>

_realname=wget
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.20.3
pkgrel=2
pkgdesc='Wget retrieves files using HTTP, HTTPS and FTP (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url='https://www.gnu.org/software/wget/'
license=('GPL3')
depends=("${MINGW_PACKAGE_PREFIX}-pcre2"
         "${MINGW_PACKAGE_PREFIX}-libidn2"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-c-ares"
         "${MINGW_PACKAGE_PREFIX}-gpgme")
source=("https://ftp.gnu.org/gnu/${_realname}/${_realname}-${pkgver}.tar.lz"{,.sig}
        taskbar-progress.patch
        openssl-windows-cert-store.patch
        gettext-reconfigure.patch)
sha256sums=('69607ce8216c2d1126b7a872db594b3f21e511e660e07ca1f81be96650932abb'
            'SKIP'
            'd5b662a4ee3f85d1c7d2f6e221c7e45f19189925e6774102114045fbfb2953b7'
            '528369e9339eeeec7066632a1badc91a190decc2842810a6bd89a49c62565423'
            'd707a4e223433f6e79dd4f455c26dc162d26de79495d85dc4f2f92b877139325')
options=('strip' '!libtool' 'staticlibs')
validpgpkeys=('7845120B07CBD8D6ECE5FF2B2A1743EDA91A35B6')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  rm -f src/tbprogress.c src/tbprogress.h > /dev/null 2>&1 || true
  patch -p1 -i ${srcdir}/gettext-reconfigure.patch
  #Patch for support of taskbar-progress in windows cmd
  patch -p1 -i ${srcdir}/taskbar-progress.patch
  #Patch for supporting windows certificate store if openssl cacerts is not found
  patch -p1 -i ${srcdir}/openssl-windows-cert-store.patch
  autoreconf -fiv
}

build() {
  local -a configure_opts
  if [ "$MINGW_CHOST" == "x86_64-w64-mingw32" ]; then
    configure_opts+=("--with-cares")
  fi

  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}
  ../${_realname}-${pkgver}/configure --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --with-ssl=openssl \
    --with-metalink \
    --with-libidn=${MINGW_PREFIX} \
    --disable-xattr \
    "${configure_opts[@]}" \
    LIBS="-lole32"
  make GETTEXT_MACRO_VERSION="0.19"
}

check() {
  cd ${srcdir}/build-${MINGW_CHOST}
  #make check
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}
  make DESTDIR="${pkgdir}" GETTEXT_MACRO_VERSION="0.19" 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"
}
