#!/usr/bin/env bash

#
# Author: Wayne E. Seguin <wayneeseguin@gmail.com>
# Licence: MIT
#

#
# This script when sourced will bootstrap a Rails development environment
# on Linux and OSX
#

# Source this file so that it will leave you in the Sites directory.

rails_version="3.0.9"
ruby_version="1.9.1"
sites_path="$HOME/Sites"
abort=false

if (( UID == 0 ))
then
  printf "ERROR: This script may not be sourced as the root user."
  abort=true
else
  if [[ ! -d "$sites_path" ]]
  then
    mkdir -p "$sites_path"
  fi

  cd "$sites_path"

  printf "#\n#  Bootstrapping a Rails development environment!\n#\n"

  if [[ $MACHTYPE = *linux* ]]
  then
    printf "#\n#  Ensuring OS packges are installed, you will be prompted for your password.\n#\n"
    if command -v apt-get
    then
      sudo apt-get install build-essential bison openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev
    elif command -v pacman
    then
      sudo pacman -S --noconfirm gcc patch curl bison zlib readline libxml2 libxslt git autoconf diffutils patch make
    elif command -v yum
    then
      sudo yum install -y gcc-c++ patch readline readline-devel zlib zlib-devel libyaml-devel libffi-devel openssl-devel
      sudo yum install -y iconv-devel >/dev/null 2>&1 # NOTE: For centos 5.4 final iconv-devel might not be available :(
    fi
  elif [[ $MACHTYPE = *darwin* ]]
  then
    if [[ ! -s /Library/Developer/Shared/XcodeTools.plist ]]
    then
      printf "Please Install XCode Tools before sourcing this environment bootstrap script."
      abort=true
    fi
  fi
fi

if ! $abort
then
  printf "Ensuring that git is installed...\n"
  if command -v git
  then
    printf "Found git! Moving right along.\n"
  else
    printf "=> Installing Git (git command not found)"
    if curl -s -L -B https://rvm.io/install/git -o gitinstall
    then
      chmod +x "$PWD/gitinstall"
      sudo bash "$PWD/gitinstall"
      if [[ -f gitinstall ]]
      then
        rm -f gitinstall
      fi
    else
      printf "ERROR: There was an error while attempting to install git."
      exit 1
    fi
  fi
  printf "=> Installing RVM the Ruby enVironment Manager\n  https://rvm.io/rvm/install/\n"

  curl -s -O -L -B https://rvm.io/releases/rvm-install-head

  chmod +x rvm-install-head

  "$PWD/rvm-install-head"

  if [[ -f rvm-install-head ]]
  then
    rm -f rvm-install-head
  fi

  printf "=> Setting up RVM to load with new shells.\n"
  echo  '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"  # Load RVM into a shell session *as a function*' >> "$HOME/.bash_profile"

  printf "=> Loading RVM"

  source ~/.rvm/scripts/rvm

  printf "=> Installing Ruby 1.8.7\n  More information about installing rubies can be found at https://rvm.io/rubies/installing/"

  rvm install $ruby_version

  printf "=> Using 1.8.7 and setting it as default for new shells\n  More information about Rubies can be found at https://rvm.io/rubies/default/\n"

  rvm use $ruby_version --default

  printf "=> Installing Rails 3 to the default gemset.\n  More information about gemsets can be found at https://rvm.io/gemsets/\n"

  gem install rails --no-rdoc --no-ri

  printf "=> Installing Bundler to the global gemset.\n  https://rvm.io/gemsets/global/\n"

  rvm gemset use global

  gem install bundler --no-rdoc --no-ri

  rvm gemset clear

  printf "=> Installing the sqlite3 Gem.\n  https://rubydoc.info/gems/sqlite3/1.3.3/frames\n"

  gem install sqlite3 --no-rdoc --no-ri

  printf "
Rails development environment bootstrapped, please enjoy Rails!
  ~Wayne E. Seguin <wayneeseguin@gmail.com>

P.S. You should now be able to generate a new Rails Application in ~/Sites with the command 'rails new <name>'
"
fi

unset ruby_version rails_version sites_path

