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

_realname=imath
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=3.2.2
pkgrel=1
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' 'clangarm64')
url='https://www.openexr.com/'
msys2_repository_url="https://github.com/AcademySoftwareFoundation/Imath"
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-cc-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/releases/download/v${pkgver}/Imath-${pkgver}.tar.gz"
        0001-cmake-libs-name.patch
        0002-fix-openexr-clang-build.patch)
sha256sums=('0f5a783b424f374e6f27ec8b0c73130e89b08814ac8fa2e84fd7fe0b05862c53'
            '5a70997f2d34eba47210ac93f67b2037ca61c5c967a4a1f00a479d1c5eda490f'
            '09b9b180f7a59490d164ef3288fa1f5e5f14f99185cb412fb88d35364690ff6d')

prepare() {
  cd "Imath-${pkgver}"
  patch -p1 -i "${srcdir}/0001-cmake-libs-name.patch"
  patch -p1 -i "${srcdir}/0002-fix-openexr-clang-build.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

  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=" \
    cmake \
      -GNinja \
      "${_extra_config[@]}" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DBUILD_SHARED_LIBS=ON \
      -DBUILD_TESTING=OFF \
      -DPython3_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
      -S "Imath-${pkgver}" \
      -B "build-${MSYSTEM}"

  cmake --build "build-${MSYSTEM}"
}

package() {
  DESTDIR="${pkgdir}" cmake --install "build-${MSYSTEM}"

  # 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" {} \;

  # Fix pkgconfig files
  if [[ ${CARCH} != i686 ]]; then
    sed -i -e "s|${PREFIX_WIN}|\$\{prefix\}|g" "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/PyImath.pc
  fi

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