SUMMARY="3D Creation/Animation/Publishing System"
DESCRIPTION="Blender is the free and open source 3D creation suite. It \
supports the entirety of the 3D pipeline—modeling, rigging, animation, \
simulation, rendering, compositing and motion tracking, even video editing \
and game creation.
Advanced users employ Blender’s API for Python scripting to customize the \
application and write specialized tools; often these are included in Blender’s \
future releases. Blender is well suited to individuals and small studios who \
benefit from its unified pipeline and responsive development process."
HOMEPAGE="https://www.blender.org/"
COPYRIGHT="2002-2018 Blender Foundation"
LICENSE="GNU GPL v2"
REVISION="27"
SOURCE_URI="https://download.blender.org/source/blender-$portVersion.tar.gz"
CHECKSUM_SHA256="4c944c304a49e68ac687ea06f5758204def049b66dc211e1cffa1857716393bc"
PATCHES="
	blender-$portVersion.patchset
	"
ADDITIONAL_FILES="
	blender.rdef.in
	Blender.sh
	"

ARCHITECTURES="all !x86_gcc2"
SECONDARY_ARCHITECTURES="x86"

PYTHON_VERSION="3.9"

PROVIDES="
	blender$secondaryArchSuffix = $portVersion
	app:blender
	cmd:blender_thumbnailer.py${secondaryArchSuffix}
	"
REQUIRES="
	haiku$secondaryArchSuffix
#	numpy${secondaryArchSuffix}_python3
#	requests_python3
	lib:libalembic$secondaryArchSuffix
	lib:libavcodec$secondaryArchSuffix
	lib:libavdevice$secondaryArchSuffix
	lib:libboost_atomic$secondaryArchSuffix
	lib:libboost_chrono$secondaryArchSuffix
	lib:libboost_date_time$secondaryArchSuffix
	lib:libboost_filesystem$secondaryArchSuffix
	lib:libboost_locale$secondaryArchSuffix
	lib:libboost_regex$secondaryArchSuffix
	lib:libboost_system$secondaryArchSuffix
	lib:libboost_thread$secondaryArchSuffix
	lib:libexecinfo$secondaryArchSuffix
	lib:libfftw3$secondaryArchSuffix
	lib:libfreetype$secondaryArchSuffix
	lib:libgcc_s$secondaryArchSuffix
	lib:libgl$secondaryArchSuffix
	lib:libglew$secondaryArchSuffix
	lib:libglu$secondaryArchSuffix
	lib:libgomp$secondaryArchSuffix
	lib:libhdf5$secondaryArchSuffix
	lib:libiconv$secondaryArchSuffix
	lib:libicui18n$secondaryArchSuffix
	lib:libicuuc$secondaryArchSuffix
	lib:libiex_3_0$secondaryArchSuffix
	lib:libilmthread_3_0$secondaryArchSuffix
	lib:libimath_3_0$secondaryArchSuffix
	lib:libLLVM_9$secondaryArchSuffix
	lib:liblzma$secondaryArchSuffix
	lib:liblzo2$secondaryArchSuffix
	lib:libopenal$secondaryArchSuffix
	lib:libopencolladabaseutils$secondaryArchSuffix
	lib:libopencolorio$secondaryArchSuffix
	lib:libopenimageio$secondaryArchSuffix
#	lib:libopenvdb$secondaryArchSuffix
	lib:libosdCPU$secondaryArchSuffix
	lib:libosdGPU$secondaryArchSuffix
#	lib:liboslcomp$secondaryArchSuffix
	lib:libpcre$secondaryArchSuffix
	lib:libpng16$secondaryArchSuffix
	lib:libpugixml$secondaryArchSuffix
	lib:libpython${PYTHON_VERSION}$secondaryArchSuffix
	lib:libsdl2_2.0$secondaryArchSuffix
	lib:libsndfile$secondaryArchSuffix
	lib:libtbb$secondaryArchSuffix
	lib:libtiff$secondaryArchSuffix
	lib:libtinyxml$secondaryArchSuffix
	lib:libturbojpeg$secondaryArchSuffix
	lib:libxml2$secondaryArchSuffix
	lib:libyaml_cpp$secondaryArchSuffix
	lib:libz$secondaryArchSuffix
	"

BUILD_REQUIRES="
	haiku${secondaryArchSuffix}_devel
	devel:eigen$secondaryArchSuffix
	devel:libalembic$secondaryArchSuffix
	devel:libavcodec$secondaryArchSuffix
	devel:libavdevice$secondaryArchSuffix
	devel:libboost_atomic$secondaryArchSuffix >= 1.69.0
	devel:libboost_chrono$secondaryArchSuffix >= 1.69.0
	devel:libboost_date_time$secondaryArchSuffix >= 1.69.0
	devel:libboost_filesystem$secondaryArchSuffix >= 1.69.0
	devel:libboost_locale$secondaryArchSuffix >= 1.69.0
	devel:libboost_regex$secondaryArchSuffix >= 1.69.0
	devel:libboost_system$secondaryArchSuffix >= 1.69.0
	devel:libboost_thread$secondaryArchSuffix >= 1.69.0
	devel:libexecinfo$secondaryArchSuffix
	devel:libfftw3$secondaryArchSuffix
	devel:libfreetype$secondaryArchSuffix
	devel:libgl$secondaryArchSuffix
	devel:libglew$secondaryArchSuffix
	devel:libglu$secondaryArchSuffix
	devel:libgomp$secondaryArchSuffix
	devel:libhdf5$secondaryArchSuffix
	devel:libiconv$secondaryArchSuffix
	devel:libiex_3_0$secondaryArchSuffix
	devel:libilmthread_3_0$secondaryArchSuffix
	lib:libimath_3_0$secondaryArchSuffix
	devel:libllvm_9$secondaryArchSuffix
	devel:liblzma$secondaryArchSuffix
	devel:liblzo2$secondaryArchSuffix
	devel:libopenal$secondaryArchSuffix
	devel:libopencolladabaseutils$secondaryArchSuffix
	devel:libopencolorio$secondaryArchSuffix
	devel:libopenimageio$secondaryArchSuffix
#	devel:libopenvdb$secondaryArchSuffix
	devel:libosdCPU$secondaryArchSuffix
	devel:libosdGPU$secondaryArchSuffix
#	devel:liboslcomp$secondaryArchSuffix
	devel:libpcre$secondaryArchSuffix
	devel:libpng16$secondaryArchSuffix
	devel:libpugixml$secondaryArchSuffix
	devel:libpython${PYTHON_VERSION}$secondaryArchSuffix
	devel:libsdl2_2.0$secondaryArchSuffix
	devel:libsndfile$secondaryArchSuffix
	devel:libtbb$secondaryArchSuffix
	devel:libtiff$secondaryArchSuffix
	devel:libtinyxml$secondaryArchSuffix
	devel:libturbojpeg$secondaryArchSuffix
	devel:libxml2$secondaryArchSuffix
	devel:libyaml_cpp$secondaryArchSuffix
	devel:libz$secondaryArchSuffix
	lib:libavdevice$secondaryArchSuffix # REQUIRED
	"
BUILD_PREREQUIRES="
#	numpy${secondaryArchSuffix}_python3
#	requests_python3
	cmd:cmake
	cmd:find
	cmd:gcc$secondaryArchSuffix
	cmd:ld$secondaryArchSuffix
	cmd:ninja
	cmd:python${PYTHON_VERSION}
	cmd:sed
	cmd:strip
	"


PATCH()
{
	find . -type f -name "*.py" | xargs sed -i -e 's,python3,python3.9,g'
}

BUILD()
{
	# IMPORTANT INFORMATION, PLEASE READ BEFORE CHANGE ANYTHING.

	# Please do not make any changes here if they not *REALLY* necessary.
	# Keep this list short and tidy.

	# Blender uses the following settings to produce the official releases:
	# https://github.com/blender/blender/blob/master/build_files/cmake/config/blender_release.cmake
	# This settings are used on Haiku too, but we had to override some settings.
	# We should follow their settings as closely as possible to make interoperability possible between diferent platforms.
	# In the recipe there are 3 group for the compiler flags:
	# - platform specific (required to build Blender on Haiku)
	# - bugous (these settings should work but they breaks build / functionality on Haiku, should be fixed)
	# - missing deps (required dependency is not (yet) available for Haiku, should be ported)
	# If a change required please put it in the correct category, if you are unsure, ask at HaikuPorts.

	INCLUDE_DIR="`finddir B_SYSTEM_HEADERS_DIRECTORY`${secondaryArchSubDir}"

	# Haiku specific settings
	BLENDER_PLATFORM_SPECIFIC="-DCMAKE_INSTALL_PREFIX:PATH=$appsDir/Blender \
		-DPYTHON_VERSION=${PYTHON_VERSION} \
		-DPYTHON_INCLUDE_DIR=`finddir B_SYSTEM_HEADERS_DIRECTORY`/python${PYTHON_VERSION}/ \
		-DPYTHON_LIBRARY=`finddir B_SYSTEM_LIB_DIRECTORY`${secondaryArchSubDir}/libpython${PYTHON_VERSION}.so.1.0 \
		-DPYTHON_EXECUTABLE=/bin/python${PYTHON_VERSION} \
		-DPYTHON_NUMPY_PATH=`finddir B_SYSTEM_LIB_DIRECTORY`/python${PYTHON_VERSION}/vendor-packages \
		-DPYTHON_REQUESTS_PATH=`finddir B_SYSTEM_LIB_DIRECTORY`/python${PYTHON_VERSION}/vendor-packages \
		-DOPENCOLLADA_COLLADABASEUTILS_INCLUDE_DIR=${INCLUDE_DIR}/opencollada/COLLADABaseUtils \
		-DOPENCOLLADA_COLLADAFRAMEWORK_INCLUDE_DIR=${INCLUDE_DIR}/opencollada/COLLADAFramework \
		-DOPENCOLLADA_COLLADASAXFRAMEWORKLOADER_INCLUDE_DIR=${INCLUDE_DIR}/opencollada/COLLADASaxFrameworkLoader \
		-DOPENCOLLADA_COLLADASTREAMWRITER_INCLUDE_DIR=${INCLUDE_DIR}/opencollada/COLLADAStreamWriter \
		-DOPENCOLLADA_GENERATEDSAXPARSER_INCLUDE_DIR=${INCLUDE_DIR}/opencollada/GeneratedSaxParser \
		-DOPENJPEG_INCLUDE_DIR=${INCLUDE_DIR} \
		-DOPENJPEG_LIBRARY=`finddir B_SYSTEM_LIB_DIRECTORY`${secondaryArchSubDir} \
		-DOPENSUBDIV_INCLUDE_DIR=${INCLUDE_DIR} \
		-DZLIB_LIBRARY_RELEASE=`finddir B_SYSTEM_LIB_DIRECTORY`${secondaryArchSubDir}/libz.so.1 \
		-DWITH_GHOST_SDL=ON \
		-DWITH_BUILDINFO=OFF \
		-DWITH_OPENCOLORIO=OFF \
		-DWITH_MOD_FLUID=OFF \
		-DWITH_PYTHON_INSTALL=OFF \
		-DWITH_PYTHON_INSTALL_NUMPY=OFF \
		-DWITH_PYTHON_INSTALL_REQUESTS=OFF \
		-DWITH_PYTHON_MODULE=OFF \
		-DWITH_PYTHON_SAFETY=OFF \
		-DCMAKE_SKIP_RPATH=ON \
		-Wno-dev"

	# Release settings breaks build on Haiku
	BLENDER_BUGOUS="-DWITH_CYCLES_LOGGING=OFF \
		-DWITH_GAMEENGINE_DECKLINK=OFF \
		-DWITH_GTESTS=OFF \
		-DWITH_SYSTEM_EIGEN3=ON \
		-DWITH_SYSTEM_LZO=ON"

	# Missing deps on Haiku
	BLENDER_MISSING="-DWITH_JACK=OFF \
		-DWITH_MEM_JEMALLOC=OFF \
		-DWITH_INPUT_NDOF=OFF"

	# Extra CXX Flags
	BLENDER_CXX="-std=c++11"

	mkdir -p build_haiku
	cd build_haiku

	cmake .. \
		-G Ninja \
		-DCMAKE_BUILD_TYPE=Release \
		-C ../build_files/cmake/config/blender_release.cmake \
		$BLENDER_PLATFORM_SPECIFIC \
		$BLENDER_BUGOUS \
		$BLENDER_MISSING \
		-DCMAKE_CXX_FLAGS="$BLENDER_CXX"

	ninja $jobArgs
}

INSTALL()
{
	cd build_haiku
	ninja install $jobArgs

	mv $appsDir/Blender/blender $appsDir/Blender/Blender

	local MAJOR="`echo "$portVersion" | cut -d. -f1`"
	local MIDDLE="`echo "$portVersion" | cut -d. -f2 | sed 's/[a-z]*//g'`"
	local MINOR="`echo 0`"
	sed \
		-e "s|@MAJOR@|$MAJOR|" \
		-e "s|@MIDDLE@|$MIDDLE|" \
		-e "s|@MINOR@|$MINOR|" \
		$portDir/additional-files/blender.rdef.in > blender.rdef

	addResourcesToBinaries blender.rdef \
		$appsDir/Blender/Blender

	# Startscript for windowed mode (WORKAROUND)
	cp $portDir/additional-files/Blender.sh $appsDir/Blender/
	chmod +x $appsDir/Blender/Blender.sh

	# Installing extra-attribs
	rc blender.rdef
	resattr -o $appsDir/Blender/Blender.sh \
		blender.rsrc

	cd $appsDir/Blender
	mkdir -p $docDir/blender

	mv GPL-license.txt GPL3-license.txt \
		LICENSE-bfont.ttf.txt jemalloc-license.txt \
		Python-license.txt copyright.txt readme.html \
		blender.svg ocio-license.txt LICENSE-bmonofont-i18n.ttf.txt \
		LICENSE-droidsans.ttf.txt $docDir/blender

	rm -rf blender.desktop

	addAppDeskbarSymlink $appsDir/Blender/Blender.sh Blender

	mkdir -p $binDir
	ln -s $appsDir/Blender/blender_thumbnailer.py \
		$binDir/blender_thumbnailer.py
}

TEST()
{
	cd build_haiku
	# The whole testcase is around 1GB and not included. Get it from here:
	# svn checkout https://svn.blender.org/svnroot/bf-blender/trunk/lib/tests
	# and put it in "work-2.xx/sources/lib"
	make test
}
