namespace :auth_basic do

desc 'Generate HTTP basic authentication'
task :generate do
  puts "Please type the password for HTTP basic authentication, press ENTER when you're finished:"
  passwd   = $stdin.gets.chomp
  htpasswd = nil

  run_locally do
    htpasswd = capture(:htpasswd, '-nb', fetch(:auth_basic_user), passwd)
    htpasswd.chomp!
  end

  on fetch(:auth_basic_servers) do
    upload! StringIO.new(htpasswd), fetch(:auth_basic_target)
    execute :chmod, 'o+r', fetch(:auth_basic_target)
  end
end

end

namespace :load do

task :defaults do
  set :auth_basic_user,     'root'
  set :auth_basic_roles,    :all
  set :auth_basic_servers,  -> { release_roles(fetch(:auth_basic_roles)) }
  set :auth_basic_target,   -> { shared_path.join('.htpasswd') }
end

end