# Author: williamjcm <w.jcm59@gmail.com>
# Contributor: mosra <mosra@centrum.cz>
# Based on the Magnum Arch Linux PKGBUILD maintained by xyproto and the MSYS2/MinGW PKGBUILD templates

_realname=magnum
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2020.06
pkgrel=4
pkgdesc='C++11/C++14 graphics middleware for games and data visualization (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://magnum.graphics/'
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-corrade>=$pkgver"
         "${MINGW_PACKAGE_PREFIX}-openal"
         "${MINGW_PACKAGE_PREFIX}-SDL2"
         "${MINGW_PACKAGE_PREFIX}-glfw"
         "${MINGW_PACKAGE_PREFIX}-vulkan-loader")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc"
             'git' 'unzip')
optdepends=("${MINGW_PACKAGE_PREFIX}-magnum-plugins: import and export features"
            "${MINGW_PACKAGE_PREFIX}-magnum-integration: integrations with third-party math and physics libraries")
source=("${_realname}-${pkgver}.zip::https://github.com/mosra/magnum/archive/v${pkgver}.zip"
        0001-cmake-fix-FindOpenAL.patch
        0002-missing-includes.patch
        0003-constexpr-enums.patch)
noextract=("${_realname}-${pkgver}.zip")
sha256sums=('78c52bc403cec27b98d8d87186622ca57f8d70ffd64342fe4094c720b7d3b0e3'
            'c97070eaac01169f8f18c1cecc40d87c6613a3a5061f22b0dc4502df0d3da095'
            '62f09c0b6f420df638014cd4ade28b5a52f3db7590cb2d666f4a785be745af44'
            '4418adc5b11148b3af138b3aa86b44a096539940c24b466b3817a8e87b67ee7d')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  rm -rf ${_realname}-${pkgver}
  unzip -q ../${_realname}-${pkgver}.zip

  cd ${_realname}-${pkgver}

  # https://github.com/mosra/magnum/commit/742b1bb8b2e0cce9ac278d95b69c403729f69210.patch
  apply_patch_with_msg \
    0001-cmake-fix-FindOpenAL.patch
  
  apply_patch_with_msg \
    0002-missing-includes.patch

  # https://github.com/mosra/magnum/issues/642
  apply_patch_with_msg \
    0003-constexpr-enums.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

    if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
        # Fix compilation with clang 16
        extra_config+=("-DCMAKE_CXX_FLAGS=-Wno-enum-constexpr-conversion")
    fi

    MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
        ${MINGW_PREFIX}/bin/cmake \
            -G'Ninja' \
            -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
            -DCMAKE_POLICY_VERSION_MINIMUM=3.5 \
            -DWITH_AL_INFO=ON \
            -DWITH_ANYAUDIOIMPORTER=ON \
            -DWITH_ANYIMAGECONVERTER=ON \
            -DWITH_ANYIMAGEIMPORTER=ON \
            -DWITH_ANYSCENEIMPORTER=ON \
            -DWITH_ANYSCENECONVERTER=ON \
            -DWITH_AUDIO=ON \
            -DWITH_DISTANCEFIELDCONVERTER=ON \
            -DWITH_WGLCONTEXT=ON \
            -DWITH_IMAGECONVERTER=ON \
            -DWITH_MAGNUMFONT=ON \
            -DWITH_MAGNUMFONTCONVERTER=ON \
            -DWITH_OBJIMPORTER=ON \
            -DWITH_OPENGLTESTER=ON \
            -DWITH_FONTCONVERTER=ON \
            -DWITH_GL_INFO=ON \
            -DWITH_GLFWAPPLICATION=ON \
            -DWITH_SDL2APPLICATION=ON \
            -DWITH_TGAIMAGECONVERTER=ON \
            -DWITH_TGAIMPORTER=ON \
            -DWITH_VK=ON \
            -DWITH_WAVAUDIOIMPORTER=ON \
            -DWITH_WINDOWLESSWGLAPPLICATION=ON \
            "${extra_config[@]}" \
            ../${_realname}-${pkgver}

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

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

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