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

_realname=libicsneo
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
conflicts=(${MINGW_PACKAGE_PREFIX}-${_realname}-git)
pkgver=1.1.0
pkgrel=1
pkgdesc="The Intrepid Control Systems Open Cross-Platform Device Communication API (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
license=("custom")
url="https://github.com/intrepidcs/libicsneo"
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-pybind11"
             "${MINGW_PACKAGE_PREFIX}-python-sphinx")
optdepends=("${MINGW_PACKAGE_PREFIX}-python: bindings support")
source=(${_realname}-${pkgver}.tar.gz::https://github.com/intrepidcs/libicsneo/archive/refs/tags/${pkgver}.tar.gz
        001-mingw-build.patch
        002-install-targets.patch
        003-python-bindings.patch)
sha256sums=('68308a81f278fc94e5b3db1e2722ac285d058dd325ad8ee89c8d9c187e5d1e8c'
            '9bdb34d464b9c680cabc7130f5f904fc4fb26639fb1ecd61584a613e7c31a173'
            '9dec215bc7e39ae24843428d9b21f4d0f76d39cfb1021bd29f332530cd7a89b6'
            'd5efc92c8b1cf41e74ff9d285c75263424cf323e65b2806cb0141f208368ead6')

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

prepare() {
  cd ${_realname}-${pkgver}
  apply_patch_with_msg \
    001-mingw-build.patch \
    002-install-targets.patch \
    003-python-bindings.patch
}

build() {
  mkdir -p build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G "Ninja" \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DLIBICSNEO_BUILD_DOCS=ON \
    -DLIBICSNEO_ENABLE_TCP=ON \
    -DLIBICSNEO_ENABLE_BINDINGS_PYTHON=ON \
    ../${_realname}-${pkgver}

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

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

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