# Maintainer: taozuhong <taozuhong@gmail.com>

_realname=hiredis
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.3.0
pkgrel=1
pkgdesc="Minimalistic C client for Redis (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/redis/hiredis"
msys2_references=(
  "cpe: cpe:/a:redis:hiredis"
  "cpe: cpe:/a:redislabs:hiredis"
)
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-openssl")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf")
source=("https://github.com/redis/hiredis/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "0001-fix-clock_gettime.patch")
sha256sums=('25cee4500f359cf5cad3b51ed62059aadfc0939b05150c1f19c7e2829123631c'
            'b1c5b2b99367bcaab3c7a6086003a6b21a4b8116678ffca64db87f3e74f35ea2')

prepare() {
  cd "${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}"/0001-fix-clock_gettime.patch
}

build() {
  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"

  mkdir -p "${srcdir}/static-${MSYSTEM}" && cd "${srcdir}/static-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake \
      -G"Ninja" \
      ${_extra_config[@]} \
      -DBUILD_SHARED_LIBS=OFF \
      -DCMAKE_INSTALL_PREFIX=$MINGW_PREFIX \
      -DENABLE_SSL=ON \
      -DDISABLE_TESTS=ON \
      ../"${_realname}-${pkgver}"

  cmake --build .

  mkdir -p "${srcdir}/shared-${MSYSTEM}" && cd "${srcdir}/shared-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake \
      -G"Ninja" \
      ${_extra_config[@]} \
      -DCMAKE_INSTALL_PREFIX=$MINGW_PREFIX \
      -DENABLE_SSL=ON \
      -DDISABLE_TESTS=ON \
      ../"${_realname}-${pkgver}"

  cmake --build .
}

package() {
  cd "${srcdir}/static-${MSYSTEM}"
  DESTDIR="${pkgdir}" cmake --install .
  cd "${srcdir}/shared-${MSYSTEM}"
  DESTDIR="${pkgdir}" cmake --install .

  # Remove NuGet package
  rm -rf "${pkgdir}"${MINGW_PREFIX}/build
}
