#
#   Copyright 2012 Wade Alcorn wade@bindshell.net
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#

clear
echo "======================================"
echo "          BeEF Installer   "
echo "======================================"
echo ""

echo "CAUTION: This installation script will install a number of BeEF dependencies including the Ruby-RVM environemnt and it's dependencies." 
echo "" 
echo "In rare cases, this may lead to unexpected behaviour or package conflicts on some systems."
echo ""
read -p  "Are you sure you wish to continue (Y/n)? " 
if [ "`echo ${REPLY} | tr [:upper:] [:lower:]`" == "n" ] ; then
	 exit;
fi

echo ""
echo "Detecting OS..";


OS=`uname`

if [ "${OS}" = "Linux" ] ; then
    if [ -f /etc/redhat-release ] ; then
        Distro='RedHat'
    elif [ -f /etc/debian_version ] ; then
        Distro='Debian'
    fi
    readonly OS
    readonly Distro
fi

if [ "$OS" == "Darwin" ]; then
	echo "Mac OSX Detected"
	echo "Installing Ruby Version Manager (RVM) & Ruby 1.9.3.."
	bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer) 
	source ~/.bash_login
	rvm install 1.9.3-p0 --with-gcc=clang
	rvm use 1.9.3-p0
	echo ""
	echo "Downloading BeEF.."
	
	git clone git://github.com/beefproject/beef.git
	cd beef
	
	echo ""
	echo "Installing Ruby Gems.."
	bundle install
	OK="yes"
	
	./beef
	
	echo ""
	echo "=========================================="
	echo "          Install Complete"
	echo "Please restart Terminal and Run BeEF with:"
	echo "                  $ ./beef     "
	echo "=========================================="
	echo ""
		
fi

if [ "$Distro" == "Debian" ]; then
	echo "Debian/Ubuntu Detected"
	echo "Installing Prerequisite Packages.."
	sudo apt-get update 
	sudo apt-get install curl git



sudo apt-get install build-essential openssl libreadline6 libreadline6-dev zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev autoconf libc6-dev libncurses5-dev automake libtool bison subversion

bash < <(curl -sk https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)

echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"' >> ~/.bashrc

source ~/.bashrc
source $HOME/.rvm/scripts/rvm

	rvm install 1.9.2
	rvm use 1.9.2 --default

	echo "Downloading BeEF.."
	git clone git://github.com/beefproject/beef.git
	cd beef

	echo "Installing Ruby Gems"
	gem install bundler
	bundle install
	
	
	./beef

	OK="yes"
	echo ""
	echo "=========================================="
	echo "          Install Complete"
	echo "=========================================="
	echo ""
fi


if [ "$Distro" == "RedHat" ]; then
	echo "Redhat/Fedora Detected"
	echo "Installing Prerequisite Packages.."
	sudo yum install -y git make gcc openssl-devel gcc-c++ patch readline readline-devel zlib zlib-devel libyaml-devel libffi-devel bzip2 autoconf automake libtool bison iconv-devel sqlite-devel

	echo ""
	echo "Installing Ruby Version Manager (RVM) & Ruby 1.9.2"
	wget https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer
	bash ./rvm-installer
	source ~/.rvm/scripts/rvm
	rvm pkg install openssl
	rvm install 1.9.2 --with-openssl-dir=$rvm_path/usr
	source ~/.rvm/scripts/rvm
	rvm use 1.9.2 --default
	
	echo "Downloading BeEF.."
	git clone git://github.com/beefproject/beef.git
	cd beef
	
	gem install bundler
	bundle
	
	source ~/.bash_profile
	
	./beef

	OK="yes"
	echo ""
	echo "=========================================="
	echo "          Install Complete"
	echo "=========================================="
	echo ""
		
fi

if [ "$OK" == "yes" ]; then		
echo ""
else 
	echo ""
	echo "======================================="
	echo "          Install Failed"
	echo "Unable to locate installer for your OS:"
	echo $OS
	echo $Distro
	echo "======================================="
	echo ""
fi