#!/bin/bash
# OPENi Cloudlet Platform Development Environment Setup v1.0
# Authors: David Benson (dbenson@tssg.org), Dylan Conway (dconway@tssg.org)

# The purpose of this script is to setup a linux development environment
# for the OPENi Cloudlet Platform.
echo "Beginning OPENi Cloudlet Platform Development Environment Setup......."
echo "This script is designed to work on systems using aptitude."
sudo apt-get autoremove -qy

function install_git {
    # Install Git
    echo "Installing Git....."
    sudo apt-get install -qy git
}

function install_node {
    # Download Node.js (v0.10.21)
    echo "Installing Node.js (v0.10.21)....."
    cd ~/Downloads
    sudo apt-get install -qy g++ curl libssl-dev apache2-utils
    wget http://nodejs.org/dist/v0.10.21/node-v0.10.21.tar.gz
    tar -xzvf node-v0.10.21.tar.gz
    # Cleanup the tarball file.
    rm node-v0.10.19.tar.gz
    # Install Node
    cd node-v0.10.21/
    ./configure
    make
    sudo make install
    rm -rf ../node-v0.10.21/
}

function install_zmq {
    # Download ZeroMQ libraries (v3.2.4)
    echo "Installing ZeroMQ libraries (v3.2.4)....."
    cd ~/Downloads
    sudo apt-get install -qy g++ uuid-dev binutils libtool autoconf automake
    wget http://download.zeromq.org/zeromq-3.2.4.tar.gz
    tar -xzvf zeromq-3.2.4.tar.gz
    # Cleanup the tarball file.
    rm zeromq-3.2.4.tar.gz
    # Install ZMQ
    cd zeromq-3.2.4
    ./configure
    make
    sudo make install
    sudo ldconfig
    rm -rf ../zeromq-3.2.4/
}

function install_sqlite3 {
    # Install SQLite3 + the developer library.
    echo "Installing SQLite3 and the developer library....."
    sudo apt-get install -qy sqlite3
    sudo apt-get install -qy libsqlite3-dev
}

function install_mongrel2 {
    # Download Mongrel2
    echo "Installing Mongrel2....."
    cd ~/Downloads
    wget https://github.com/zedshaw/mongrel2/tarball/v1.8.0
    tar -xzvf v1.8.0
    # Clean up tarball file.
    rm v1.8.0
    # Install Mongrel2
    cd zedshaw-mongrel2-bc721eb/
    # no ./configure
    make
    sudo make install
    rm -rf ../zedshaw-mongrel2-bc721eb/
}

function install_chef {
    # Install Chef
    curl -L -k https://www.opscode.com/chef/install.sh | sudo bash
}

if install_git 2>/dev/null ; then
    echo "Git was successfully installed."
else
    echo "An error occured whhttp://www.youtube.com/watch?v=LJ2t4jfVTiUen installing Git, exiting setup."
    return
fi

if install_chef 2>/dev/null ; then
    echo "Chef was successfully installed."
else
    echo "An error occured when installing Chef, exiting setup."
    return
fi

if install_node 2>/dev/null ; then
    echo "Node.js (v0.10.21) was successfully installed."
else
    echo "An error occured when installing Chef, exiting setup."
    return
fi

if install_zmq 2>/dev/null ; then
    echo "ZeroMQ (v2.2.0) was successfully installed."
else
    echo "An error occured when installing ZeroMQ, exiting setup."
    return
fi

if install_sqlite3 2>/dev/null ; then
    echo "SQLite3 and its development library were successfully installed."
else
    echo "An error occured when installing SQLite3, exiting setup."
    return
fi

if install_mongrel2 2>/dev/null ; then
    echo "Mongrel2 (v1.8.0) was successfully installed."
else
    echo "An error occured when installing Mongrel2, exiting setup."
    return
fi
