#!/usr/bin/env bash

system="$(uname)"

if [[ "Linux" == "$system" ]]
then
  for file in /etc/*-release
  do
    release="( $(cat $file) )"
    break
  done
  printf "\n\nRequirements for ${system} $release\n"

  rvm_apt_binary="$(command -v apt-get)"
  rvm_emerge_binary="$(command -v emerge)"
  rvm_pacman_binary="$(command -v pacman)"
  rvm_yum_binary="$(command -v yum)"
  rvm_zypper_binary="$(command -v zypper)"
  rvm_free_ram_mb="$(free -m | awk '{if (NR==3) print $4}')"

  printf "
NOTE: 'ruby' represents Matz's Ruby Interpreter (MRI) (1.8.X, 1.9.X)
             This is the *original* / standard Ruby Language Interpreter
      'ree'  represents Ruby Enterprise Edition
      'rbx'  represents Rubinius

bash >= 4.1 required
curl is required
git is required (>= 1.7 recommended)
patch is required (for ree and some ruby-head's).

To install rbx and/or Ruby 1.9 head (MRI) (eg. 1.9.2-head),
then you must install and use rvm 1.8.7 first.

NOTE: To all Multi-User installers - DO NOT forget to add your users to the 'rvm' group.
      The installer does not auto-add any users to the rvm group. Admins must do this.
      Group memberships are ONLY evaluated at login time. Log them out, then back in.
"

  if [[ ! -z "$rvm_apt_binary" ]]
  then
    printf "
Additional Dependencies:
# For Ruby / Ruby HEAD (MRI, Rubinius, & REE), install the following:
  ruby: ${rvm_apt_binary} install build-essential 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 ncurses-dev automake libtool bison subversion

# For JRuby, install the following:
  jruby: ${rvm_apt_binary} install curl g++ openjdk-6-jre-headless
  jruby-head: ${rvm_apt_binary} install ant openjdk-6-jdk

# For IronRuby, install the following:
  ironruby: ${rvm_apt_binary} install curl mono-2.0-devel
"

  elif [[ ! -z "$rvm_emerge_binary" ]]
  then
    printf "
Additional Dependencies:
# For Ruby / Ruby HEAD (MRI, Rubinius, & REE), install the following:
  ruby|ruby-head: emerge libiconv readline zlib openssl curl git libyaml sqlite libxslt libtool gcc autoconf automake bison m4

  # For JRuby, install the following:
  jruby: emerge dev-java/sun-jdk dev-java/sun-jre-bin

  # For IronRuby, install the following:
  ironruby: emerge dev-lang/mono
    "

  elif [[ ! -z "$rvm_pacman_binary" ]]
  then
    printf "
Additional Dependencies:
# For Ruby / Ruby HEAD (MRI, Rubinius, & REE), install the following:
  ruby: pacman -Sy --noconfirm gcc patch curl zlib readline libxml2 libxslt git autoconf diffutils make libtool bison subversion

  # For JRuby, install the following:
  jruby: pacman -Sy --noconfirm jdk jre curl

  # For IronRuby, install the following:
  ironruby: pacman -Sy --noconfirm mono
"

  elif [[ ! -z "$rvm_yum_binary" ]]
  then
    printf "
Additional Dependencies:
# For Ruby / Ruby HEAD (MRI, Rubinius, & REE), install the following:
  ruby: yum install -y gcc-c++ patch readline readline-devel zlib zlib-devel libyaml-devel libffi-devel openssl-devel make bzip2 autoconf automake libtool bison iconv-devel ## NOTE: For centos >= 5.4 iconv-devel is provided by glibc

  # For JRuby, install the following:
  jruby: yum install -y java
"

  elif [[ ! -z "$rvm_zypper_binary" ]]
  then
    printf "
Additional Dependencies:
# For Ruby / Ruby HEAD (MRI, Rubinius, & REE), install the following:
  ruby: sudo zypper install -y patterns-openSUSE-devel_basis gcc-c++ bzip2 readline-devel zlib-devel \\
                           libxml2-devel libxslt-devel libyaml-devel libopenssl-devel libffi45-devel \\
													 libtool bison

  # For JRuby, install the following:
  jruby: sudo zypper install -y java-1_6_0-sun # Non-Oss repository required
"

  else
    printf "
Additional Dependencies:
# For Ruby / Ruby HEAD (MRI, Rubinius, & REE), install the following:
  ruby: # gcc-c++ patch readline zlib libyaml iconv libxml2 libxslt libtool bison

  # For JRuby, install the following:
  jruby: # The SUN java runtime environment and development kit.

  # For IronRuby, install the following:
  ironruby: #The Mono Runtime and Development Platform (version 2.6 or greater is recommended).
"
  fi
elif [[ "$MACHTYPE" == *darwin* ]]
then

  release="( $(sw_vers -productName) )"

  printf "
  Notes for ${system} $release
    For Snow Leopard: Xcode Version 3.2.1 (1613) or later, such as 3.2.6 or Xcode 4.1.
    [ Please note that Xcode 3.x will *not* work on OS X Lion. The 'cross-over' is Xcode 4.1. ]

    You should download the Xcode tools from developer.apple.com, since the Snow Leopard dvd install contained bugs.
    You can find Xcode 4.1 for OS X Lion at:
    https://developer.apple.com/downloads/download.action?path=Developer_Tools/xcode_4.1_for_lion/xcode_4.1_for_lion.dmg

    ** Lion Users: Xcode Version 4.2.x for OS X Lion works only for ruby 1.9.3-p0 (or higher).
                   It currently fails to build several other rubies and gems, as well as several Homebrew and
                   Macports packages. Xcode Version 4.1 (4B110) works.

    For MacRuby: Install LLVM first.
    For JRuby:  Install the JDK. See http://developer.apple.com/java/download/  # Current Java version "1.6.0_26"
    For IronRuby: Install Mono >= 2.6

    To use an RVM installed Ruby as default, instead of the system ruby:
      rvm install 1.8.7 # installs patch 352: closest supported version
      rvm system ; rvm gemset export system.gems ; rvm 1.8.7 ; rvm gemset import system.gems # migrate your gems
      rvm --default 1.8.7

  "
  if (( UID == 0 ))
  then
	 printf "\n\r \033[32mRVM Group Membership Management\033[0m - With Multi-User installations, the RVM installer automatically
	 creates an 'rvm' group which, as the RVM documentation explains, administrators must add the users they wish
	 to let use the RVM installation to. The call is made in the installer as:\n
	     \033[32msudo dscl . -create /Groups/\$rvm_group_name gid \$gid\033[0m\n
	 wherein RVM creates the gid by checking for the last assigned gid and adding 1. To physicially add a user to
	 the group, administrators must use:\n
	     \033[32msudo dscl localhost -append /Local/Default/Groups/rvm GroupMembership \$user_name\033[0m\n
         To check on group membership to the RVM group, administrators would execute the following:\n
             \033[32mrvmsudo dscl localhost -read /Local/Default/Groups/rvm\033[0m\n
         Pay attention to the GroupMembership and PrimaryGroupID lines. This tells you who is in it, and the GID for the RVM group.
	 Afterwards, should administrators wish to remove users from the group, they would execute:\n
	     \033[32msudo dscl localhost -delete /Local/Default/Groups/rvm GroupMembership \$user_name\033[0m
	     \033[32msudo dsmemberutil flushcache\033[0m\n
	 This will keep the 'rvm' group, but remove the listed user from it. They can directly delete the rvm group with:\n
	     \033[32msudo dscl . -delete /Groups/rvm && sudo dsmemberutil flushcache\033[0m\n
	 without previously deleting users from the group, as well. This will completely remove the 'rvm' group from the system.
	 Please note, the call to 'dsmemberutil flushcache' is required on both removal of the user from the group, and/or
	 removal of the group directly because that membership is still cached locally until either reboot or sync with
	 Directory Services. Allowing the group membership to stay in the user's 'groups' output does not mean the user is
	 automatically re-added to the 'rvm' group should the group be subsequently be re-added. This means the user(s) end up
	 erroneously showing they are part of the 'rvm' group even though they actually are not, if the call to 'dsmemberutil'
	 is not made. By this we mean the 'groups' command will still show them a part of the 'rvm' group, even if the user
	 logs out and then back in, due to caching. This applies to Tiger, Leopard, Snow Leopard, and Lion. Previous versions of
	 the OS such as Cheetah/Puma, and Jaguar used 'nicl', a.k.a NetInfo, and not 'dscl'.\n\n"
  fi
fi
