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

_realname=openimagedenoise
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.1.0
pkgrel=2
pkgdesc="Intel Open Image Denoise library of high-performance, high-quality denoising filters for images rendered with ray tracing (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64')
url="https://openimagedenoise.github.io/"
license=("Apache 2.0")
depends=("${MINGW_PACKAGE_PREFIX}-intel-tbb")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-python"
             "git")
options=('strip' 'buildflags' '!debug')
source=(#${_realname}-${pkgver}.tar.gz::https://github.com/OpenImageDenoise/oidn/archive/v${pkgver}.tar.gz
        ${_realname}-${pkgver}.tar.gz::https://github.com/OpenImageDenoise/oidn/releases/download/v${pkgver}/oidn-${pkgver}.src.tar.gz
        001-build-fixes.patch)
sha256sums=('4dd484abea8a0b3d12d346343fcb1ab7abef8f94318d8c537f69a20c2a75c4eb'
            'e789c3ef6be360ec42f6b0da805bc424f0e021b093091361d2be53a77946724d')

prepare() {
  cd ${srcdir}/oidn-${pkgver}
  patch -p1 -i ${srcdir}/001-build-fixes.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

  [[ -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} \
    ${extra_config} \
    ../oidn-${pkgver}

  make
}

package() {
  cd "${srcdir}"/build-${MINGW_CHOST}
  make DESTDIR=${pkgdir} install
  install -Dm644 ${srcdir}/oidn-${pkgver}/LICENSE.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.txt"
}
