#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.  The
# ASF licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License.  You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
# License for the specific language governing permissions and limitations
# under the License.

# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.

require 'rake'
require 'rake/testtask'
require 'rubygems/package_task'

begin
  require 'ci/reporter/rake/test_unit'
rescue LoadError
end

begin
  require 'cucumber'
  require 'cucumber/rake/task'
  namespace :cucumber do
    %w(mock ec2 sbc).each do |driver|
      namespace driver do
        Cucumber::Rake::Task.new(:test) do |t|
          t.cucumber_opts = "../tests/#{driver} --format pretty"
          t.rcov = false
        end
        Cucumber::Rake::Task.new(:features) do |t|
          t.cucumber_opts = "../tests/#{driver} --format html --out ../tests/tmp/cucumber_#{driver}.html"
          t.rcov = false
        end
        Cucumber::Rake::Task.new(:junit) do |t|
          t.cucumber_opts = "../tests/#{driver} --format junit --out #{File.join(File.dirname(__FILE__), "tmp", "junit_#{driver}")}"
        end
      end
    end
  end
rescue LoadError
end

namespace :test do
  %w(mock rackspace rhevm).each do |driver|
    desc "Run #{driver} unit tests"
    Rake::TestTask.new(driver) { |t|
      t.test_files = ['tests/common.rb', "tests/drivers/#{driver}/setup.rb"] + FileList.new("tests/drivers/#{driver}/*_test.rb") + FileList.new('tests/rabbit_test.rb')
      t.options = "-v -v"
      t.verbose = true
      t.warning = false
    }
  end
end

desc "Call our Test::Unit suite"
task :test do
  %w(mock rackspace rhevm).each do |driver|
   Rake::Task["test:#{driver}"].reenable
   Rake::Task["test:#{driver}"].invoke
  end
end

desc "Call our Cucumber suite"
task :cucumber do
  %w(mock ec2 sbc).each do |driver|
   Rake::Task["cucumber:#{driver}:test"].reenable
   Rake::Task["cucumber:#{driver}:test"].invoke
  end
end

begin
  require 'yard'
  YARD::Rake::YardocTask.new do |t|
    t.files   = ['lib/**/*.rb', '*.rb']   # optional
  end
rescue LoadError
end

spec = Gem::Specification.load('deltacloud-core.gemspec')

Gem::PackageTask.new(spec) do |pkg|
  pkg.need_tar = true
end

desc "List all REST routes defined through Rabbit"
task :routes do
  require 'server.rb'
  Sinatra::Rabbit::routes.each do |m, path|
    puts sprintf("\033[1;30m%-8s\033[0m %s", m.to_s.upcase, path)
  end
end

namespace :mock do
  namespace :fixtures do
    desc "Setup Mock driver fixtures"
    task 'setup' do
      if ENV["DELTACLOUD_MOCK_STORAGE"]
        storage_root = ENV["DELTACLOUD_MOCK_STORAGE"]
      elsif ENV["USER"]
        storage_root = File::join("/var/tmp", "deltacloud-mock-#{ENV["USER"]}")
      else
        raise "Please set either the DELTACLOUD_MOCK_STORAGE or USER environment variable"
      end
      data = Dir::glob(File::join(File::dirname(__FILE__), "lib", "deltacloud", "drivers", "mock", "data", "*"))
      FileUtils::mkdir_p(storage_root, :verbose => true)
      FileUtils::cp_r(data, storage_root, :verbose => true)
    end

    desc "Remove Mock driver fixtures"
    task 'clean' do
      if ENV["DELTACLOUD_MOCK_STORAGE"]
        storage_root = ENV["DELTACLOUD_MOCK_STORAGE"]
      elsif ENV["USER"]
        storage_root = File::join("/var/tmp", "deltacloud-mock-#{ENV["USER"]}")
      else
        raise "Please set either the DELTACLOUD_MOCK_STORAGE or USER environment variable"
      end
      FileUtils::rm_rf(storage_root, :verbose => true)
    end

    desc "Reset Mock driver fixtures"
    task 'reset' do
      Rake::Task["mock:fixtures:clean"].reenable
      Rake::Task["mock:fixtures:clean"].invoke
      Rake::Task["mock:fixtures:setup"].reenable
      Rake::Task["mock:fixtures:setup"].invoke
    end

  end
end
