set :application,     "op1"

set :deploy_server,   "gotar.ru"

set :user,            "deploy"
set :deploy_to,       "/home/#{fetch(:user)}/projects/#{fetch(:application)}"
set :unicorn_conf,    "/etc/unicorn/#{fetch(:application)}.rb"
set :unicorn_pid,     "/var/run/unicorn/#{fetch(:application)}.pid"

set :rvm_ruby_version, '2.0.0'      # Defaults to: 'default'

set :repo_url,        "ssh://#{fetch(:user)}@#{fetch(:deploy_server)}/home/#{fetch(:user)}/git/#{fetch(:application)}.git"

set :linked_files, %w{config/database.yml}
set :linked_dirs, %w{bin log tmp/pids public/system}

set :unicorn_start_cmd, "(cd #{deploy_to}/current; rvm use #{fetch(:rvm_ruby_version)} do bundle exec unicorn -Dc #{fetch(:unicorn_conf)})"

# - for unicorn - #
namespace :deploy do
  desc "Start application"
  task :start do 
    on roles(:app) do |host|
      run fetch(:unicorn_start_cmd)
    end
  end

  desc "Stop application"
  task :stop do
    on roles(:app) do |host|
      run "[ -f #{fetch(:unicorn_pid)} ] && kill -QUIT `cat #{fetch(:unicorn_pid)}`"
    end
  end

  desc "Restart Application"
  task :restart do
    on roles(:app) do |host|
      run "[ -f #{fetch(:unicorn_pid)} ] && kill -USR2 `cat #{fetch(:unicorn_pid)}` || #{fetch(:unicorn_start_cmd)}"
    end
  end

  after :finishing, 'deploy:cleanup'

end
