#!/usr/bin/env bash

# Everything necessary for Webpacker support in Rails
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

sudo apt-get -y update
sudo apt-get -y install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev nodejs yarn

# Create .svsbank_profile if none
cd
touch .svsbank_profile
# make sure non login & login terminal have access to svsbank source
touch .bash_profile

# remove already injected
sed -i 's/# START: svsbank-cli inject for source path//g' .bashrc
sed -i 's/source ~\/.svsbank_profile//g' .bashrc
sed -i 's/# END: svsbank-cli inject for source path//g' .bashrc
sed -i 's/# START: svsbank-cli inject for source path//g' .bash_profile
sed -i 's/source ~\/.svsbank_profile//g' .bash_profile
sed -i 's/# END: svsbank-cli inject for source path//g' .bash_profile

# delete end of file blang line
sed -i -e :a -e '/^\n*$/{$d;N;ba' -e '}' .bashrc
sed -i -e :a -e '/^\n*$/{$d;N;ba' -e '}' .bash_profile

# reinject svsbank source to bashrc & bash_profile.
echo '# START: svsbank-cli inject for source path' >> ~/.bashrc
echo 'source ~/.svsbank_profile' >> ~/.bashrc
echo '# END: svsbank-cli inject for source path' >> ~/.bashrc
echo '# START: svsbank-cli inject for source path' >> ~/.bash_profile
echo 'source ~/.svsbank_profile' >> ~/.bash_profile
echo '# END: svsbank-cli inject for source path' >> ~/.bash_profile

# remove already injected .rbenv & .ruby-build path
sed -i 's/# START: svsbank-cli inject for .rbenv path//g' .svsbank_profile
sed -i 's/export PATH="$HOME\/.rbenv\/bin:$PATH"//g' .svsbank_profile
sed -i 's/eval "$(rbenv init -)"//g' .svsbank_profile
sed -i 's/# END: svsbank-cli inject for .rbenv path//g' .svsbank_profile
sed -i 's/# START: svsbank-cli inject for .ruby-build path//g' .svsbank_profile
sed -i 's/export PATH="$HOME\/.rbenv\/plugins\/ruby-build\/bin:$PATH"//g' .svsbank_profile
sed -i 's/# END: svsbank-cli inject for .ruby-build path//g' .svsbank_profile

# delete blang line
sed -i '/^$/d' .svsbank_profile

# Installing with rbenv
cd
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo '# START: svsbank-cli inject for .rbenv path' >> ~/.svsbank_profile
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.svsbank_profile
echo 'eval "$(rbenv init -)"' >> ~/.svsbank_profile
echo '# END: svsbank-cli inject for .rbenv path' >> ~/.svsbank_profile

git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
echo '# START: svsbank-cli inject for .ruby-build path' >> ~/.svsbank_profile
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.svsbank_profile
echo '# END: svsbank-cli inject for .ruby-build path' >> ~/.svsbank_profile

source .svsbank_profile

rbenv install 2.3.0
rbenv global 2.3.0
ruby -v

gem install bundler
rbenv rehash

# Install Rails
gem install rails -v 5.2.0
rbenv rehash
rails -v

echo "Rails installed succesfully"
