#!/bin/bash

##########################################################################
# Copyright 2014 ThoughtWorks, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##########################################################################

set -e
GO_ROOT=$(cd $(dirname $0)/../.. > /dev/null && pwd)
echo $GO_ROOT

# installer and cleanup any pending gems
cd server/webapp/WEB-INF/rails.new
rm -rf bin/* .bundle/config

# first install all gems using bundler
$GO_ROOT/tools/bin/jruby -S bundle install --path vendor/bundle --binstubs --clean

# then install bundler afterwards, because, well bundler uninstalls itself
$GO_ROOT/tools/bin/jruby -S gem install bundler --install-dir vendor/bundle/jruby/1.9 --version 1.9.9 --bindir bin

cd $GO_ROOT
git checkout server/webapp/WEB-INF/rails.new/bin/bundle
git checkout server/webapp/WEB-INF/rails.new/bin/rails
git checkout server/webapp/WEB-INF/rails.new/bin/rake
