# 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.51.0
pkgrel=1
pkgdesc="Cross-platform asynchronous I/O (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://libuv.org/'
msys2_repository_url='https://github.com/libuv/libuv/'
msys2_documentation_url='https://docs.libuv.org/'
msys2_references=(
  'archlinux: libuv'
  "cpe: cpe:/a:libuv:libuv"
  "cpe: cpe:/a:libuv_project:libuv"
)
license=("spdx:MIT")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=(https://dist.libuv.org/dist/v${pkgver}/libuv-v${pkgver}.tar.gz{,.sign})
sha256sums=('5f0557b90b1106de71951a3c3931de5e0430d78da1d9a10287ebc7a3f78ef8eb'
            'SKIP')
# PGP key IDs are available from https://github.com/libuv/libuv/blob/v1.x/MAINTAINERS.md
validpgpkeys=(
  'CFBB9CA9A5BEAFD70E2B3C5A79A67C55A3679C8B' # Jameson Nash <vtjnash@gmail.com>
  '612F0EAD9401622379DF4402F28C3C8DA33C03BE' # Santiago Gimeno (sgimeno) <santiago.gimeno@gmail.com>
)

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

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  CFLAGS+=" -Wno-int-conversion"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
    "${_extra_config[@]}" \
    -DLIBUV_BUILD_TESTS=OFF \
    ../${_realname}-v${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

check(){
  cd "${srcdir}/build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/cmake -DLIBUV_BUILD_TESTS=ON ../${_realname}-v${pkgver}
  ${MINGW_PREFIX}/bin/cmake --build .
  # One these tests fails
  ${MINGW_PREFIX}/bin/ctest || true
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

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