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

_realname=assimp
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=5.0.1
pkgrel=9
pkgdesc="Portable Open Source library to import various well-known 3D model formats in an uniform manner (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
license=('BSD')
depends=("${MINGW_PACKAGE_PREFIX}-minizip"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
url="http://www.assimp.org"
source=(${_realname}-${pkgver}.tar.gz::https://github.com/assimp/assimp/archive/v${pkgver}.tar.gz
        build-fixes.patch
        mingw-cmake-targets.patch
        llvm-rc-workaround.patch)
options=('!strip' 'staticlibs')
sha256sums=('11310ec1f2ad2cd46b95ba88faca8f7aaa1efe9aa12605c55e3de2b977b3dbfc'
            '8122b87256094833d8407ad8003c67916459e41472c5cfee58a55ed495d2f50c'
            'ce709ad48e9a623dc17480b15bf01c06c6c20a573c358d1768ddc676f706b210'
            '3bdb6483fda22db9126991380a01220785e2e1d3ac1be7e4694096e5f69bcdfd')

prepare() {
  cd ${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/build-fixes.patch
  patch -p1 -i ${srcdir}/mingw-cmake-targets.patch

  if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
    sed -i "s/-fPIC //g" CMakeLists.txt
    patch -p1 -i "${srcdir}/llvm-rc-workaround.patch"
  fi
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=RELEASE \
    -DASSIMP_BUILD_ASSIMP_TOOLS=YES \
    -DASSIMP_BUILD_STATIC_LIB=OFF \
    -DASSIMP_BUILD_TESTS=OFF \
    -DDirectX_DXERR_LIBRARY=${MINGW_PREFIX}/${MINGW_CHOST}/lib/libdxerr9.a \
    ../${_realname}-${pkgver}

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

package() {
  cd "${srcdir}//build-${MINGW_CHOST}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target install
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"

  # Remove this in release > 5.0.1
  # https://bugs.archlinux.org/task/67735
  sed -i "s|includedir=.*|includedir=${MINGW_PREFIX}/include|" "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc
}
