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

_realname=embree
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.12.1
pkgrel=2
pkgdesc="High Performance Ray Tracing Kernels Intel Corporation (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://www.embree.org/"
license=("Apache 2.0")
depends=("${MINGW_PACKAGE_PREFIX}-intel-tbb"
         "${MINGW_PACKAGE_PREFIX}-glfw")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gcc")
options=('strip' 'buildflags' '!debug')
source=(${_realname}-${pkgver}.tar.gz::https://github.com/embree/embree/archive/v${pkgver}.tar.gz
        001-build-fixes.patch)
sha256sums=('0c9e760b06e178197dd29c9a54f08ff7b184b0487b5ba8b8be058e219e23336e'
            'f597aed33951601fae37932dfe0cf8df1dda0470513a190fdfa9f5c690fcabd1')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/001-build-fixes.patch
  
  mv kernels/embree.rc{,.orig}
  iconv -f UTF-16LE -t UTF-8 kernels/embree.rc.orig > kernels/embree.rc
}

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

  [[ -d "build-${MINGW_CHOST}" ]] && rm -rf "build-${MINGW_CHOST}"
  mkdir -p build-${MINGW_CHOST} && cd build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DEMBREE_STATIC_LIB=OFF \
    -DEMBREE_ISPC_SUPPORT=OFF \
    -DEMBREE_STACK_PROTECTOR=ON \
    ${extra_config} \
    ../${_realname}-${pkgver}

  make
}

package() {
  cd "${srcdir}"/build-${MINGW_CHOST}
  make DESTDIR=${pkgdir} install
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/LICENSE.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
  
  # Resolve conflict with imagemagick
  mv ${pkgdir}${MINGW_PREFIX}/bin/convert{,_embree}.exe
}
