namespace :airbrake do

desc <<-DESC
  Notify Airbrake of the deployment by running the notification on the REMOTE machine.
    - Run remotely so we use remote API keys, environment, etc.
DESC
task :deploy do
  # update scm state to get the repository information
  invoke "#{scm}:update"
  on roles(:all) do
    within release_path do
      # XXX: Invoking deploy:set_rails_env would set :rails_env to proper
      # value, but that would make us depend on capistrano-rails
      with rails_env: fetch(:rails_env, fetch(:stage)) do
        # Compose the command notify_command
        airbrake_env = fetch(:airbrake_env, fetch(:rails_env, fetch(:stage)))
        notify_command = "airbrake:deploy"
        notify_command << " TO=#{airbrake_env}"
        notify_command << " REVISION=#{fetch(:current_revision)} REPO=#{fetch(:repo_url)}"
        notify_command << " USER=#{local_user.strip.shellescape}"

        info "Notifying Airbrake of Deploy (#{notify_command})"
        execute :rake, notify_command
        info "Airbrake Notification Complete."
      end
    end
  end
end

end