# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Andrew Sun <adsun701@gmail.com>

_realname=c-ares
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.17.1
pkgrel=1
pkgdesc="C library that performs DNS requests and name resolves asynchronously (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://c-ares.haxx.se/"
license=("MIT, BSD")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-cmake")
options=('strip' 'staticlibs')
source=(#"https://c-ares.haxx.se/download/${_realname}-${pkgver}.tar.gz"{,.asc}
        https://github.com/c-ares/c-ares/archive/cares-${pkgver//./_}.tar.gz
        0002-Add-static-pkg-config.patch
        0003-fix-cmake-targets.patch)
validpgpkeys=('914C533DF9B2ADA2204F586D78E11C6B279D5C91'
              '27EDEAF22F3ABCEB50DB9A125CC908FDB71E12C2')
sha256sums=('61f7cf09605f5e38d4828f82d0e2ddb9de8e355ecfd6819b740691c644583b8f'
            '787ce206f7956ecde7a5b61ae49701d6396d9869e05e5db335801f38b2af2ad9'
            'ce5810b35ea0f1cfbd5b5c8b5eea96f736d89750d0a68b9da413f4dea16119d8')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  cd "${srcdir}"/${_realname}-cares-${pkgver//./_}
  del_file_exists libcares-static.pc.cmake
  apply_patch_with_msg \
    0002-Add-static-pkg-config.patch \
    0003-fix-cmake-targets.patch
}

build() {
  msg2 "Build shared version ..."
  [[ -d "${srcdir}"/build-shared-${CARCH} ]] && rm -rf "${srcdir}"/build-shared-${CARCH}
  mkdir -p "${srcdir}"/build-shared-${CARCH} && cd "${srcdir}"/build-shared-${CARCH}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
      -Wno-dev \
      -G"MSYS Makefiles" \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DCMAKE_BUILD_TYPE="Release" \
      -DCARES_STATIC=OFF \
      -DCARES_SHARED=ON \
      ../${_realname}-cares-${pkgver//./_}

  make

    msg2 "Build static version ..."
  [[ -d "${srcdir}"/build-static-${CARCH} ]] && rm -rf "${srcdir}"/build-static-${CARCH}
  mkdir -p "${srcdir}"/build-static-${CARCH} && cd "${srcdir}"/build-static-${CARCH}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
      -Wno-dev \
      -G"MSYS Makefiles" \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DCMAKE_BUILD_TYPE="Release" \
      -DCARES_STATIC=ON \
      -DCARES_SHARED=OFF \
      ../${_realname}-cares-${pkgver//./_}

  make
}

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

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

  # msys2-runtime-devel provide nameser.h but mingw-w64 don't have it
  cp ${srcdir}/${_realname}-cares-${pkgver//./_}/src/lib/nameser.h ${pkgdir}${MINGW_PREFIX}/include
}
