# Template file for 'freecad'
pkgname=freecad
version=0.21.2
revision=1
build_wrksrc="FreeCAD-${version}"
build_style=cmake
pycompile_dirs="usr/lib/${pkgname}/Mod"
_inst_prefix=/usr/lib/${pkgname}
configure_args="
 -DPYTHON_EXECUTABLE=/usr/bin/python3
 -DBUILD_QT5=ON
 -DBUILD_FEM_NETGEN=OFF
 -DBUILD_FLAT_MESH=ON
 -DCMAKE_INSTALL_PREFIX=${_inst_prefix}
 -DCMAKE_INSTALL_DATAROOTDIR=/usr/share
 -DCMAKE_INSTALL_DATADIR=/usr/share/${pkgname}
 -DMEDFILE_INCLUDE_DIRS=/usr/include/med
 -DPYSIDE2RCCBINARY=/usr/bin/rcc-qt5
 -DPYSIDE2UICBINARY=/usr/bin/uic-qt5
"
hostmakedepends="pkg-config swig doxygen graphviz python3-setuptools
 python3-matplotlib"
makedepends="boost-devel libxerces-c-devel zlib-devel occt-devel vtk-devel
 hdf5-devel openmpi-devel libmed-devel eigen double-conversion-devel
 coin3-devel libshiboken2-devel libspnav-devel liblz4-devel netcdf-devel
 jsoncpp-devel coin3-doc glew-devel python3-devel pyside2-tools
 libpyside2-python3-devel qt5-devel qt5-svg-devel qt5-tools-devel
 qt5-declarative-devel qt5-webengine-devel qt5-webchannel-devel
 qt5-location-devel qt5-xmlpatterns-devel"
depends="python3-matplotlib python3-pyside2-webengine python3-pivy
 python3-GitPython python3-Markdown"
short_desc="General purpose 3D CAD modeler"
maintainer="yopito <pierre.bourgin@free.fr>"
license="LGPL-2.0-or-later"
homepage="https://freecadweb.org/"
_pycxxver=7.1.8
distfiles="
 https://github.com/FreeCAD/FreeCAD/archive/${version}.tar.gz
 ${SOURCEFORGE_SITE}/cxx/pycxx-${_pycxxver}.tar.gz
"
checksum="ceaf77cd12e8ad533d1535cc27ae4ca2a6e80778502dc9cdec906415d674b674
 4b91e1e1141c23fbd5039df635c4bb6e75632168548f56b83ce177193c0c98c6"
python_version=3
patch_args="-Np1 -d ${build_wrksrc}"

if [ "$XBPS_TARGET_LIBC" = musl ]; then
	makedepends+=" libexecinfo-devel"
	LDFLAGS="-lexecinfo"
fi

CXXFLAGS="-DPYCXX_PYTHON_2TO3"

post_extract() {
	rm -r "${build_wrksrc}/src/CXX"
	ln -Tsr "./pycxx-${_pycxxver}/CXX" "${build_wrksrc}/src/CXX"
}

post_patch() {
	# Report exact minor version
	# Freecad has a record of shipping mismatching version info
	vminor=${version#*.}
	vminor=${vminor%%.*}
	sed -i -e "s,^\(set(PACKAGE_VERSION_MINOR\) .*,\1 \"${vminor}\")," CMakeLists.txt

	# SubWCRev.py (SCM check) reports "unknown" for these fields
	vsed -e "s,\${PACKAGE_WCREF},${revision}_voidlinux," \
	     -e "s,\${PACKAGE_WCDATE},(from release)," \
	     -e "s,\${PACKAGE_WCURL},VoidLinux package ${pkgname}," \
	     -i src/Build/Version.h.cmake
}

pre_configure() {
	configure_args+=" -DPYCXX_INCLUDE_DIR=${wrksrc}/pycxx-${_pycxxver}"
	configure_args+=" -DPYCXX_SOURCE_DIR=${wrksrc}/pycxx-${_pycxxver}/Src"
}

post_install() {
	# AppHomePath is computed with binary's realpath:
	# do not move binaries but symlink them instead.
	vmkdir usr/bin
	for f in FreeCAD FreeCADCmd; do
		ln -s ${_inst_prefix}/bin/${f} ${DESTDIR}/usr/bin/${f}
	done
}
