# Author: williamjcm <w.jcm59@gmail.com>
# Contributor: mosra <mosra@centrum.cz>
# Based on the AUR PKGBUILD from https://github.com/mosra/magnum-integration and the MSYS2/MinGW PKGBUILD templates

_realname=magnum-integration
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2020.06
_imgui_pkgver=1.76
pkgrel=4
pkgdesc='Integration libraries for the Magnum C++11/C++14 graphics engine (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://magnum.graphics/'
msys2_repository_url="https://github.com/mosra/magnum-integration"
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-magnum>=$pkgver"
         "${MINGW_PACKAGE_PREFIX}-bullet"
         "${MINGW_PACKAGE_PREFIX}-eigen3"
         "${MINGW_PACKAGE_PREFIX}-glm")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/mosra/magnum-integration/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "https://github.com/ocornut/imgui/archive/v${_imgui_pkgver}.tar.gz")
noextract=("${_realname}-${pkgver}.tar.gz")
sha256sums=('4eb461e0a38d7be69a52b8faf7664493da4e4cabc2c4fa86bd672d2e8f0a9311'
            'e482dda81330d38c87bd81597cacaa89f05e20ed2c4c4a93a64322e97565f6dc')

prepare() {
  cd "${srcdir}"
  echo "Extracting ${_realname}-${pkgver}.tar.gz ..."
  tar -xzf ${_realname}-${pkgver}.tar.gz || true
}

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

  # Bundle ImGui
  ln -sf $srcdir/imgui-${_imgui_pkgver}/ "$srcdir/${_realname}-${pkgver}/src/MagnumExternal/ImGui"

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G'Ninja' \
      -DCMAKE_POLICY_VERSION_MINIMUM=3.5 \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DWITH_BULLET=ON \
      -DWITH_DART=OFF \
      -DWITH_EIGEN=ON \
      -DWITH_GLM=ON \
      -DWITH_IMGUI=ON \
      "${extra_config[@]}" \
      ../${_realname}-${pkgver}

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

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

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
