#!/bin/bash

################################################################################
#
# Prepares benchmark bundle for Linux.
#
# This is so called "works for me (tm)".
#
#
# The following settings are to be passed via config file in this directory
# or via environment variable:
#
# - STORAGE_DIRECTORY
#
#   This is directory where persistent "temporary" files will be stored.
#   For example, this is where benchmark repository is being checked out.
#
################################################################################

SCRIPT=$(readlink -f $0)
SCRIPTPATH=`dirname $SCRIPT`

CONFIG="${SCRIPTPATH}/config"

if [ ! -f ${CONFIG} ]; then
  echo "Configuration is not provided, will expect all settings to be"
  echo "passed via environment variables."
else
  . "${CONFIG}"
fi

################################################################################
# Initialization and sanity checks.

SVN_BENCHMARK_URL="https://svn.blender.org/svnroot/bf-blender/trunk/lib/benchmarks/cycles/"
SVN_BENCHMARK_CHECKOUT_DIRECTORY="${STORAGE_DIRECTORY}/benchmark_scenes"
BUNDLE_DIRECTORY="${STORAGE_DIRECTORY}/blender-benchmark"
SCENES="barbershop_interior bmw27 classroom fishy_cat koro pavillon_barcelona victor"
IMAGE_WIDTH=1600  # 800*2
IMAGE_HEIGHT=740  # 370*2

SVN=`which svn`
if [ -z "${SVN}" ]; then
  echo "ERROR: Subversion is not found, can not continue."
  exit 1
fi

CONVERT=`which convert`
if [ -z "${CONVERT}" ]; then
  echo "ERROR: ImageMagic's convert is not found, can not continue."
  exit 1
fi

# Make sure storage directory exists.
if [ -z ${STORAGE_DIRECTORY} ]; then
  echo "ERROR: STORAGE_DIRECTORY is not specified, can not continue."
  exit 1
fi
mkdir -p "${STORAGE_DIRECTORY}"

if [ -z "${BLENDER_VERSION}" ]; then
  echo ""
fi

################################################################################
# Check out benchmark repository.


if [ -d "${SVN_BENCHMARK_CHECKOUT_DIRECTORY}" ]; then
  echo "Updating benchmark scenes..."
  ${SVN} up "${SVN_BENCHMARK_CHECKOUT_DIRECTORY}"
else
  echo "Making a new checkout of scenes..."
  ${SVN} checkout "${SVN_BENCHMARK_URL}" "${SVN_BENCHMARK_CHECKOUT_DIRECTORY}"
fi

################################################################################
# Package the bundle.

# We always start from scratch.
rm -rf "${BUNDLE_DIRECTORY}"
mkdir -p "${BUNDLE_DIRECTORY}"

# Create background to stack image on.
BACKGROUND_IMAGE="${STORAGE_DIRECTORY}/background.png"
${CONVERT} -size ${IMAGE_WIDTH}x${IMAGE_HEIGHT} xc:'rgb(51,51,51)' ${BACKGROUND_IMAGE}

# Copy scenes.
mkdir -p "${BUNDLE_DIRECTORY}/scenes"
echo "Bundling scenes..."
for scene in ${SCENES}; do
  echo "Bundling scene ${scene}..."
  cp -r "${SVN_BENCHMARK_CHECKOUT_DIRECTORY}/${scene}" "${BUNDLE_DIRECTORY}/scenes/${scene}"
  # Leave single scene only.
  rm -f "${BUNDLE_DIRECTORY}/scenes/${scene}/${scene}_gpu.blend"
  mv "${BUNDLE_DIRECTORY}/scenes/${scene}/${scene}_cpu.blend" "${BUNDLE_DIRECTORY}/scenes/${scene}/${scene}.blend"
  # Tweak image.
  if true; then
    input="${BUNDLE_DIRECTORY}/scenes/${scene}/${scene}.png"
    input_we=`basename "${input%.*}"`
    output_scaled="${STORAGE_DIRECTORY}/${input_we}_scaled.png"
    output_compo="${STORAGE_DIRECTORY}/${input_we}.png"
    ${CONVERT} "${input}" -resize ${IMAGE_WIDTH}x${IMAGE_HEIGHT} "${output_scaled}"
    ${CONVERT} ${BACKGROUND_IMAGE} "${output_scaled}" -gravity center -composite  "${output_compo}"
    mv "${output_compo}" "${input}"
    rm ${output_scaled}
  fi
done
