# -*- mode: ruby -*-
# vi: set ft=ruby :

# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.

# APACHE THRIFT PROVISIONING SCRIPT
##############################################################
# This script is used to configure the base Centos 6.5
# Vagrant box for Apache Thrift compiler and lib builds.
# The base box is Centos 6.5 with no additional packages
# except those required to support VirtualBox Guest tools:
# perl, dkms, kernel-devel and the "Development Tools" group.
# The epel repo was also added along with the
# yum-plugin-protectbase package to prefer base repo packages.
# The script below provisions ALL languages. This will take
# time. You can greatly reduce the build time by commenting
# out the LIB provisioning for uneeded language libraries.
# Expect full provisioning to take 30 minutes on a fast
# machine with an excellent Internet connection (and another
# 15 minutes for the build).
#
# Machine accounts:
# - User: vagrant/vagrant
# - Admin: root/vagrant
# Vagrant public ssh key also installed
##############################################################

$build_and_test = <<SCRIPT
echo "Provisioning system to compile and test Apache Thrift."
date > /etc/vagrant.provision_begin

# Apache Thrift compiler dependencies
#####################################

#install an updated autoconf
wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.69.tar.gz
tar xvf autoconf-2.69.tar.gz
cd autoconf-2.69
./configure --prefix=/usr
make
sudo make install
cd ..

#install an updated automake
wget http://ftp.gnu.org/gnu/automake/automake-1.14.tar.gz
tar xvf automake-1.14.tar.gz
cd automake-1.14
./configure --prefix=/usr
make
sudo make install
cd ..

#install an updated bison
wget http://ftp.gnu.org/gnu/bison/bison-2.5.1.tar.gz
tar xvf bison-2.5.1.tar.gz
cd bison-2.5.1
./configure --prefix=/usr
make
sudo make install
cd ..

# C++98 LIB Dependencies
#####################################
sudo yum -y install libevent-devel zlib-devel openssl-devel

#Install an updated Boost library
wget http://sourceforge.net/projects/boost/files/boost/1.55.0/boost_1_55_0.tar.gz
tar xvf boost_1_55_0.tar.gz
cd boost_1_55_0
./bootstrap.sh
sudo ./b2 install

# Java LIB Dependencies
#####################################
sudo yum install -y ant junit ant-nodeps ant-junit java-1.8.0-openjdk-devel

# Python LIB Dependencies
#####################################
sudo yum install -y python-devel python-setuptools python-twisted

# Ruby LIB Dependencies
#####################################
sudo yum install -y ruby ruby-devel rubygems
sudo gem install bundler rake

# Node.js LIB Dependencies
#####################################
sudo yum install -y nodejs nodejs-devel npm

# Perl LIB Dependencies
#####################################
sudo yum install -y perl-Bit-Vector perl-Class-Accessor perl-ExtUtils-MakeMaker perl-Test-Simple

# PHP LIB Dependencies
#####################################
sudo yum install -y php php-devel php-pear re2c

# GLibC LIB Dependencies
#####################################
sudo yum install -y glib2-devel

# Erlang LIB Dependencies
#####################################
sudo yum install -y erlang-kernel erlang-erts erlang-stdlib erlang-eunit erlang-rebar

# Lua LIB Dependencies
#####################################
sudo yum install -y lua-devel

# Go LIB Dependencies
#####################################
sudo yum install -y golang golang-pkg-linux-amd64

# C# LIB Dependencies
#####################################
sudo yum install -y mono-core mono-devel mono-web-devel mono-extras mingw32-binutils mingw32-runtime mingw32-nsis

# Haskell LIB Dependencies
#####################################
wget http://sherkin.justhub.org/el6/RPMS/x86_64/justhub-release-2.0-4.0.el6.x86_64.rpm
sudo rpm -ivh justhub-release-2.0-4.0.el6.x86_64.rpm
sudo yum -y install haskell
sudo cabal update
sudo cabal install cabal-install

# Build and Test Apache Thrift
#####################################
date > /etc/vagrant.provision_end
echo "Starting Apache Thrift build..."
cd /thrift
sh bootstrap.sh

# At the time of this file's creation Ruby, Python, Go and Lua fail
# their unit tests in this environment. To build and test any of these
# libraries uncomment the appropriate --without switches below.

sh configure --without-ruby --without-go --without-lua --without-python
make
echo "Starting Apache Thrift tests..."
make check
echo "Finished building and testing Apache Thrift."
echo 'Use "make install" to install the compiler and libraries.'
date > /etc/vagrant.make_end

SCRIPT

Vagrant.configure("2") do |config|
  # Every Vagrant virtual environment requires a box to build off of.
  ##### Centos 6.5 minimal system with VirtualBox Guest Additions
  ##### Box maintained by ra@apache.org, see README.md for box config
  config.vm.box = "RandyAbernethy/thrift-centos-6.5-64"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  ##### This box will never change
  config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  # If true, then any SSH connections made will enable agent forwarding.
  # Default value: false
  # config.ssh.forward_agent = true

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"
  ##### By convention the thrift source tree is mapped to /thrift
  config.vm.synced_folder "../../../", "/thrift"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  ##### The machine needs 2 CPUs and 2GB RAM for reasonable performance
  config.vm.provider "virtualbox" do |vb|
    vb.customize ["modifyvm", :id, "--memory", "2048"]
    vb.customize ["modifyvm", :id, "--cpus", "2"]
  end

  # Enable provisioning with CFEngine. CFEngine Community packages are
  # automatically installed. For example, configure the host as a
  # policy server and optionally a policy file to run:
  #
  # config.vm.provision "cfengine" do |cf|
  #   cf.am_policy_hub = true
  #   # cf.run_file = "motd.cf"
  # end
  #
  # You can also configure and bootstrap a client to an existing
  # policy server:
  #
  # config.vm.provision "cfengine" do |cf|
  #   cf.policy_server_address = "10.0.2.15"
  # end

  # Enable provisioning with Puppet stand alone.  Puppet manifests
  # are contained in a directory path relative to this Vagrantfile.
  # You will need to create the manifests directory and a manifest in
  # the file default.pp in the manifests_path directory.
  #
  # config.vm.provision "puppet" do |puppet|
  #   puppet.manifests_path = "manifests"
  #   puppet.manifest_file  = "default.pp"
  # end

  # Enable provisioning with chef solo, specifying a cookbooks path, roles
  # path, and data_bags path (all relative to this Vagrantfile), and adding
  # some recipes and/or roles.
  #
  # config.vm.provision "chef_solo" do |chef|
  #   chef.cookbooks_path = "../my-recipes/cookbooks"
  #   chef.roles_path = "../my-recipes/roles"
  #   chef.data_bags_path = "../my-recipes/data_bags"
  #   chef.add_recipe "mysql"
  #   chef.add_role "web"
  #
  #   # You may also specify custom JSON attributes:
  #   chef.json = { mysql_password: "foo" }
  # end

  # Enable provisioning with chef server, specifying the chef server URL,
  # and the path to the validation key (relative to this Vagrantfile).
  #
  # The Opscode Platform uses HTTPS. Substitute your organization for
  # ORGNAME in the URL and validation key.
  #
  # If you have your own Chef Server, use the appropriate URL, which may be
  # HTTP instead of HTTPS depending on your configuration. Also change the
  # validation key to validation.pem.
  #
  # config.vm.provision "chef_client" do |chef|
  #   chef.chef_server_url = "https://api.opscode.com/organizations/ORGNAME"
  #   chef.validation_key_path = "ORGNAME-validator.pem"
  # end
  #
  # If you're using the Opscode platform, your validator client is
  # ORGNAME-validator, replacing ORGNAME with your organization name.
  #
  # If you have your own Chef Server, the default validation client name is
  # chef-validator, unless you changed the configuration.
  #
  #   chef.validation_client_name = "ORGNAME-validator"

  ##### Run the Apache Thrift provisioning script (declared above)
  config.vm.provision :shell, :inline => $build_and_test
end
