#!/bin/bash

set -e # terminate the script if any command fails

## Preflight ##

# Create the log directory if it does not exist
if [ ! -x log ]; then
  mkdir log
fi

if ! echo $LANG | grep UTF &> /dev/null; then
  echo "Your system language is not UTF-8; please check the value of \$LANG"
  exit
fi

if [ -x /usr/local/var/postgres ]; then
  echo "A PostgreSQL data directory already exists at /usr/local/var/postgres; remove it to use this script"
  exit
fi

exec > log/osx_dev.log

## Install Homebrew (http://mxcl.github.com/homebrew) and other dependencies ##

if [ ! -x /usr/local/bin/brew ]; then
  echo "Installing Homebrew..."
  ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
fi

echo "Installing development dependencies from Brewfile..."
brew bundle # Install development dependencies using Homebrew
gcc-4.2 -v # Check that GCC installed successfully

## Install RVM and Ruby 2.0.0 with turbo patchset ##
echo "Installing RVM..."
\curl -sSL https://get.rvm.io | bash -s stable --rails --autolibs=homebrew
source ~/.rvm/scripts/rvm

rvm get stable
rvm autolibs homebrew # Tell RVM to install anything it's missing with homebrew
rvm requirements # This will install baseline requirements that might be missing
rvm install 2.0.0-turbo # Now, install Ruby 2.0.0 with performance patches
rvm --create --ruby-version use 2.0.0-turbo # Use this Ruby as the default in this directory

# Check that the correct version is installed and active
echo "Checking Ruby installation..."
ruby -v | grep 2.0.0

## Configure PostgreSQL (PostgreSQL was installed by Homebrew using `brew bundle`) ##
echo "Loading PostgreSQL..."

# stop PostgreSQL if it's already running
if brew services list | grep postgres; then
  brew services stop postgresql
fi

export PATH=/usr/local/opt/postgresql/bin:$PATH # You may want to put this in your default path!
initdb /usr/local/var/postgres -E utf8

brew services start postgresql

# Seed data relies on both 'postgres' and 'vagrant' users existing
echo "Creating PostgreSQL users..."

# Postgres is starting in the background. If it's not started yet, try again in a second
until createuser --createdb --superuser postgres; do
  sleep 1
done
createuser --createdb --superuser vagrant

# Obviously this password is not safe for production use
echo "Creating PostgreSQL databases..."
psql -d postgres -c "ALTER USER vagrant WITH PASSWORD 'password';"
psql -d postgres -c "create database discourse_development owner vagrant encoding 'UTF8' TEMPLATE template0;"
psql -d postgres -c "create database discourse_test        owner vagrant encoding 'UTF8' TEMPLATE template0;"
psql -d discourse_development -c "CREATE EXTENSION hstore;"
psql -d discourse_development -c "CREATE EXTENSION pg_trgm;"

## Start Redis (Redis was installed by Homebrew using `brew bundle`) ##
echo "Loading Redis..."

# stop Redis if it's already running (e.g. an old version)
if brew services list | grep redis; then
  brew services stop redis
fi

brew services start redis

## Install gems ##
echo "Installing gems..."
bundle install

## Prepare your database ##
echo "Preparing the database..."
rake db:migrate
rake db:test:prepare
rake db:seed_fu

## Run tests ##
echo "Done! Running tests..."
bundle exec rspec