#!/bin/bash

function rake_assets_precompile() {
  [[ "$DISABLE_ASSET_COMPILATION" == "true" ]] && return
  [ ! -f Gemfile ] && return
  [ ! -f Rakefile ] && return
  ! grep " rake " Gemfile.lock >/dev/null && return
  ! bundle exec 'rake -T' | grep "assets:precompile" >/dev/null && return

  echo "---> Starting asset compilation ..."
  bundle exec rake assets:precompile
}

set -e

export RACK_ENV=${RACK_ENV:-"production"}

if [ -n "$RUBYGEM_MIRROR" ]; then
  bundle config mirror.https://rubygems.org $RUBYGEM_MIRROR
fi

shopt -s dotglob
echo "---> Installing application source ..."
mv /tmp/src/* ./

echo "---> Building your Ruby application from source ..."
if [ -f Gemfile ]; then
  ADDTL_BUNDLE_ARGS="--retry 2"
  if [ -f Gemfile.lock ]; then
    ADDTL_BUNDLE_ARGS+=" --deployment"
  fi

  if [[ "$RAILS_ENV" == "development" || "$RACK_ENV" == "development" ]]; then
    BUNDLE_WITHOUT=${BUNDLE_WITHOUT:-"test"}
  elif [[ "$RAILS_ENV" == "test" || "$RACK_ENV" == "test" ]]; then
    BUNDLE_WITHOUT=${BUNDLE_WITHOUT:-"development"}
  else
    BUNDLE_WITHOUT=${BUNDLE_WITHOUT:-"development:test"}
  fi
  
  if [ -n "$BUNDLE_WITHOUT" ]; then
    ADDTL_BUNDLE_ARGS+=" --without $BUNDLE_WITHOUT"
  fi
  
  echo "---> Running 'bundle install ${ADDTL_BUNDLE_ARGS}' ..."
  bundle install --path ./bundle ${ADDTL_BUNDLE_ARGS}

  echo "---> Cleaning up unused ruby gems ..."
  bundle clean -V
fi

if ! bundle exec rackup -h &>/dev/null; then
  echo "WARNING: Rubygem Rack is not installed in the present image."
  echo "         Add rack to your Gemfile in order to start the web server."
fi

if [[ "$RAILS_ENV" == "production" || "$RACK_ENV" == "production" ]]; then
  rake_assets_precompile
fi

# Fix source directory permissions
fix-permissions ./

# Make the ./tmp folder world writeable as Rails or other frameworks might use
# it to store temporary data (uploads/cache/sessions/etcd).
# The ./db folder has to be writeable as well because when Rails complete the
# migration it writes the schema version into ./db/schema.db
set +e
[[ -d ./tmp ]] && chgrp -R 0 ./tmp && chmod -R g+rw ./tmp
[[ -d ./db ]] && chgrp -R 0 ./db && chmod -R g+rw ./db
set -e
