#!/bin/bash
#
# RightScript: Install RightScale
#
# Author: Chris Fordham <chris.fordham@rightscale.com>
# Copyright (c) 2007-2008 by RightScale Inc., all rights reserved worldwide
#

#
# Set environment variables
#
export arch=`uname -m`
export rs_release='v4.3.7'
export cloud='ec2'

#
# Linux detection
#
if [ -e /usr/bin/lsb_release ]; then
  case `lsb_release -si` in
    Ubuntu*) export RS_DISTRO=ubuntu
             export RS_BASE_OS=debian
			 echo 'Ubuntu detected.'
             ;;
    Debian*) export RS_DISTRO=debian
             export RS_BASE_OS=debian
			 echo 'Debian detected.'
             ;;
    CentOS*) export RS_DISTRO=centos
             export RS_BASE_OS=redhat
			 echo 'CentOS detected.'
             ;;
    Fedora*) export RS_DISTRO=redhat
             export RS_BASE_OS=redhat
			 echo 'Fedora detected.'
             ;;
    *)       export RS_DISTRO=unknown
             export RS_BASE_OS=unknown
             ;;
  esac
fi
echo

#
# Test for a reboot,  if this is a reboot just skip this script.
#
if test "$RS_REBOOT" = "true" ; then
 echo "Skip Install RightScale on reboot."
 logger -t RightScale "Skip Install RightScale on reboot."
 exit 0 # Leave with a smile ...
fi

#
# Install RightScale Scripts
#
echo 'Installing RightScale Scripts...'

echo 'Fetching RightScale Scripts.'
curl -o /tmp/rightscale_scripts.tgz http://s3.amazonaws.com/rightscale_scripts/rightscale_scripts_"$rs_release".tgz

echo 'Extracting RightScale Scripts to /opt.'
tar -xzf /tmp/rightscale_scripts.tgz -C /opt/

echo 'Symlinking rightscale system service from /opt.'
ln -fv /opt/rightscale/etc/init.d/rightscale /etc/init.d/rightscale

#
# Setup rightscale daemon service
#
echo 'Set rightscale service as executable.'
chmod +x /opt/rightscale/etc/init.d/rightscale
chmod +x /etc/init.d/rightscale

echo 'Creating rightscale daemon include directory.'
mkdir -p /etc/rightscale.d

echo 'Enabling RightScale service.'
if [ $RS_DISTRO == 'ubuntu' ]; then
	
    # RightScale service must start after sshd, so ensure it's S30, not S20
    update-rc.d rightscale start 99 2 3 4 5 . stop 1 0 1 6 .

    #hack ruby 
    ln -sf /usr/bin/ruby1.8 /usr/bin/ruby

elif [ $RS_DISTRO == 'centos' ]; then
	
    chkconfig --add rightscale

elif [ $RS_DISTRO == 'debian' ]; then
	
	#update-rc.d rightscale defaults
	# RightScale service must start after sshd, so ensure it's S30, not S20
    update-rc.d rightscale start 99 2 3 4 5 . stop 1 0 1 6 .

else

  echo "distribution not supported at this time: $RS_DISTRO"
  echo "exiting..."
  exit 1

fi

echo 'Symlinking RightScale MOTD.'
ln -fv /opt/rightscale/etc/motd /etc/motd
echo 'Setting RightScale release.'
echo $rs_release > /etc/rightscale-release

#
# Install RubyGems
#
echo 'Installing RubyGems...'
echo 'Fetching RubyGems.'
wget -O /tmp/rubygems.tgz http://rubyforge.org/frs/download.php/45905/rubygems-1.3.1.tgz

echo 'Extracting RubyGems.'
mkdir -p /tmp/rubygems
tar -xzvf /tmp/rubygems.tgz  -C /tmp/rubygems

# Symlink gem script
if [ $RS_DISTRO == 'ubuntu' ]; then ln -sv /usr/bin/gem1.8 /usr/bin/gem ;fi
if [ $RS_DISTRO == 'debian' ]; then ln -sv /usr/bin/gem1.8 /usr/bin/gem ;fi

# Create gem install wrapper script in /tmp
cat << EOF >> /tmp/rubygems_install.sh
#!/bin/bash -exe
cd /tmp/rubygems/rubygems-1.3.1/
ruby setup.rb --no-rdoc --no-ri
gem source -a http://ec2-us-east-mirror.rightscale.com/rubygems/archive/latest/
gem source -r http://mirror.rightscale.com
gem install --no-rdoc --no-ri xml-simple net-ssh net-sftp s3sync 
updatedb
EOF

echo 'Running RubyGems install script.'
chmod +x /tmp/rubygems_install.sh && /tmp/rubygems_install.sh && echo 'Removing RubyGems install script.' && rm /tmp/rubygems_install.sh

echo 'Setting Cloud.'
echo $cloud > /etc/rightscale.d/cloud

echo 'Done.'