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

_realname=openvr
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.14.15
pkgrel=2
pkgdesc="API and SDK that allows access to VR hardware from multiple vendors (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://github.com/ValveSoftware/openvr/"
license=("BSD3")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-python")
source=(${_realname}-${pkgver}.tar.gz::https://github.com/ValveSoftware/openvr/archive/v${pkgver}.tar.gz
        001-_WIN32-macros.patch
        002-install-headers.patch
        003-mingw-library-name.patch
        004-install-location.patch
        cxx_header_generator.py)
sha256sums=('40644e569f057e86c733d818a22d821c640307e5058d95958414422134d79584'
            '51c697a176847aaead97f185422f844091b4d4257b84074ec1c6c8a466c74283'
            '10ece7ce59e78747c24ac98bb891d65ab6e57a888094a1fad5e87b56dff5ae10'
            '103ec4394f2fef820276906397011a498396694999e492c14ce0fda19f6d1014'
            '3fe7b4420a52ecf4233558ef5dbcc56f2abf35e204c90fbcefd13e0127a3938f'
            '739d0f4eeb8d83d2236d2174fbfac209ea20009bcaabba455c9b31301c14acfe')
noextract=(${_realname}-${pkgver}.tar.gz)

prepare() {
  [[ -d ${srcdir}/openvr-${pkgver} ]] && rm -rf ${srcdir}/openvr-${pkgver}
  tar -xzf ${srcdir}/${_realname}-${pkgver}.tar.gz -C ${srcdir} || true
  
  cd openvr-${pkgver}
  patch -p1 -i ${srcdir}/001-_WIN32-macros.patch
  patch -p1 -i ${srcdir}/002-install-headers.patch
  patch -p1 -i ${srcdir}/003-mingw-library-name.patch
  patch -p1 -i ${srcdir}/004-install-location.patch
}

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DBUILD_SHARED=ON \
    ../openvr-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build . #VERBOSE=1
  
  [[ -d ${srcdir}/build-${MINGW_CHOST}-static ]] && rm -r ${srcdir}/build-${MINGW_CHOST}-static
  mkdir ${srcdir}/build-${MINGW_CHOST}-static && cd ${srcdir}/build-${MINGW_CHOST}-static

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DBUILD_SHARED=OFF \
    ../openvr-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build . #VERBOSE=1
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}-static
  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/cmake --build . --target install
  
  cd ${srcdir}/build-${MINGW_CHOST}-shared
  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/cmake --build . --target install

  ${MINGW_PREFIX}/bin/python ${srcdir}/cxx_header_generator.py -d "${pkgdir}${MINGW_PREFIX}/include/openvr"
  cp ${pkgdir}${MINGW_PREFIX}/include/openvr/openvr_mingw.hpp ${pkgdir}${MINGW_PREFIX}/include/openvr_mingw.hpp
  install -Dm644 "${srcdir}/openvr-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
