# 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=0.2.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}-python-sphinx")
source=(https://github.com/intrepidcs/libicsneo/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz
        001-mingw-build.patch
        002-install-targets.patch)
sha256sums=('19f90436e050ba1dfa025f02470a9ffda2b7c117ffe46f01e19a4f139451099d'
            'fe44449cdd96d59a45d8a777acf0e2d0a99ca6b377de1f508165a5822e6f3551'
            '9dec215bc7e39ae24843428d9b21f4d0f76d39cfb1021bd29f332530cd7a89b6')

# 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
}

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} \
    ../${_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"
}
