namespace :apache do
  %w(start stop restart reload).each do |task_name|
    desc "#{task } Apache"
    task task_name do
      on roles(:app), in: :sequence, wait: 5 do
        sudo "/etc/init.d/apache2 #{task_name}"
      end
    end
  end

  desc "Enable site virual host"
  task "enable_virtual_host" do
    on roles(:app) do
      "cd /etc/apache2/sites-available/"
      sudo "a2ensite #{fetch(:server_name)}"
    end
  end

  desc "Remove default Apache Virtual Host"
  task "remove_default_vhost" do
    on roles(:app) do
      if test("[ -f /etc/apache2/sites-enabled/000-default.conf ]")
      sudo "rm /etc/apache2/sites-enabled/000-default.conf"
      puts "removed default Apache Virtualhost"
      else
        puts "No default Apache Virtualhost to remove"
      end
    end
  end
end
