#!/bin/bash

SRC_PATH="/usr/local/src"
DEST_PATH="/var/www/html/redmine"
REDMINE_PACKAGES_URL="http://downloads.safesquid.net/contrib/utilities/redmine/packages"

LIST_OF_APPS="build-essential subversion apache2 libapache2-mod-passenger apache2-dev libapr1-dev libaprutil1-dev mysql-server mysql-client libmysqlclient-dev imagemagick libmagickwand-dev python-software-properties libreadline-dev libssl-dev zlib1g-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev curl libcurl4-openssl-dev libffi-dev libgdbm-dev git zip unzip"

INSTALL_PACKAGES() {
#Required Dependency Packages	
	aptitude install -y ${LIST_OF_APPS}
}

MYSQL(){
#MySQL Configuration
	#create database and provide permission
	mysql -u root -pmysql -e "CREATE DATABASE redmine character SET utf8; CREATE user 'redmine'@'localhost' IDENTIFIED BY 'redmine'; GRANT ALL privileges ON redmine.* TO 'redmine'@'localhost'"	
}

RUBY(){

#install ruby
	#download ruby-2.3.0.tar.gz package
	cd ${SRC_PATH}
	wget ${REDMINE_PACKAGES_URL}/ruby-2.3.0.tar.gz
	
	#untar the downloaded package and move 	
	tar -zxvf ruby-2.3.0.tar.gz && mv ruby-2.3.0 ruby
	
	cd ruby/
	./configure 
	make
	make install
	
	cd ../
}

RAILS(){
#install rails
	#download rails-4.2.5.gem package
	wget ${REDMINE_PACKAGES_URL}/rails-4.2.5.gem
	
	#install the downloaded package
	gem install rails-4.2.5.gem
	
}
PASSENGER(){
#install passenger
	#download the passenger-5.0.24.gem package and install
	wget ${REDMINE_PACKAGES_URL}/passenger-5.0.24.gem
	gem install passenger-5.0.24.gem
	
	#passenger-install-apache2-module
	passenger-install-apache2-module -a --languages ruby
}

REDMINE(){
#Install redmine-3.2.0.tar.gz 
	#download the package
	wget ${REDMINE_PACKAGES_URL}/redmine-3.2.0.tar.gz
	
	#untar the downloaded package and move
	tar -zxvf redmine-3.2.0.tar.gz && mv redmine-3.2.0 redmine
			
#Redmine Configuration
	cp -Rv redmine/config/configuration.yml.example redmine/config/configuration.yml
	#cp database.yml.example  database.yml
  
	cat << _EOF > ${SRC_PATH}/redmine/config/database.yml
# Default setup is given for MySQL with ruby1.9.
# Examples for PostgreSQL, SQLite3 and SQL Server can be found at the end.
# Line indentation must be 2 spaces (no tabs).

production:
  adapter: mysql2
  database: redmine
  host: localhost
  username: redmine
  password: redmine
  encoding: utf8

_EOF
  
	#chown redmine:redmine ${SRC_PATH}/redmine/config/database.yml

#configure bundle
	pushd redmine/
	#cd redmine/
	gem install bundler
	/usr/local/bin/bundle install --without development test
	/usr/local/bin/bundle exec rake generate_secret_token
	/usr/local/bin/bundle exec RAILS_ENV="production rake db:migrate"
	#export RAILS_ENV="production rake redmine:load_default_data"
	#cd ../
	popd
}
REDMINE_PLUGIN(){
	cd ${SRC_PATH}/redmine/plugins
	
	wget ${REDMINE_PACKAGES_URL}/redmine-ldap-sync.tar.gz
	
	#untar the downloaded package and move
	tar -zxvf redmine-ldap-sync.tar.gz && rm redmine-ldap-sync.tar.gz
	
	/usr/local/bin/bundle install
	/usr/local/bin/rake redmine:plugins:migrate RAILS_ENV=production
	/usr/local/bin/rake -T redmine:plugins:ldap_sync RAILS_ENV=production
	
	}
APACHE(){
#Apache Configuration 
	#Passenger Module Configuration
	mv -v /etc/apache2/mods-available/passenger.conf /etc/apache2/mods-available/passenger.conf.default`date +%d%m%Y:%H:%M:%S`
	
	cat << _EOF > /etc/apache2/mods-available/passenger.conf
<IfModule mod_passenger.c>
	PassengerRoot /usr/local/lib/ruby/gems/2.3.0/gems/passenger-5.0.24
	PassengerDefaultRuby /usr/local/bin/ruby
</IfModule>
_EOF

	mv -v /etc/apache2/mods-available/passenger.load /etc/apache2/mods-available/passenger.load.default`date +%d%m%Y:%H:%M:%S`
	
	cat << _EOF > /etc/apache2/mods-available/passenger.load
LoadModule passenger_module /usr/local/lib/ruby/gems/2.3.0/gems/passenger-5.0.24/buildout/apache2/mod_passenger.so
_EOF
			
	ln -fs ${SRC_PATH}/redmine/public ${DEST_PATH}
	a2enmod passenger
	#Default Sites Configuration
	cp -v /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/backup-default.conf.default`date +%d%m%Y:%H:%M:%S`
	sed -i '/ErrorLog/i \
	<Directory /var/www/html/redmine> \
	\tRailsBaseURI /redmine \
    \tPassengerResolveSymlinksInDocumentRoot on \
    </Directory>' /etc/apache2/sites-available/000-default.conf
	
	service apache2 restart
}	


MAIN(){
	INSTALL_PACKAGES
	MYSQL
	RUBY
	RAILS
	PASSENGER
	REDMINE
	REDMINE_PLUGIN
	APACHE
	}
MAIN

mv /root/redmine.sh /usr/local/src/
