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

_realname=ogre3d
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=14.4.1
_imgui_ver=1.92.1
pkgrel=1
pkgdesc="A cross-platform 3D game engine (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.ogre3d.org/"
msys2_repository_url="https://github.com/OGRECave/ogre"
msys2_references=(
  'archlinux: ogre'
)
license=("spdx:MIT")
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-bullet"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-glsl-optimizer"
         "${MINGW_PACKAGE_PREFIX}-hlsl2glsl"
         "${MINGW_PACKAGE_PREFIX}-SDL2"
         "${MINGW_PACKAGE_PREFIX}-tinyxml"
         "${MINGW_PACKAGE_PREFIX}-winpthreads"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-swig"
             "${MINGW_PACKAGE_PREFIX}-assimp"
             "${MINGW_PACKAGE_PREFIX}-freeimage"
             "${MINGW_PACKAGE_PREFIX}-pugixml"
             "${MINGW_PACKAGE_PREFIX}-qt6-base")
optdepends=("${MINGW_PACKAGE_PREFIX}-assimp"
            "${MINGW_PACKAGE_PREFIX}-freeimage"
            "${MINGW_PACKAGE_PREFIX}-python: python bindings"
            "${MINGW_PACKAGE_PREFIX}-pugixml: XML converter"
            "${MINGW_PACKAGE_PREFIX}-qt6-base: integration with the Qt Library for window creation and input")
options=('!strip')
source=("https://github.com/OGRECave/ogre/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        https://github.com/ocornut/imgui/archive/v${_imgui_ver}/imgui-${_imgui_ver}.tar.gz
        001-use-soversion.patch
        004-use-mingw-w64-directx.patch
        010-missing-include.patch
        015-export-symbols-samples-plugin.patch
        016-fix-plugins-install-destination.patch)
sha256sums=('1c1d5d48b26acf8dd3f32fcb4cafd1216f9fe3a2ceec260ec086926651ad74de'
            '32c237c2abf67a2ffccaac17192f711d4a787554b4133187a153d49057d6109c'
            '81fbe2f951be7f5b5bded24a3050be3188e164d321df9124b5abfb543234a128'
            'c7ca64dbd9a8f3f0271c12739807021a5c0782d254c4142c6e86746a96a44438'
            '2850af66e34003415e86b4f17c24e117b221f64ab98e1cef45b5102c9c48350d'
            '004842e88cbbc76d09356fbddf5bc123b1b0fc7b2e64b8677b2a2316df7d9099'
            'ed6e383b1b4f1b84f1e93288540789c8fca0693a5dc9e3b4f840a63adbf5daac')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/ogre-${pkgver}"
  apply_patch_with_msg \
    004-use-mingw-w64-directx.patch \
    010-missing-include.patch \
    015-export-symbols-samples-plugin.patch \
    016-fix-plugins-install-destination.patch
}

build() {
  mkdir build-${MSYSTEM} && cd build-${MSYSTEM}

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

  CXXFLAGS+=" -Wno-ignored-attributes -Wno-inconsistent-missing-override"

  cp -r "${srcdir}"/imgui-${_imgui_ver} .

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX" \
  ${MINGW_PREFIX}/bin/cmake.exe -Wno-dev \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DPYTHON_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
    -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
    -DOGRE_DEPENDENCIES_DIR=${MINGW_PREFIX} \
    -DOGRE_INSTALL_TOOLS=ON \
    -DOGRE_COPY_DEPENDENCIES=OFF \
    -DOGRE_INSTALL_DEPENDENCIES=OFF \
    -DOGRE_BUILD_RENDERSYSTEM_D3D11=ON \
    -DOGRE_BUILD_RENDERSYSTEM_GL3PLUS=ON \
    -DOGRE_BUILD_COMPONENT_VOLUME=ON \
    -DOGRE_BUILD_COMPONENT_PAGING=ON \
    -DOGRE_BUILD_COMPONENT_TERRAIN=ON \
    -DOGRE_BUILD_COMPONENT_RTSHADERSYSTEM=ON \
    -DOGRE_BUILD_COMPONENT_PROPERTY=ON \
    -DOGRE_BUILD_COMPONENT_PYTHON=ON \
    -DOGRE_BUILD_COMPONENT_CSHARP=OFF \
    -DOGRE_BUILD_PLUGIN_FREEIMAGE=ON \
    -DOGRE_CONFIG_DOUBLE=OFF \
    -DOGRE_BUILD_COMPONENT_JAVA=OFF \
    -DOGRE_BUILD_SAMPLES=ON \
    -DOGRE_INSTALL_SAMPLES=ON \
    -DOGRE_BUILD_TESTS=OFF \
    -DOGRE_CONFIG_FILESYSTEM_UNICODE=ON \
    $([[ ${CARCH} == aarch64 ]] && echo "-DOGRE_BUILD_RENDERSYSTEM_D3D9=OFF") \
    -DOGRE_BUILD_TESTS=OFF \
    -DOGRE_INSTALL_PDB=OFF \
    -DOGRE_MEDIA_PATH=share/OGRE/Media \
    -DOGRE_CMAKE_DIR=lib/OGRE/cmake \
    -DOGRE_DOCS_PATH=share/doc/OGRE \
    -DOGRE_PLUGINS_PATH=lib/OGRE \
    -DOGRE_NODELESS_POSITIONING=ON \
    "${extra_config[@]}" \
    ../ogre-${pkgver}

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

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

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

  install -Dm0644 "${srcdir}"/ogre-${pkgver}/LICENSE "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
  mkdir -p "${pkgdir}"${MINGW_PREFIX}/share/OGRE/Media/fonts
}
