#!/bin/bash
# Builds blender-legacy for testing
#
# Generated files/directories:
# ./build.log
# ./install.log
# ./build/*
# ./release/*

# Define paths
BUILD_PATH="$(realpath ./build)"
RELEASE_PATH="$(realpath ./release)"

# Create paths
mkdir -p "${BUILD_PATH}"
mkdir -p "${RELEASE_PATH}"

# Get python version
_pyver=$(python -c "from sys import version_info; print(\"%d.%d\" % (version_info[0],version_info[1]))")

# CMake
cmake -G "Ninja" -S "blender-2.79b" -B "${BUILD_PATH}" \
      -C "./blender-2.79b/build_files/cmake/config/blender_release.cmake" \
      -DCMAKE_INSTALL_PREFIX=/usr \
      -DCMAKE_BUILD_TYPE=Release \
      -DWITH_INSTALL_PORTABLE=OFF \
      -DWITH_GL_PROFILE_CORE=OFF \
      -DWITH_GL_PROFILE_ES20=ON \
      -DWITH_SYSTEM_GLEW=ON \
      -DWITH_PYTHON_INSTALL=OFF \
      -DPYTHON_VERSION="${_pyver}" \
      -DWITH_LLVM=ON \
      -DWITH_CODEC_FFMPEG=OFF \
      -DWITH_CYCLES=OFF \
      -DWITH_OPENCOLLADA=OFF \
      -DWITH_OPENCOLORIO=OFF \
      -DWITH_OPENVDB=OFF \
      -DWITH_OPENIMAGEIO=OFF \
      -DWITH_GAMEENGINE=OFF \
      -DWITH_PLAYER=OFF \
      -DWITH_PYTHON_MODULE=OFF \
      -DWITH_CYCLES_OSL=NO \
      -DWITH_CYCLES_CUDA_BINARIES=OFF

export NINJA_STATUS="[%p | %f<%r<%u | %cbps ] "
ninja -C "${BUILD_PATH}" 2>&1 | tee build.log;

if [[ $? -eq 0 ]]; then
  DESTDIR="${RELEASE_PATH}" ninja -C "${BUILD_PATH}" install | tee install.log;
fi

if [[ $? -eq 0 ]]; then
  echo ""
  echo "You may want to try blender with command:"
  echo "  BLENDER_SYSTEM_SCRIPTS=${RELEASE_PATH}/usr/2.79/scripts \\"
  echo "  BLENDER_SYSTEM_DATAFILES=${RELEASE_PATH}/usr/2.79/scripts \\"
  echo "  ${BUILD_PATH}/bin/blender"
fi


