#!/bin/sh

set -e
BASE_PATH=$(pwd)
CPU=$(uname -m)

BLENDER_DIR=${BASE_PATH}/blender
DEPS_BUILD=${BASE_PATH}/build_linux_${CPU}_deps
DEPS_SRC=${BASE_PATH}/blender-build/build_environment
DEPS_INSTALL=${BASE_PATH}/lib/lib_${CPU}
BLENDER_SRC=${BLENDER_DIR}
BLENDER_BUILD=${BASE_PATH}/build_linux_${CPU}

cd ${BASE_PATH}/blender
#sed -e 's/set(OPENAL_HASH .*/set(OPENAL_HASH a4922a79526c590b6cac0c10f3f1bef8)/g' -i build_files/build_environment/cmake/versions.cmake
##       sed -e 's/set(LIBGLU_HASH .*/set(LIBGLU_HASH d41d8cd98f00b204e9800998ecf8427e)/g' -i build_files/build_environment/cmake/versions.cmake
#sed -e 's@set(OPENAL_URI .*@set(OPENAL_URI https://github.com/kcat/openal-soft/archive/refs/tags/${OPENAL_VERSION}.tar.gz)@g' -i build_files/build_environment/cmake/versions.cmake
#sed -e 's@set(OPENAL_FILE .*@set(OPENAL_FILE openal-soft-${OPENAL_VERSION}.tar.gz)@g' -i build_files/build_environment/cmake/versions.cmake
#sed -e 's@set(LIBGLU_URI .*@set(LIBGLU_URI https://archive.mesa3d.org/glu/glu-${LIBGLU_VERSION}.tar.xz)@g' -i build_files/build_environment/cmake/versions.cmake
#sed -e 's@set(MESA_URI .*@set(MESA_URI https://archive.mesa3d.org/older-versions/21.x/mesa-${MESA_VERSION}.tar.xz)@g' -i build_files/build_environment/cmake/versions.cmake
#sed -e 's@set(FRIBIDI_URI .*@set(FRIBIDI_URI https://github.com/fribidi/fribidi/archive/refs/tags/${FRIBIDI_VERSION}.tar.gz)@g' -i build_files/build_environment/cmake/versions.cmake
#sed -e 's@set(FRIBIDI_FILE .*@set(FRIBIDI_FILE fribidi-${FRIBIDI_VERSION}.tar.gz)@g' -i build_files/build_environment/cmake/versions.cmake
ARM_DEFS=
if [ x$CPU = xaarch64 ]; then
ARM_DEFS="-DBLENDER_PLATFORM_ARM=ON"
python3 ./build_files/utils/make_update.py --architecture arm64
else
python3 ./build_files/utils/make_update.py
fi
cd ${BASE_PATH}

cmake -S ${DEPS_SRC} -B ${DEPS_BUILD} \
	-DHARVEST_TARGET=${DEPS_INSTALL} \
	${ARM_DEFS}
export VERBOSE=1
cd ${DEPS_BUILD} && make install && cd ${BASE_PATH}

cmake -S ${BLENDER_SRC} -B ${BLENDER_BUILD} \
	-C${BLENDER_DIR}/build_files/cmake/config/blender_release.cmake \
	${ARM_DEFS} \
	-DWITH_CYCLES_EMBREE=OFF \
	-DLIBDIR=${BASE_PATH}/lib/lib_${CPU}
export VERBOSE=1
cd ${BLENDER_BUILD} && make && make install && \
	make package_archive && cd ${BASE_PATH}
mkdir -p prebuilts
cp ./build_linux_${CPU}/release/Blender-3.6-unknown-linux-${CPU}.tar.xz ./prebuilts
git lfs track --filename ./prebuilts/Blender-3.6-unknown-linux-${CPU}.tar.xz
git add ./prebuilts/Blender-3.6-unknown-linux-${CPU}.tar.xz

