# config valid only for Capistrano 3.1
# lock '3.2.1'
#require 'capistrano/local_precompile'

set :application, 'mozo.bar'
#set :repo_url, 'root@uflows.com:/var/git/qwaiter.git'
set :repo_url, '/var/git/qwaiter.git'

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

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

set :deploy_via, :remote_cache
set :branch, :master
set :user, 'root'
set :deploy_to, '/var/www/mozo.bar'
set :bundle_without, %w[development test assets].join(' ')
# 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, %w{config/database.yml}

# Default value for linked_dirs is []
set :linked_dirs, %w{log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system public/assets}

# Default value for default_env is {}
# set :default_env, { path: "/opt/ruby/bin:$PATH" }
#set :default_env, {'DEPLOY' => 'yes'}

set :puma_threads,    [4, 16]
set :puma_workers,    2
set :puma_bind,       "unix://#{shared_path}/tmp/sockets/app.sock"
set :puma_state,      "#{shared_path}/tmp/pids/puma.state"
set :puma_pid,        "#{shared_path}/tmp/pids/puma.pid"
set :puma_access_log, "#{release_path}/log/puma.error.log"
set :puma_error_log,  "#{release_path}/log/puma.access.log"
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, false  # Change to true if using ActiveRecord

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

namespace :deploy do

  desc 'Restart application'
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
      # Your restart mechanism here, for example:
       within release_path do
         #execute :rake, 'cache:clear', as: 'www-data'
         #with rails_env: fetch(:rails_env) do
           #execute :rake, 'dedigest_assets', as: 'www-data'
         #end
       end
       #execute :chown, "-R www-data:www-data", release_path
       #execute :chown, "-R www-data:www-data", release_path.join('public/assets/')
       #execute :kill, capture("lsof -i:9022 -t") # kill counters, will reboot itself
       execute :touch, release_path.join('tmp/restart.txt')
    end
  end

  task :update_remote_cache do
    on roles(:app), in: :sequence, wait: 5 do
      within shared_path.join('cached-copy') do
        execute :git, :pull
      end
    end
  end

  task :build_drb_counter do

  end

  namespace :assets do

    Rake::Task['deploy:assets:precompile'].clear_actions
    Rake::Task['deploy:assets:backup_manifest'].clear_actions

    desc 'Precompile assets locally and upload to servers'
    task :precompile do
      on roles(fetch(:assets_roles)) do
        run_locally do
          with rails_env: fetch(:stage) do
            execute "RAILS_ENV=#{fetch(:stage)} DEPLOY=yes bin/rake assets:precompile"
            execute "RAILS_ENV=#{fetch(:stage)} DEPLOY=yes bin/rake assets:dedigest"
            execute "RAILS_ENV=#{fetch(:stage)} DEPLOY=yes bin/rake assets:gzip"
          end
        end

        #within release_path do
          #with rails_env: fetch(:rails_env) do
            #upload!('./public/assets/', "#{shared_path}/public/", recursive: true)
          #end
        #end
        rsync_host = host
        run_locally { execute "rsync -av --delete --ignore-times ./public/assets/ #{fetch(:user)}@#{rsync_host}:#{shared_path}/public/assets/" }
        #run_locally { execute "rsync -av ./public/assets/ #{fetch(:user)}@#{rsync_host}:#{shared_path}/public/assets/" }
        execute :chown, "-R www-data:www-data", shared_path.join('public/assets/')

        #run_locally { execute 'rm -rf public/assets' }
      end
    end

  end
  #desc 'Set config/puma.rb-symlink for upstart'
  #task :puma_config do
    #on roles(:app) do
      #execute "ln -sf #{shared_path}/puma.rb #{release_path}/config/puma.rb"
    #end
  #end
  #after :finishing, :puma_config

  after :publishing, :restart do
    #deploy.update_remote_cache
  end

  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', as: 'www-data'
       #end
    end
  end

end
