#!/bin/sh
# Usage: script/travis [ruby-version [file]]
# Example: script/travis 2.0 test/failing_test.rb
# Example: script/travis 2.3.0
set -e

mkdir -p vendor/docker
docker rm -fv docker-travis > /dev/null 2>&1 || true
docker run --volume=$(pwd):/home/travis/builds/jekyll/jekyll \
  --workdir=/home/travis/builds/jekyll/jekyll \
  --volume=$(pwd)/vendor/docker:/home/travis/builds/jekyll/jekyll/vendor/bundle \
  --user=travis --name=docker-travis -dit quay.io/travisci/travis-ruby \
      bash > /dev/null

status=0
if [ $# -eq 2 ]; then
  docker exec -it docker-travis bash -ilc " \
    rvm use --install --binary --fuzzy $1
    bundle install --path vendor/bundle -j 12 \\
      --without benchmark:site:development
    bundle clean
    script/test $2
  " || status=$?

elif [ $# -eq 1 ]; then
  docker exec -it docker-travis bash -ilc " \
    rvm use --install --binary --fuzzy $1
    bundle install --path vendor/bundle -j 12 \\
      --without benchmark:site:development
    bundle clean
    bundle exec rake
  " || status=$?

else
  docker exec -it docker-travis \
    bash -il || status=$?
fi

docker rm -fv docker-travis > /dev/null
exit $status
