# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Martell Malone <martellmalone@gmail.com>

_realname=libuv
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.40.0
pkgrel=3
pkgdesc="Cross-platform asynchronous I/O (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://github.com/libuv/libuv"
license=("custom")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
options=('staticlibs' 'strip')
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/libuv/libuv/archive/v${pkgver}.tar.gz"
        "libuv-win-atomics-non-x86.patch")
sha256sums=('70fe1c9ba4f2c509e8166c0ca2351000237da573bb6c82092339207a9715ba6b'
            'cfdc72694fb8578499eece9cf51102b8cc140d2ab07cd77c1a371b950d2add95')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Np1 -i ../libuv-win-atomics-non-x86.patch
 ./autogen.sh
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p "${srcdir}/build-${MINGW_CHOST}" && cd "${srcdir}/build-${MINGW_CHOST}"

  export lt_cv_deplibs_check_method='pass_all'

  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST}

  make
}

check(){
  cd "${srcdir}/build-${MINGW_CHOST}"
  # One these tests fails
  make check || true
}

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

  install -D -m644 "${srcdir}"/${_realname}-${pkgver}/LICENSE "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}/LICENSE
}
