# Contributor: Aiden Grossman <agrossman154@yahoo.com>
# Maintainer: Aiden Grossman <agrossman154@yahoo.com>
pkgname=freecad
pkgver=0.21.0
pkgrel=0
pkgdesc="Free and open source 3D parametric modeler"
url="https://freecadweb.org/"
license="LGPL-2.0-or-later"
arch="x86_64" # dependency OpenCascade is only x86_64
depends="
	graphviz
	hdf5
	opencascade
	py3-matplotlib
	py3-numpy
	py3-pivy
	py3-ply
	py3-pyside2
	py3-six
	py3-yaml
	python3
	"
makedepends="
	boost-dev
	cmake
	coin-dev
	doxygen
	eigen-dev
	freeimage-dev
	glu-dev
	hdf5-dev
	libmedc-dev
	libshiboken2-dev
	onetbb-dev
	opencascade-dev
	py3-pyside2-dev
	python3-dev
	shiboken2
	qt5-qtsvg-dev
	qt5-qtwebengine-dev
	qt5-qtxmlpatterns-dev
	samurai
	swig
	vtk-dev
	xerces-c-dev
	"
options="!check" # Failing test units upstream as well
checkdepends="xvfb-run mesa mesa-dri-gallium font-opensans"
source="https://github.com/FreeCAD/FreeCAD/archive/$pkgver/freecad-$pkgver.tar.gz
	$pkgname-cstdint.patch::https://github.com/FreeCAD/FreeCAD/commit/f8f02f089537497a1bf46bc1057f01659b6636c1.patch
	no-execinfo.patch
	"
builddir="$srcdir/FreeCAD-$pkgver"

build() {
	cmake -B build -G Ninja \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_C_FLAGS="$CFLAGS -fPIC -w" \
		-DCMAKE_CXX_FLAGS="$CXXFLAGS -fPIC -w" \
		-DCMAKE_INSTALL_PREFIX=/usr/lib/freecad \
		-DCMAKE_INSTALL_DATADIR=/usr/share/freecad \
		-DCMAKE_INSTALL_DATAROOTDIR=/usr/share \
		-DCMAKE_INSTALL_DOCDIR=/usr/share/freecad/doc \
		-DBUILD_ENABLE_CXX_STD=C++17 \
		-DBUILD_QT5=ON \
		-DFREECAD_USE_EXTERNAL_PIVY=ON \
		-DFREECAD_USE_OCC_VARIANT="Official Version" \
		-DFREECAD_USE_QT_FILEDIALOG=ON \
		-DPYTHON_EXECUTABLE=/usr/bin/python3
	cmake --build build
}

check() {
	DESTDIR=test_install cmake --install build
	mkdir -p test_install/usr/bin
	ln -s ../lib/freecad/bin/FreeCAD test_install/usr/bin/FreeCAD
	ln -s ../lib/freecad/bin/FreeCADCmd test_install/usr/bin/FreeCADCmd
	LD_LIBRARY_PATH="$PWD"/test_install/usr/lib/freecad/lib \
		xvfb-run "$PWD"/test_install/usr/bin/FreeCAD -t 0
}

package() {
	DESTDIR="$pkgdir" cmake --install build

	# FreeCAD does not initialize correctly when binaries
	# are located under /usr/bin; thus, symlinks are necessary.
	install -d "$pkgdir"/usr/bin
	ln -s /usr/lib/freecad/bin/FreeCAD "$pkgdir"/usr/bin/FreeCAD
	ln -s /usr/lib/freecad/bin/FreeCADCmd "$pkgdir"/usr/bin/FreeCADCmd
}

sha512sums="
f5b4428217d9abd18aed1dcd8a5c77132b90173d07627ff9164919a87f04d242f9d616e9655751ec06ff57fc3e242090cc4d7e1d7045fdae9bc2e281e7855281  freecad-0.21.0.tar.gz
27dffdbb124096e7fcbec3589cdcfc587ea09bf039c1c1108edb68a9a4fd1278f66a39642636f98b8759a44da92485fd748f8b3d615b0625bc37fc717130e237  freecad-cstdint.patch
73aaba7015dce7048eb7d2456131b5b5ba4673cc980503331987be54d99daed5f61db015ca33d7d2ef0f02bd3192da8ce122c103c3b93f9959927deb4f0b933e  no-execinfo.patch
"
