

set :application, 'blog_eft_app' 
	set :domain, 'blog.technique-eft.com'

set :user, 'web'
set :scm_user, "git"
set :scm_host, "git.nicolasbally.com"



set :rvm_ruby_string, 'ruby-2.3.0'           
set :rvm_autolibs_flag, "read-only"      

set :bundle_cmd, "/home/#{user}/.rvm/gems/#{rvm_ruby_string}@global/bin/bundle"
set :bundle_dir, "/home/#{user}/.rvm/gems/#{rvm_ruby_string}"
set :use_sudo, false





set :deploy_to, "/home/#{user}/#{application}/" 
set :deploy_via, :remote_cache

set :normalize_asset_timestamps, false

set :rails_env, "production"

role :app, domain
role :web, domain
role :db, domain, :primary => true

set :scm, :git

set :repository,  "#{scm_user}@#{scm_host}:root/#{application}.git"

set :branch, "master"

default_run_options[:pty] = true 


require "rvm/capistrano"
require 'bundler/capistrano'




load 'deploy/assets'

namespace :deploy do
  
#  namespace :assets do
#    task :precompile, :roles => :web, :except => { :no_release => true } do
#      from = source.next_revision(current_revision)
#      if releases.length <= 1 || capture("cd #{latest_release} && #{source.local.log(from)} vendor/assets/ app/assets/ | wc -l").to_i > 0
#        run %Q{cd #{latest_release} && #{rake} RAILS_ENV=#{rails_env} #{asset_env} assets:precompile}
#      else
#        logger.info "Skipping asset pre-compilation because there were no asset changes"
#      end
#    end
#  end
  
  
  
  desc "Symlink shared config files"
  task :symlink_config_files do
    
    
    run "ln -s #{deploy_to}shared/storage/private #{current_path}/private_medias"

    run "ln -s #{deploy_to}shared/storage/public #{current_path}/public/public_medias"

    run "ln -s #{deploy_to}shared/production.sqlite3 #{current_path}/db/production.sqlite3"
    
    sudo "cp #{current_path}/config/unicorn_init_d /etc/init.d/#{application}"
    
    sudo "chmod +x /etc/init.d/#{application}"
    sudo "update-rc.d #{application} defaults	"
  end



  task :start do 
    sudo "service #{application} start"
  end
  task :stop do 
    run "service #{application} stop"  
  end
  task :restart do
    sudo "service #{application} restart"
  end

  task :reload_nginx do
    
    sudo "rm -rf /etc/nginx/sites-enabled/#{application}"
    sudo "ln -s #{current_path}/config/nginx.conf /etc/nginx/sites-enabled/#{application} "
    
    sudo "service nginx reload"
  end



  task :seeds do 
    run "cd #{deploy_to}current && rake db:seed RAILS_ENV=#{rails_env}"

  end
  

    task :create_files_folders do
  
      run "mkdir #{deploy_to}shared/storage"
      run "mkdir #{deploy_to}shared/storage/private"
      run "mkdir #{deploy_to}shared/storage/public"
    end

    
  
end


#before 'deploy:setup', 'rvm:install_rvm'   # install RVM
#before 'deploy:setup', 'rvm:install_ruby'  # install Ruby and create gemset, OR:
#before 'deploy:setup', 'rvm:create_gemset' # only create gemset
  


after "deploy:setup", "deploy:create_files_folders"

#after "deploy:restart", "deploy:cleanup"


before "deploy:restart", "deploy:symlink_config_files"

after "deploy", "deploy:cleanup"


  