# config valid only for current version of Capistrano
lock '3.4.0'

application = "blog_perso_app"
set :application, application 

set :stages, ["production"]

scm_user =  "git"
scm_host = "git.nicolasbally.com"

set :scm_user, scm_user
set :scm_host, scm_host

set :pty, true

set :rvm_ruby_version, 'ruby-2.1.2'          
#set :rvm_autolibs_flag, "read-only"      

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





set :deploy_to, "/home/web/#{application}/" 

set :deploy_via, :remote_cache

set :normalize_asset_timestamps, false

set :rails_env, "production"


set :scm, :git

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

set :branch, "master"


set :shared_path, "#{deploy_to}shared"

#default_run_options[:pty] = true 







namespace :deploy do
  
  
  desc "Symlink shared config files"
  task :symlink_config_files do
  
    on roles(:app) do
      execute :ln, "-s #{shared_path}/storage/private #{current_path}/private_medias"
      execute :ln, "-s #{shared_path}/storage/public #{current_path}/public/public_medias"
      execute :ln, "-s #{shared_path}/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


  end
  

  desc 'Restart application'
  task :restart do
    on roles(:app) do
      
      sudo "/etc/init.d/#{application} restart"
      
      
      
      
      
    end
  end

  after :publishing, :symlink_config_files
  
  after :symlink_config_files, :restart

  after :restart, :clear_cache do
    on roles(:web), in: :groups, limit: 3, wait: 10 do
       #nothing here
    end
  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"





  
  
  
	


# Default branch is :master
# ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp

# Default deploy_to directory is /var/www/my_app_name
# set :deploy_to, '/var/www/my_app_name'

# Default value for :scm is :git
# set :scm, :git

# Default value for :format is :pretty
# set :format, :pretty

# Default value for :log_level is :debug
# set :log_level, :debug

# Default value for :pty is false
# set :pty, true

# Default value for :linked_files is []
# set :linked_files, fetch(:linked_files, []).push('config/database.yml', 'config/secrets.yml')

# Default value for linked_dirs is []
# set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system')

# Default value for default_env is {}
# set :default_env, { path: "/opt/ruby/bin:$PATH" }

# Default value for keep_releases is 5
# set :keep_releases, 5

#namespace :deploy do

#  after :restart, :clear_cache do
#    on roles(:web), in: :groups, limit: 3, wait: 10 do
      # Here we can do anything such as:
      # within release_path do
      #   execute :rake, 'cache:clear'
      # end
#    end
#  end

#end
