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

_realname=openimageio
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.2.10.0
pkgrel=3
pkgdesc="A library for reading and writing images, including classes, utilities, and applications (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://www.openimageio.org/"
license=("custom")
depends=("${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-field3d"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-fmt"
         "${MINGW_PACKAGE_PREFIX}-jasper"
         "${MINGW_PACKAGE_PREFIX}-giflib"
         "${MINGW_PACKAGE_PREFIX}-glew"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-libheif"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libraw"
         "${MINGW_PACKAGE_PREFIX}-libsquish"
         "${MINGW_PACKAGE_PREFIX}-ffmpeg"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-opencolorio"
         "${MINGW_PACKAGE_PREFIX}-opencv"
         "${MINGW_PACKAGE_PREFIX}-openexr"
         "${MINGW_PACKAGE_PREFIX}-openjpeg"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-openvdb"
         "${MINGW_PACKAGE_PREFIX}-ptex"
         "${MINGW_PACKAGE_PREFIX}-pugixml"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             #"${MINGW_PACKAGE_PREFIX}-qt5"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-pybind11"
             "git")
optdepends=(#"${MINGW_PACKAGE_PREFIX}-qt5: iv image viewer"
            "${MINGW_PACKAGE_PREFIX}-python: bindings support")
options=('strip' 'buildflags' '!debug')
source=(${_realname}-${pkgver}.tar.gz::https://github.com/OpenImageIO/oiio/archive/Release-${pkgver}.tar.gz
        git+https://github.com/OpenImageIO/oiio-images.git
        0000-fix-mingw-w64.patch
        0000-workaround-ansidecl-h-PTR-define-conflict.patch
        0008-find-openjpeg.patch
        0012-maybe-uninitialized-errors.patch
        0015-python-module-ext.patch
        0017-cmake-compiler-remove-Werror.patch
        0018-link-hdf5.patch)
sha256sums=('dbc0e3e9718497d9f71ea01fb1de8b87449775ad9dbcea4d2538d9c52bbe1d5a'
            'SKIP'
            '25c71ed54150546dbed782e705d0e114ec080873f624e521bc72d263b592a1dd'
            '9e4e21333676268a91c0f4e7676aeab7658e5f748e7e5cfe43a92f0fd7931229'
            '3fa875a999044c5ba758e91df2f213be1ca217307fc9b0085902efe77ca286a0'
            'bc37ea8326f7f5f26068051b537f2491e9554a380e6584bf55aaad111d33eb81'
            'e943eaf234af935429211c14ad89d5a2a77d84ab8bd3327279d6197ba1f4b0ed'
            '5a124550e06ce928b928f7fef52ba86cf87ed4ade1cc5848288fad620e84c9ec'
            '066de45a500ae4eaa5fbdd99bb6ea7904ddc88389ad1d726d629edf5d9527b26')

prepare() {
  cd ${srcdir}/oiio-Release-${pkgver}
  patch -p1 -i ${srcdir}/0000-fix-mingw-w64.patch
  patch -p1 -i ${srcdir}/0000-workaround-ansidecl-h-PTR-define-conflict.patch
  patch -p1 -i ${srcdir}/0008-find-openjpeg.patch
  patch -p1 -i ${srcdir}/0012-maybe-uninitialized-errors.patch
  patch -p1 -i ${srcdir}/0015-python-module-ext.patch
  patch -p1 -i ${srcdir}/0017-cmake-compiler-remove-Werror.patch
  patch -p1 -i ${srcdir}/0018-link-hdf5.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}

  local _pyver=$(${MINGW_PREFIX}/bin/python -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DUSE_FREETYPE=ON \
    -DUSE_OPENCV=ON \
    -DUSE_FIELD3D=ON \
    -DUSE_OPENGL=ON \
    -DUSE_QT=OFF \
    -DUSE_FFMPEG=ON \
    -DPYTHON_VERSION=${_pyver} \
    -DUSE_EXTERNAL_PUGIXML=ON \
    -DOIIO_BUILD_TESTS=OFF \
    -DSTOP_ON_WARNING=OFF \
    ${extra_config} \
    ../oiio-Release-${pkgver}

  make
}

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

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/OpenImageIO/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
    sed -e "s|${MINGW_PREFIX}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done

  install -Dm644 ${srcdir}/oiio-Release-${pkgver}/LICENSE.md "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.md"
}
