# Maintainer: Rafal Brzegowy <rafal.brzegowy@yahoo.com>

_realname=opencascade
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=7.5.2
_pkgver2=V${pkgver//./_}
pkgrel=1
pkgdesc='Open CASCADE Technology, 3D modeling & numerical simulation (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
depends=("${MINGW_PACKAGE_PREFIX}-tk"
         "${MINGW_PACKAGE_PREFIX}-tcl"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-intel-tbb"
         "${MINGW_PACKAGE_PREFIX}-freeimage"
         "${MINGW_PACKAGE_PREFIX}-vtk"
         "${MINGW_PACKAGE_PREFIX}-ffmpeg"
         "${MINGW_PACKAGE_PREFIX}-rapidjson"
         "${MINGW_PACKAGE_PREFIX}-openvr")
makedepends=("${MINGW_PACKAGE_PREFIX}-qt5"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-doxygen")
license=('custom')
url='https://www.opencascade.org'
source=("opencascade-${pkgver}.tgz::https://git.dev.opencascade.org/gitweb/?p=occt.git;a=snapshot;h=refs/tags/${_pkgver2};sf=tgz"
        "0001-fix-compile-openvr.patch")
sha256sums=('1A32D2B0D6D3C236163CB45139221FB198F0F3CDAD56606C5B1C9A2A8869B3AC'
         'SKIP')
		 
prepare() {
  cd "${srcdir}"/occt-${_pkgver2}
  patch -p1 -i "${srcdir}"/0001-fix-compile-openvr.patch
}

build() {
  local common_config
  common_config=(
    -DUSE_D3D=1
    -D3RDPARTY_DIR="${MINGW_PREFIX}"
    -D3RDPARTY_TK_DIR="${MINGW_PREFIX}"
    -D3RDPARTY_TCL_DIR="${MINGW_PREFIX}"
    -D3RDPARTY_FREETYPE_DIR="${MINGW_PREFIX}"
    -D3RDPARTY_FREEIMAGE_DIR="${MINGW_PREFIX}"
    -D3RDPARTY_FFMPEG_DIR="${MINGW_PREFIX}"
    -D3RDPARTY_RAPIDJSON_DIR="${MINGW_PREFIX}"
    -D3RDPARTY_OPENVR_DIR="${MINGW_PREFIX}"
    -D3RDPARTY_TK_DLL_DIR="${MINGW_PREFIX}/bin/"
    -D3RDPARTY_TK_DLL="${MINGW_PREFIX}/bin/tk86.dll"
    -D3RDPARTY_TK_LIBRARY_DIR="${MINGW_PREFIX}/lib/"
    -D3RDPARTY_TCL_DLL_DIR="${MINGW_PREFIX}/bin/"
    -D3RDPARTY_TCL_DLL="${MINGW_PREFIX}/bin/tcl86.dll"
    -D3RDPARTY_TCL_LIBRARY_DIR="${MINGW_PREFIX}/lib/"
    -D3RDPARTY_FREETYPE_DLL_DIR="${MINGW_PREFIX}/bin/"
    -D3RDPARTY_FREETYPE_DLL="${MINGW_PREFIX}/bin/libfreetype-6.dll"
    -D3RDPARTY_FREETYPE_LIBRARY_DIR="${MINGW_PREFIX}/lib/"
    -DUSE_FFMPEG=1
    -D3RDPARTY_FFMPEG_DLL_DIR_avformat="${MINGW_PREFIX}/bin/"
    -D3RDPARTY_FFMPEG_DLL_avformat="${MINGW_PREFIX}/bin/avformat-58.dll"
    -D3RDPARTY_FFMPEG_LIBRARY_DIR_avformat="${MINGW_PREFIX}/lib/"
    -D3RDPARTY_FFMPEG_DLL_DIR_avutil="${MINGW_PREFIX}/bin/"
    -D3RDPARTY_FFMPEG_DLL_avutil="${MINGW_PREFIX}/bin/avutil-56.dll"
    -D3RDPARTY_FFMPEG_LIBRARY_DIR_avutil="${MINGW_PREFIX}/lib/"
    -D3RDPARTY_FFMPEG_DLL_DIR_avcodec="${MINGW_PREFIX}/bin/"
    -D3RDPARTY_FFMPEG_DLL_avcodec="${MINGW_PREFIX}/bin/avcodec-58.dll"
    -D3RDPARTY_FFMPEG_LIBRARY_DIR_avcodec="${MINGW_PREFIX}/lib/"
    -D3RDPARTY_FFMPEG_DLL_DIR_swscale="${MINGW_PREFIX}/bin/"
    -D3RDPARTY_FFMPEG_DLL_swscale="${MINGW_PREFIX}/bin/swscale-5.dll"
    -D3RDPARTY_FFMPEG_LIBRARY_DIR_swscale="${MINGW_PREFIX}/lib"
    -DUSE_FREEIMAGE=1
    -D3RDPARTY_FREEIMAGE_DLL_DIR_freeimage="${MINGW_PREFIX}/bin/"
    -D3RDPARTY_FREEIMAGE_DLL_freeimage="${MINGW_PREFIX}/bin/libfreeimage-3.dll"
    -D3RDPARTY_FREEIMAGE_LIBRARY_DIR_freeimage="${MINGW_PREFIX}/lib/"
    -DUSE_OPENVR=1
    -D3RDPARTY_OPENVR_DLL_DIR_openvr_api="${MINGW_PREFIX}/bin/"
    -D3RDPARTY_OPENVR_DLL_openvr_api="${MINGW_PREFIX}/bin/libopenvr_api.dll"
    -D3RDPARTY_OPENVR_LIBRARY_DIR_openvr_api="${MINGW_PREFIX}/lib/"
    -DUSE_RAPIDJSON=1
    -DUSE_TBB=0
    -D3RDPARTY_TBB_DLL_DIR="${MINGW_PREFIX}/bin/"
    -D3RDPARTY_TBB_DLL="${MINGW_PREFIX}/bin/tbb.dll"
    -D3RDPARTY_TBB_LIBRARY_DIR="${MINGW_PREFIX}/lib/"
    -D3RDPARTY_TBB_LIBRARY="${MINGW_PREFIX}/lib/libtbb.dll.a"
    -D3RDPARTY_TBBMALLOC_DLL_DIR="${MINGW_PREFIX}/bin/"
    -D3RDPARTY_TBBMALLOC_DLL="${MINGW_PREFIX}/bin/tbbmalloc.dll"
    -D3RDPARTY_TBBMALLOC_LIBRARY_DIR="${MINGW_PREFIX}/lib/"
    -D3RDPARTY_TBBMALLOC_LIBRARY="${MINGW_PREFIX}/lib/libtbbmalloc.dll.a"
    -DUSE_VTK=1	
  )

  #Static Build
  [[ -d "${srcdir}/build-${MINGW_CHOST}-static" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}-static"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}-static" && cd "${srcdir}/build-${MINGW_CHOST}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -G"MSYS Makefiles" \
    -DCMAKE_BUILD_TYPE="Release" \
    -DINSTALL_DIR_LAYOUT="Unix" \
    -DBUILD_LIBRARY_TYPE="Static" \
    "${common_config[@]}" \
    -D3RDPARTY_TK_LIBRARY="${MINGW_PREFIX}/lib/libtk86.a" \
    -D3RDPARTY_TCL_LIBRARY="${MINGW_PREFIX}/lib/libtcl86.a" \
    -D3RDPARTY_FREETYPE_LIBRARY="${MINGW_PREFIX}/lib/libfreetype.a" \
    -D3RDPARTY_FFMPEG_LIBRARY_avformat="${MINGW_PREFIX}/lib/libavformat.a" \
    -D3RDPARTY_FFMPEG_LIBRARY_avutil="${MINGW_PREFIX}/lib/libavutil.a" \
    -D3RDPARTY_FFMPEG_LIBRARY_avcodec="${MINGW_PREFIX}/lib/libavcodec.a" \
    -D3RDPARTY_FFMPEG_LIBRARY_swscale="${MINGW_PREFIX}/lib/libswscale.a" \
    -D3RDPARTY_FREEIMAGE_LIBRARY="${MINGW_PREFIX}/lib/libfreeimage.a" \
    -D3RDPARTY_OPENVR_LIBRARY_openvr_api="${MINGW_PREFIX}/lib/libopenvr_api.a" \
    ../occt-${_pkgver2}	
	
    ${MINGW_PREFIX}/bin/cmake --build .

  #Shared Build
  [[ -d "${srcdir}/build-${MINGW_CHOST}-shared" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}-shared"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}-shared" && cd "${srcdir}/build-${MINGW_CHOST}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -G"MSYS Makefiles" \
    -DCMAKE_BUILD_TYPE="Release" \
    -DINSTALL_DIR_LAYOUT="Unix" \
    -DBUILD_LIBRARY_TYPE="Shared" \
    "${common_config[@]}" \
    -D3RDPARTY_TK_LIBRARY="${MINGW_PREFIX}/lib/libtk86.dll.a" \
    -D3RDPARTY_TCL_LIBRARY="${MINGW_PREFIX}/lib/libtcl86.dll.a" \
    -D3RDPARTY_FREETYPE_LIBRARY="${MINGW_PREFIX}/lib/libfreetype.dll.a" \
    -D3RDPARTY_FFMPEG_LIBRARY_avformat="${MINGW_PREFIX}/lib/libavformat.dll.a" \
    -D3RDPARTY_FFMPEG_LIBRARY_avutil="${MINGW_PREFIX}/lib/libavutil.dll.a" \
    -D3RDPARTY_FFMPEG_LIBRARY_avcodec="${MINGW_PREFIX}/lib/libavcodec.dll.a" \
    -D3RDPARTY_FFMPEG_LIBRARY_swscale="${MINGW_PREFIX}/lib/libswscale.dll.a" \
    -D3RDPARTY_FREEIMAGE_LIBRARY="${MINGW_PREFIX}/lib/libfreeimage.dll.a" \
    -D3RDPARTY_OPENVR_LIBRARY_openvr_api="${MINGW_PREFIX}/lib/libopenvr_api.dll.a" \
    ../occt-${_pkgver2}
	
    ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  #Static Install
  cd "${srcdir}/build-${MINGW_CHOST}-static"
  DESTDIR=${pkgdir} cmake --build . --target install
  
  #Shared Install
  cd "${srcdir}/build-${MINGW_CHOST}-shared"
  DESTDIR=${pkgdir} cmake --build . --target install
  
  install -Dm644 ${srcdir}/occt-${_pkgver2}/LICENSE_LGPL_21.txt ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
