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

_realname=opencolorio
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
pkgver=1.1.1
pkgrel=12
pkgdesc="A color management framework for visual effects and animation (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://opencolorio.org/"
license=('BSD')
depends=("${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-glew"
         "${MINGW_PACKAGE_PREFIX}-lcms2"
         "${MINGW_PACKAGE_PREFIX}-openexr"
         "${MINGW_PACKAGE_PREFIX}-ptex"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-tinyxml"
         "${MINGW_PACKAGE_PREFIX}-yaml-cpp")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-pystring"
             $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || \
             echo "${MINGW_PACKAGE_PREFIX}-openimageio")) # Remove this workaround after available in repo
options=('strip' '!debug')
source=(${_realname}-${pkgver}.tar.gz::https://github.com/imageworks/OpenColorIO/archive/v${pkgver}.tar.gz
        001-fix-typo-in-formatregistry.patch
        002-mingw-w64.patch
        003-fix-redefinitions.patch
        004-msvc-flags.patch
        005-declspec-typo.patch
        006-mingw-no-dupenv_s.patch
        007-detect-mingw-python.patch
        008-export-attributes.patch
        009-python-module-extension.patch)
sha256sums=('c9b5b9def907e1dafb29e37336b702fff22cc6306d445a13b1621b8a754c14c8'
            '22a8df1dc67ce9da35f2795b4be495ace59620416bc3767fb7dbcbc8b1f2a9e8'
            'fa8c45602714eb70c7bbde05a37edf3b1cd1188e3667151af03c971bf11f3fd4'
            '802b66e1e52c5b4341afbdcc31211917b1223fb2db111c49603bc415c29d4b91'
            '122a76e1c7b34c79bfc2c5c9585e6ea489a6303cef6443fe15f0d49d53e84c2d'
            'e05535790881bdd719084b1da131f2c0a7d858ab6a22c2e9e061eaf451383ea1'
            '3251ea540eae1fb0648248f0c60179765956003a1c4adbf6c88e7476171a8702'
            '330a81ce83c32bfffe214b3404c5001585b9113a0e58572ea09bb67960a80ced'
            '5df481814e34ea162d5a48582b3055e3411c1ca206601724c2d7220e5b7649cb'
            '7cdfee2b802c8c74ff455191179d265ebd9359db35feec9405c93f47afff4b6e')

prepare() {
  cd ${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/001-fix-typo-in-formatregistry.patch
  patch -p1 -i ${srcdir}/002-mingw-w64.patch
  patch -p1 -i ${srcdir}/003-fix-redefinitions.patch
  patch -p1 -i ${srcdir}/004-msvc-flags.patch
  patch -p1 -i ${srcdir}/005-declspec-typo.patch
  patch -p1 -i ${srcdir}/006-mingw-no-dupenv_s.patch
  patch -p1 -i ${srcdir}/007-detect-mingw-python.patch
  patch -p1 -i ${srcdir}/008-export-attributes.patch
  patch -p1 -i ${srcdir}/009-python-module-extension.patch
}

build() {
  local _arch_opt=""
  if [ "${CARCH}" = "i686" ]; then
    _arch_opt="-DOCIO_USE_SSE=OFF"
  fi

  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 ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}

  local _py3ver=$(${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 \
    -G"MSYS Makefiles" \
     ${extra_config} \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DGLEW_INCLUDES=${MINGW_PREFIX}/include \
    -DGLEW_LIBRARIES=${MINGW_PREFIX}/lib/libglew32.dll.a \
    -DUSE_EXTERNAL_TINYXML=ON \
    -DUSE_EXTERNAL_YAML=ON \
    -DUSE_EXTERNAL_LCMS=ON \
    -DOCIO_USE_BOOST_PTR=ON \
    -DOCIO_BUILD_PYGLUE=ON \
    -DOCIO_PYGLUE_LINK=ON \
    -DPYTHON_VERSION=${_py3ver} \
    -DPYTHON=${MINGW_PREFIX}/bin/python.exe \
    -DPYTHON_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
    -DOCIO_BUILD_TRUELIGHT=OFF \
    ${_arch_opt} \
    ../${_realname}-${pkgver}

  make
}

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

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