# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=opensubdiv
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-docs")
pkgver=3.7.0
pkgrel=1
pkgdesc="An Open-Source subdivision surface library (mingw-w64)"
arch=(any)
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="http://graphics.pixar.com/opensubdiv"
msys2_repository_url="https://github.com/PixarAnimationStudios/OpenSubdiv"
license=('custom:TOST-1.0')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-tbb")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-glfw"
             "${MINGW_PACKAGE_PREFIX}-ptex"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-python-pygments"
             "${MINGW_PACKAGE_PREFIX}-python-docutils"
             "${MINGW_PACKAGE_PREFIX}-opencl-icd"
             "${MINGW_PACKAGE_PREFIX}-opencl-headers")
source=("https://github.com/PixarAnimationStudios/OpenSubdiv/archive/v${pkgver//./_}/OpenSubdiv-${pkgver}.tar.gz"
        002-Fix-building-on-mingw-w64.patch)
sha256sums=('f843eb49daf20264007d807cbc64516a1fed9cdb1149aaf84ff47691d97491f9'
            '2de9d7a14fdca3aa3e34f15b4ad3c0a9db137a5b97a755a496a4bb703163426d')

prepare() {
  cd "${srcdir}"/"OpenSubdiv-${pkgver//./_}"

  patch -Np1 -i "${srcdir}"/002-Fix-building-on-mingw-w64.patch
}

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

  CXXFLAGS+=" -w" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DBUILD_SHARED_LIBS=ON \
    -DNO_CLEW=ON \
    -DNO_CUDA=ON \
    -DNO_EXAMPLES=ON \
    -DNO_REGRESSION=ON \
    -DNO_TUTORIALS=ON \
    -DNO_TESTS=ON \
    -DNO_GLTESTS=ON \
    -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python \
    -DDXSDK_ROOT=${MINGW_PREFIX} \
    ../OpenSubdiv-${pkgver//./_}

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

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

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

  # split docs
  mkdir -p dest${MINGW_PREFIX}/share
  mv "${pkgdir}${MINGW_PREFIX}"/share/doc dest${MINGW_PREFIX}/share/doc
}

package_opensubdiv-docs() {
  pkgdesc+=" (documentation)"
  depends=()

  mv dest/* "${pkgdir}"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
