#!/bin/bash

set -eo pipefail

scripts="$(dirname $0)"

export RAILS_HOST_NAME=build.hitobito.com

gem install bundler -v 1.17.3

if [ -d /tmp/artifacts/bundle ]; then
    echo "Restoring bundle from artifacts"
    mv /tmp/artifacts/bundle /tmp/src/bundle
fi

if [[ "$INTEGRATION_BUILD" == 1 ]]; then
    "$scripts/pull-composition-heads"
fi

"$scripts/prepare-source"


# After moving to ruby-25-centos7 we end up with a .bundle directory in pwd
# which we need to remove for the original builder script to execute correctly
if [[ -d '.bundle' ]]; then
  echo "Removing .bundle directory from `pwd`"
  rm -rf .bundle
fi

# /opt/app-root/src must be empty, otherwise assemble fails
if [[ -d './bin' ]]; then
  echo "Removing bin directory from `pwd`"
  rm -rf ./bin
fi

# Call original builder script
"$scripts/assemble-base"

if [[ "$INTEGRATION_BUILD" == 1 ]]; then
    "$scripts/pull-push-transifex-translations"
fi

BUILD_DATE=$(date '+%Y-%m-%d %H:%M:%S')
echo "(built at: $BUILD_DATE)" > /opt/app-root/src/BUILD_INFO
