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

_realname=openvr
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.5.1
pkgrel=1
pkgdesc="API and SDK that allows access to VR hardware from multiple vendors (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/ValveSoftware/openvr/"
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python")
source=(https://github.com/ValveSoftware/openvr/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz
        002-install-headers.patch
        003-mingw-library-name.patch
        004-install-location.patch
        cxx_header_generator.py)
sha256sums=('54f654fce001682d8ac608f544a6c41e03a672b005c1deca3579fa36480a537c'
            '10ece7ce59e78747c24ac98bb891d65ab6e57a888094a1fad5e87b56dff5ae10'
            '103ec4394f2fef820276906397011a498396694999e492c14ce0fda19f6d1014'
            '3fe7b4420a52ecf4233558ef5dbcc56f2abf35e204c90fbcefd13e0127a3938f'
            '739d0f4eeb8d83d2236d2174fbfac209ea20009bcaabba455c9b31301c14acfe')
noextract=(${_realname}-${pkgver}.tar.gz)

# 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() {
  [[ -d "${srcdir}"/${_realname}-${pkgver} ]] && rm -rf "${srcdir}"/${_realname}-${pkgver}
  tar -xzf "${srcdir}"/${_realname}-${pkgver}.tar.gz -C "${srcdir}" || true

  cd "${srcdir}"/${_realname}-${pkgver}
  apply_patch_with_msg \
    002-install-headers.patch \
    003-mingw-library-name.patch \
    004-install-location.patch
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DBUILD_SHARED=ON \
    ${extra_config[@]} \
    ../${_realname}-${pkgver}

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

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DBUILD_SHARED=OFF \
    ${extra_config[@]} \
    ../openvr-${pkgver}

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

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

  cd "${srcdir}"/build-${MSYSTEM}-shared
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --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"
}
