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

_realname=ogre3d
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.12.6
pkgrel=2
pkgdesc="A cross-platform 3D game engine (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://www.ogre3d.org/"
license=("MIT")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-swig"
             "git")
depends=("${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-cppunit"
         "${MINGW_PACKAGE_PREFIX}-FreeImage"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-glsl-optimizer"
         "${MINGW_PACKAGE_PREFIX}-hlsl2glsl"
         "${MINGW_PACKAGE_PREFIX}-intel-tbb"
         "${MINGW_PACKAGE_PREFIX}-openexr"
         "${MINGW_PACKAGE_PREFIX}-SDL2"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-pugixml"
         "${MINGW_PACKAGE_PREFIX}-tinyxml"
         "${MINGW_PACKAGE_PREFIX}-winpthreads"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zziplib")
options=('staticlibs' '!strip') # '!buildflags'
source=(${_realname}-${pkgver}.tar.gz::"https://github.com/OGRECave/ogre/archive/v${pkgver}.tar.gz"
        git+https://github.com/ocornut/imgui.git#commit=e5cb04b132cba94f902beb6186cb58b864777012
        002-link-shared-freeimage.patch
        004-use-mingw-w64-directx.patch
        010-missing-include.patch
        011-mingw-check-security-coockie-not-available.patch
        012-add-link-library.patch
        014-custom-install.patch
        020-dx11-mingw.patch)
sha256sums=('c6347094733b459b6e8bb10f6ae234a66b635e3ef4212ba536b8fa146a775acf'
            'SKIP'
            '67966d6e0791c5804ed5cbec116594ebb72e9633118c31458dfa16181fdd01b2'
            'c7ca64dbd9a8f3f0271c12739807021a5c0782d254c4142c6e86746a96a44438'
            'd4e812fceb3dadc20c85857e3e624654cbc6bd4ebd20990fed2e5e84adfa9272'
            '60145b0dcbacc5da8528c0accc5a3fbe1f00fe81c1b14f312c2f128594eb93c4'
            'fb4db83c9d42358a63bf4a11792cdea43482d69f09d7da9467782af32ba1bfb7'
            '12845dd3270a6b05ab52e982213a7f2687b51a09cd6b598c0550555d3d94ac6d'
            '67a99e88d1d096e92849cffcf818f991c12edaeadeb8f96f0593bb41905d6e3e')

prepare() {
  # Workaround packaging bug
  cp -rf imgui/* ogre-${pkgver}/Components/Overlay/src/imgui/

  cd "${srcdir}/ogre-${pkgver}"
  patch -p1 -i ${srcdir}/002-link-shared-freeimage.patch
  patch -p1 -i ${srcdir}/004-use-mingw-w64-directx.patch
  patch -p1 -i ${srcdir}/010-missing-include.patch
  patch -p1 -i ${srcdir}/011-mingw-check-security-coockie-not-available.patch
  patch -p1 -i ${srcdir}/012-add-link-library.patch
  patch -p1 -i ${srcdir}/014-custom-install.patch
  # https://github.com/OGRECave/ogre/commit/3fafb147242f32079dd3b57b8a80ad9fbbb415cb
  patch -p1 -i ${srcdir}/020-dx11-mingw.patch
}

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

  rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir build-${MINGW_CHOST} && cd build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -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_CONFIG_DOUBLE=OFF \
    -DOGRE_BUILD_COMPONENT_JAVA=OFF \
    -DOGRE_BUILD_SAMPLES=ON \
    -DOGRE_INSTALL_SAMPLES=ON \
    -DOGRE_CONFIG_FILESYSTEM_UNICODE=ON \
    -DPYTHON_EXECUTABLE="${MINGW_PREFIX}/bin/python.exe" \
    "${extra_config[@]}" \
    ../ogre-${pkgver}

    make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make -j1 DESTDIR=${pkgdir} install

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