require 'capistrano/ssh_agent/helper_methods'

include Capistrano::SshAgent::HelperMethods

namespace :ssh_agent do

namespace :config do

  task :git do
    unless fetch(:scm) == :git
      puts 'It seems you are NOT using git as a Capistrano strategy. At the moment capistrano-ssh-agent supports only git.'
      puts 'Please change `scm` setting to `:git`.'
      exit 1
    end
  end

  task :repo_url do
    if fetch(:repo_url) =~ /^http/
      report_error_for('config_repo_url')
    end
  end

  task :password do
    hosts = []
    on release_roles :all do
      if host.netssh_options[:password]
        hosts << host
      end
    end
    report_error_for('config_password', hosts) if hosts.any?
  end

  task :agent_forwarding do
    hosts = []
    on release_roles :all do
      unless host.netssh_options[:forward_agent]
        hosts << host
      end
    end
    report_error_for('config_agent_forwarding', hosts) if hosts.any?
  end

end

namespace :local do

  task :private_key_exists do
    specified_keys = fetch(:ssh_options, {})[:keys] || ''
    unless File.exists?(File.expand_path('~/.ssh/id_rsa')) ||
      File.exists?(File.expand_path('~/.ssh/id_dsa')) ||
      File.exists?(specified_keys)
      report_error_for('local_private_key_exists')
    end
  end

  task :agent_running_env_var do
    unless ENV.include?('SSH_AUTH_SOCK')
      report_error_for('local_agent_running_env_var')
    end
  end

  task :agent_running_ssh_add do
    if !system('ssh-add -l') && $? == 2
      report_error_for('local_agent_running_ssh_add')
    end
  end

  task :keys_added_to_agent do
    if !system('ssh-add -l') && $? == 1
      report_error_for('local_keys_added_to_agent')
    end
  end

  task :repo_access do
    unless system({ 'GIT_ASKPASS' => '/bin/echo' }, "git ls-remote #{fetch(:repo_url)}")
      report_error_for('local_repo_access')
    end
  end

end

namespace :remote do

  task :agent_running do
    hosts = []
    on release_roles :all do
      if capture(:echo, '$SSH_AUTH_SOCK').empty?
        hosts << host
      end
    end
    report_error_for('remote_agent_running', hosts) if hosts.any?
  end

  task repo_access: :'git:wrapper' do
    hosts = []
    on release_roles :all do
      with fetch(:git_environmental_variables) do
        hosts << host unless strategy.check
      end
    end
    report_error_for('remote_repo_access', hosts) if hosts.any?
  end

end

end

desc 'Perform ssh agent checks' task :check_ssh_agent do

initalize_report
invoke 'ssh_agent:config:git'
invoke 'ssh_agent:config:repo_url'
invoke 'ssh_agent:config:password'
invoke 'ssh_agent:config:agent_forwarding'
invoke 'ssh_agent:local:private_key_exists'
invoke 'ssh_agent:local:agent_running_env_var'
invoke 'ssh_agent:local:agent_running_ssh_add'
invoke 'ssh_agent:local:keys_added_to_agent'
invoke 'ssh_agent:local:repo_access'
invoke 'ssh_agent:remote:agent_running'
invoke 'ssh_agent:remote:repo_access'
print_report

end