# Maintainer: Philippe Renon <philippe.renon@yahoo.fr>

_realname=vrpn
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=7.35
_pkgver=0${pkgver}
pkgrel=2
pkgdesc="The Virtual-Reality Peripheral Network (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/vrpn/vrpn/wiki"
license=('spdx:BSL-1.0')
depends=("${MINGW_PACKAGE_PREFIX}-hidapi"
         "${MINGW_PACKAGE_PREFIX}-jsoncpp"
         "${MINGW_PACKAGE_PREFIX}-libusb"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-freeglut"
         "${MINGW_PACKAGE_PREFIX}-swig")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/${_realname}/${_realname}/archive/v${_pkgver}/${_realname}-${pkgver}.tar.gz"
        "0002-fix-shared-build.patch"
        "0003-fix-includes.patch")
sha256sums=('83139846247e6a0530b974e03744bb358d11f88109f231a3031815924374bb9d'
            '14855180b0f6612b548ccf56a4b66de3fb1ef69b4b6418ab76e39fd643e9e609'
            'a2c08e4d18213aaacfa32894c284015c42dd72af609611c8c4cf36048ed26447')

prepare() {
  cd "${srcdir}/${_realname}-${_pkgver}"

  patch -p1 -i "${srcdir}"/0002-fix-shared-build.patch
  patch -p1 -i "${srcdir}"/0003-fix-includes.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

  _pyver=$(${MINGW_PREFIX}/bin/python -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake -Wno-dev \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DDIRECTX_ROOT_DIR=${MINGW_PREFIX} \
    -DWINHID_ROOT_DIR=${MINGW_PREFIX} \
    -DVRPN_USE_SHARED_LIBRARY=ON \
    -DVRPN_USE_WINSOCK2=ON \
    -DVRPN_BUILD_JAVA=OFF \
    -DVRPN_USE_MPI=OFF \
    -DPYTHON_LIBRARY=${MINGW_PREFIX}/bin/libpython${_pyver}.dll \
    -DPYTHON_INCLUDE_DIR=${MINGW_PREFIX}/include/python${_pyver} \
    "${extra_config[@]}" \
    ../${_realname}-${_pkgver}

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

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

  install -Dm644 "${srcdir}"/${_realname}-${_pkgver}/README.Legal "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}/LICENSE
}
