#!/bin/bash

#
# This script runs on the target machine. First it installs ruby if
# necessary, then it runs teleport.
#

# bail on errors
set -eu

#
# Some package history:
#
# Ruby 1.8.7
#
# 10.04 (lucid):
#   libopenssl-ruby libreadline-ruby irb rdoc ri ruby ruby-dev
# 11.04 (maverick):
#   libopenssl-ruby/libreadline-ruby/irb now provided by libruby
#   rdoc now provided by ruby
#   we can still use the same package list, though. harmless.
# 13.04 (raring):
#   the default ruby package is now 1.9.1 (1.9.3)
#   we now use ruby1.8, which includes everything
#
#
# Ruby 1.9.3
#
# 12.04 (precise):
#   ruby1.9.3 package is now available



#
# functions
#

function banner() {
  printf '\e[1;37;43m[%s] %-72s\e[0m\n' `date '+%H:%M:%S'` "run.sh: $1"
}

function fatal() {
  printf '\e[1;37;41m[%s] %-72s\e[0m\n' `date '+%H:%M:%S'` "run.sh: error - $1"
  exit 1
}

function install_ruby() {
  banner "apt-get update / upgrade..."
  apt-get update
  apt-get -y upgrade

  # which version of readline to install?
  local readline
  if [ "${DISTRIB_RELEASE//[.]/}" -lt "1110" ] ; then
    readline=libreadline5-dev
  else
    readline=libreadline-gplv2-dev
  fi
  apt-get -y install wget $readline

  banner "installing Ruby $CONFIG_RUBY..."
  case $CONFIG_RUBY in
    1.8.7 ) install_ruby_187 ;;
    1.9.2 ) install_ruby_192 ;;
    1.9.3 ) install_ruby_193 ;;
    2.0.0 ) install_ruby_200 ;;
    REE )   install_ruby_ree ;;
	* )     fatal "unknown ruby ($CONFIG_RUBY)" ;;
  esac
}

function install_ruby_187() {
  if [ "${DISTRIB_RELEASE//[.]/}" -lt "1304" ] ; then
    apt-get -y install irb libopenssl-ruby libreadline-ruby rdoc ri ruby ruby-dev
  else
    apt-get -y install ruby1.8
  fi
  install_rubygems
}

function install_rubygems() {
  wget http://production.cf.rubygems.org/rubygems/rubygems-$CONFIG_RUBYGEMS.tgz
  tar xfpz rubygems-$CONFIG_RUBYGEMS.tgz
  (cd rubygems-$CONFIG_RUBYGEMS ; ruby setup.rb)
  if [ ! -f /usr/bin/gem ] ; then
    ln -s /usr/bin/gem1.8 /usr/bin/gem
  fi
}

#
# thanks to http://blog.statuspage.io/moving-to-ruby-2-on-mac-rvm-and-ubuntu-12-04/
# for suggestions
#

function install_ruby_20_requirements() {
  apt-get -y install build-essential checkinstall libffi-dev libncurses5-dev libssl-dev libyaml-dev zlib1g-dev
}

function install_ruby_200() {
  local patch=p247

  install_ruby_20_requirements

  wget http://ftp.ruby-lang.org/pub/ruby/2.0/ruby-2.0.0-$patch.tar.gz
  tar xvzf ruby-2.0.0-$patch.tar.gz

  cd ruby-2.0.0-$patch
  ./configure --prefix=/usr/local \
              --program-suffix=200 \
              --with-ruby-version=2.0.0 \
              --disable-install-doc
  make
  checkinstall -D -y \
                    --nodoc \
                    --dpkgflags=--force-overwrite \
                    --pkgname="ruby2.0.0" \
                    --pkgversion="2.0.0-$patch" \
                    --provides="ruby200"
  cd ..

  update-alternatives --install /usr/local/bin/ruby ruby /usr/local/bin/ruby200 200 \
                      --slave   /usr/local/bin/ri   ri   /usr/local/bin/ri200 \
                      --slave   /usr/local/bin/irb  irb  /usr/local/bin/irb200 \
                      --slave   /usr/local/bin/gem  gem  /usr/local/bin/gem200 \
                      --slave   /usr/local/bin/erb  erb  /usr/local/bin/erb200 \
                      --slave   /usr/local/bin/rdoc rdoc /usr/local/bin/rdoc200
}

#
# thanks to http://threebrothers.org/brendan/blog/ruby-1-9-2-on-ubuntu-11-04/
# for suggestions
#

function install_ruby_19_requirements() {
  apt-get -y install bison build-essential checkinstall libffi-dev libssl-dev libyaml-dev zlib1g-dev
}

function install_ruby_192() {
  local patch=p320

  install_ruby_19_requirements

  wget http://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.2-$patch.tar.gz
  tar xvzf ruby-1.9.2-$patch.tar.gz

  cd ruby-1.9.2-$patch
  ./configure --prefix=/usr/local \
              --program-suffix=192 \
              --with-ruby-version=1.9.2 \
              --disable-install-doc
  make
  checkinstall -D -y \
                    --fstrans=no \
                    --nodoc \
                    --dpkgflags=--force-overwrite \
                    --pkgname="ruby1.9.2" \
                    --pkgversion="1.9.2-$patch" \
                    --provides="ruby"
  cd ..

  update-alternatives --install /usr/local/bin/ruby ruby /usr/local/bin/ruby192 192 \
                      --slave   /usr/local/bin/ri   ri   /usr/local/bin/ri192 \
                      --slave   /usr/local/bin/irb  irb  /usr/local/bin/irb192 \
                      --slave   /usr/local/bin/gem  gem  /usr/local/bin/gem192 \
                      --slave   /usr/local/bin/erb  erb  /usr/local/bin/erb192 \
                      --slave   /usr/local/bin/rdoc rdoc /usr/local/bin/rdoc192
}

function install_ruby_193_src() {
  local patch=p448

  install_ruby_19_requirements

  wget http://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.3-$patch.tar.gz
  tar xvzf ruby-1.9.3-$patch.tar.gz

  cd ruby-1.9.3-$patch
  ./configure --prefix=/usr/local \
              --program-suffix=193 \
              --with-ruby-version=1.9.3 \
              --disable-install-doc
  make
  checkinstall -D -y \
                    --nodoc \
                    --dpkgflags=--force-overwrite \
                    --pkgname="ruby1.9.3" \
                    --pkgversion="1.9.3-$patch" \
                    --provides="ruby"
  cd ..

  update-alternatives --install /usr/local/bin/ruby ruby /usr/local/bin/ruby193 193 \
                      --slave   /usr/local/bin/ri   ri   /usr/local/bin/ri193 \
                      --slave   /usr/local/bin/irb  irb  /usr/local/bin/irb193 \
                      --slave   /usr/local/bin/gem  gem  /usr/local/bin/gem193 \
                      --slave   /usr/local/bin/erb  erb  /usr/local/bin/erb193 \
                      --slave   /usr/local/bin/rdoc rdoc /usr/local/bin/rdoc193
}

function install_ruby_193_pkg() {
  apt-get -y install ruby1.9.3

  # Update rubygems to the latest version. The package version uses
  # the /var/lib gem path, and the updated version switches to
  # /usr/lib. Better do this now before any gems are installed,
  # otherwise they'll break later!
  REALLY_GEM_UPDATE_SYSTEM=1 gem update --system
}

function install_ruby_193() {
  if [ "${DISTRIB_RELEASE//[.]/}" -lt "1204" ] ; then
    install_ruby_193_src
  else
    install_ruby_193_pkg
  fi
}

function install_ruby_ree() {
  local ree="ruby-enterprise_1.8.7-2012.02_${ARCH}_ubuntu10.04.deb"

  # this is necessary on 12.04 (thanks noeticpenguin)
  apt-get -y install libssl0.9.8

  wget http://rubyenterpriseedition.googlecode.com/files/$ree
  dpkg -i $ree

  # remove all gems
  gem list | cut -d" " -f1 | xargs gem uninstall
}


#
# main
#

# are we on Ubuntu?
if ! grep -q Ubuntu /etc/lsb-release ; then
  fatal "Teleport only works with Ubuntu"
fi

# which version?
. /etc/lsb-release
case $DISTRIB_RELEASE in
  10.* | 11.* | 12.* | 13.04 ) ;; # nop
  *)
    banner "warning - Ubuntu $DISTRIB_RELEASE hasn't been tested with Teleport yet"
esac

# which architecture?
if [ $(uname -m) == "x86_64" ] ; then
  ARCH=amd64
else
  ARCH=i386
fi

# read our config
cd /tmp/_teleported
source ./config

# do we need to install ruby?
if ! which ruby > /dev/null; then
  install_ruby
elif ! [[ `ruby -v` =~ "$CONFIG_RUBY" ]]; then
  # installed ruby version does not matched the one configured
  if [[ 'true' = "$CONFIG_UPGRADE" ]] ; then
    install_ruby
  else
    banner "warning - Use the --upgrade argument to upgrade to Ruby $CONFIG_RUBY"
  fi
fi

# do we need to get rid of the apt rubygems package?
if dpkg-query -f='${Status}' -W rubygems1.8 2>&1 | grep 'install ok installed' ; then
  banner "Installing rubygems from source..."
  apt-get -y install wget
  apt-get -y remove rubygems1.8
  install_rubygems
fi

# run teleport!
ruby -I gem -r teleport -e "Teleport::Main.new(:install)"
