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

_realname=vrpn
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=7.34
_pkgver=0${pkgver}
pkgrel=7
pkgdesc="The Virtual-Reality Peripheral Network (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://github.com/vrpn/vrpn/wiki"
license=("BSL1.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}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gcc")
options=('staticlibs' 'strip')
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/${_realname}/${_realname}/archive/v${_pkgver}.tar.gz"
        "0002-fix-shared-build.patch")
sha256sums=('45024722110afe4b0663b12b68ea46864e79197ea752bba90c599f87e4d51dbe'
            '21244cdc98bd23b6faf5ada5219b848bc79efe30d019af5a63e37bcb203ff687')

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

  patch -p1 -i ${srcdir}/0002-fix-shared-build.patch
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir ${srcdir}/build-${MINGW_CHOST}
  cd ${srcdir}/build-${MINGW_CHOST}

  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 \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DDIRECTX_ROOT_DIR=${MINGW_PREFIX}/${MINGW_CHOST} \
    -DWINHID_ROOT_DIR=${MINGW_PREFIX}/${MINGW_CHOST} \
    -DVRPN_USE_SHARED_LIBRARY=ON \
    -DVRPN_USE_WINSOCK2=ON \
    "${extra_config[@]}" \
    ../${_realname}-${_pkgver}

  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install

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