# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=imath
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=3.1.11
pkgrel=2
pkgdesc='A C++ and python library of 2D and 3D vector, matrix, and math operations for computer graphics (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url='https://www.openexr.com/'
msys2_repository_url="https://github.com/AcademySoftwareFoundation/Imath"
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             $([[ ${CARCH} == i686 ]] || echo \
               "${MINGW_PACKAGE_PREFIX}-boost" \
               "${MINGW_PACKAGE_PREFIX}-python" \
               "${MINGW_PACKAGE_PREFIX}-python-numpy"))
if [[ ${CARCH} != i686 ]]; then
  optdepends=("${MINGW_PACKAGE_PREFIX}-python: python bindings"
              "${MINGW_PACKAGE_PREFIX}-boost: python bindings")
fi
source=("https://github.com/AcademySoftwareFoundation/Imath/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        0001-cmake-libs-name.patch
        0002-fix-openexr-clang-build.patch)
sha256sums=('9057849585e49b8b85abe7cc1e76e22963b01bfdc3b6d83eac90c499cd760063'
            'c9a21f10e921b08b059f10fefabaaf2b97178847dbcedc5d0b824456c63f2153'
            '0eea3c7b57d5b59887f963d696c2afdcfb1976a3913f161995e19b13d6a65086')

prepare() {
  cd "Imath-${pkgver}"
  patch -p1 -i "${srcdir}/0001-cmake-libs-name.patch"
  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    patch -p1 -i "${srcdir}/0002-fix-openexr-clang-build.patch"
  fi
}

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

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

  if [[ ${CARCH} != i686 ]]; then
    _extra_config+=("-DPYTHON=ON" "-DPYBIND11=OFF")
  else
    _extra_config+=("-DPYTHON=OFF" "-DPYBIND11=OFF")
  fi

  # https://github.com/AcademySoftwareFoundation/Imath/issues/312
  CFLAGS="-DPLATFORM_VISIBILITY_AVAILABLE -fvisibility=hidden -fvisibility-inlines-hidden" \
  CXXFLAGS="-DPLATFORM_VISIBILITY_AVAILABLE -fvisibility=hidden -fvisibility-inlines-hidden" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G "Ninja" \
    "${_extra_config[@]}" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DBUILD_SHARED_LIBS=ON \
    -DBUILD_TESTING=OFF \
    -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
    -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
    -DIMATH_INSTALL_PKG_CONFIG=ON \
    ../Imath-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build .
}

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

  # Fix cmake files
  local PREFIX_WIN=$(cygpath -am ${MINGW_PREFIX})
  find "${pkgdir}${MINGW_PREFIX}/lib/cmake" -type f -name '*.cmake' \
      -exec sed -i -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" {} \;

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