# Maintainer: Swarnava Ghosh <swarnavaghosh04@gmail.com>
# Credits: Maximilian Schiller <manimax3@outlook.de> [https://aur.archlinux.org/cgit/aur.git/tree/PKGBUILD?h=krpc-cpp]

_realname=krpc-cpp
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=0.5.4
pkgrel=5
pkgdesc="KRPC Client for C++ (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/krpc/krpc"
license=('spdx:LGPL-3.0-only AND GPL-3.0-or-later AND MIT')
depends=(
  "${MINGW_PACKAGE_PREFIX}-asio"
  "${MINGW_PACKAGE_PREFIX}-protobuf")
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/krpc/krpc/releases/download/v${pkgver}/krpc-cpp-${pkgver}.zip"
        '001-krpc-cpp-cmake.patch'
        '002-krpc-cpp-gpb.patch'
        '003-update-to-io-context.patch'
        )
sha256sums=('50b9142569644219a4dbaac1d285c3ecdd583eb6aa5e52247d921a046413408c'
            '77e6028756bcfd06aa7ca6265327f9cf43ccade1ebcc657e826fea85d12aa0fd'
            'cc0d5a46d68ef83604888b40a92adc01c7d5c985f1733e62bd5a4dcbf824b32b'
            'a93960b5df541d3bb6cac1234fde028973d03edb674699dde2793cfa169def0e')

prepare() {
  cd "${_realname}-${pkgver}"

  patch -Np1 -i "${srcdir}"/001-$_realname-cmake.patch
  patch -Np1 -i "${srcdir}"/002-$_realname-gpb.patch
  patch -Np1 -i "${srcdir}"/003-update-to-io-context.patch
}

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

  CXXFLAGS+=" -DPROTOBUF_USE_DLLS" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe \
      -GNinja \
      -DCMAKE_POLICY_VERSION_MINIMUM=3.5 \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${_extra_config[@]}" \
      -DCMAKE_CXX_STANDARD=17 \
      ../${_realname}-${pkgver}

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

package() {
  cd "${srcdir}"/build-${MSYSTEM}

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING.LESSER" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.LESSER"
}
