# Maintainer: Markus Rathgeb <maggu2810@gmail.com>

_realname=restbed
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.8
pkgrel=3
pkgdesc="Corvusoft's Restbed framework brings asynchronous RESTful functionality to C++14 applications (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/Corvusoft/restbed"
license=('spdx:AGPL-3.0-only')
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}-asio")
source=("https://github.com/Corvusoft/restbed/archive/${pkgver}/${_realname}-${pkgver}.tar.gz"
        "0001-fix-CMakeLists.txt.patch"
        "0002-fix-dll-import-export-instructions.patch"
        "0003-fix-asio-deprecated-symbols.patch")
sha256sums=('4801833f86a67b8a123c2c01203e259eb81157e1e9ef144a3b6395cb2d838a42'
            'a4f3bea758d595aad69a1e59417da02646509509ef78e7f92e87afe93e1226c5'
            '0f8f1f8564b0c82bd591bfea1de199e8be04cc7cfd57c01f049e319dcec72201'
            '59c50653879eaeaf946ae88a188fe075ecdf384f6bea3038c057c8753c76541f')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Np1 -i "${srcdir}/0001-fix-CMakeLists.txt.patch"
  patch -Np1 -i "${srcdir}/0002-fix-dll-import-export-instructions.patch"
  patch -Np1 -i "${srcdir}/0003-fix-asio-deprecated-symbols.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

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

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

check() {
  # Shared Check
  cd "${srcdir}/build-${MSYSTEM}"
  PATH=$PWD/src:$PATH ${MINGW_PREFIX}/bin/ctest.exe ./ || true
}

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

  # License
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/legal/LICENSE* -t "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/
}
