# Template file for 'freecad'
pkgname=freecad
version=0.20.2
revision=7
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"
makedepends="python3-devel 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 pyside2-tools
 liblz4-devel libpyside2-python3-devel python3-matplotlib netcdf-devel
 jsoncpp-devel qt5-devel qt5-svg-devel qt5-tools-devel qt5-webkit-devel
 qt5-xmlpatterns-devel coin3-doc glew-devel python3-pivy"
depends="python3-matplotlib python3-pyside2-webengine python3-pivy"
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="46922f3a477e742e1a89cd5346692d63aebb2b67af887b3e463e094a4ae055da
 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
}
