# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgbase="c-ares"
pkgname=("libcares" "libcares-devel")
pkgver=1.17.2
pkgrel=1
pkgdesc="C library that performs DNS requests and name resolves asynchronously (libraries)"
arch=('i686' 'x86_64')
url="https://c-ares.haxx.se/"
license=("MIT")
makedepends=("cmake")
depends=("gcc-libs")
options=('staticlibs')
source=("https://github.com/c-ares/c-ares/releases/download/cares-${pkgver//./_}/c-ares-${pkgver}.tar.gz"{,.asc}
        "0001-cmake-check-win32-winsock.patch")
sha256sums=('4803c844ce20ce510ef0eb83f8ea41fa24ecaae9d280c468c582d2bb25b3913d'
            'SKIP'
            'd581371cc2e371e215a0442d3ac0711b93a5a549a9521055461c9d4d0777e8e5')
validpgpkeys=('27EDEAF22F3ABCEB50DB9A125CC908FDB71E12C2') # Daniel Stenberg <daniel@haxx.se>

prepare() {
  cd "${srcdir}/${pkgbase}-${pkgver}"

  # https://github.com/c-ares/c-ares/pull/422.patch
  patch -p1 -i "${srcdir}/0001-cmake-check-win32-winsock.patch"
}

build() {
  cd "${srcdir}/${pkgbase}-${pkgver}"

  cmake -B "build-static" \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCARES_STATIC=ON \
    -DCARES_SHARED=OFF
  cmake --build "build-static"
  DESTDIR="${srcdir}/dest" cmake --install "build-static"

  cmake -B "build-shared" \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCARES_STATIC=OFF \
    -DCARES_SHARED=ON
  cmake --build "build-shared"
  DESTDIR="${srcdir}/dest" cmake --install "build-shared"
}

package_libcares() {
  groups=('libraries')

  mkdir -p ${pkgdir}/usr/bin
  cp -rf ${srcdir}/dest/usr/bin ${pkgdir}/usr/

  mkdir -p ${pkgdir}/usr/share
  cp -rf ${srcdir}/dest/usr/share ${pkgdir}/usr/

  install -vDm 644 "${srcdir}/${pkgbase}-${pkgver}/LICENSE.md" -t "${pkgdir}/usr/share/licenses/${pkgbase}/"
}

package_libcares-devel() {
  pkgdesc="c-ares headers and libraries (development)"
  groups=('development')
  depends=("libcares=${pkgver}")

  mkdir -p ${pkgdir}/usr
  cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/lib ${pkgdir}/usr/
}
