require 'rake/rdoctask'
require 'rake/testtask'
require 'rake/gempackagetask'
require 'rake/packagetask'

###################################################
# Dependencies
###################################################

task "default" => ["test"]
task "fulltest" => ["test", "blackbox"] 
task "generate_rdoc" => ["cleanup_readme"]
task "cleanup_readme" => ["rdoc"]

###################################################
# Gem specification
###################################################

gem_spec = Gem::Specification.new do |s|           
  s.name = 'scrubyt' 
  s.version = '0.2.8'
  s.summary = 'A powerful Web-scraping framework'
  s.description = %{scRUBYt! is an easy to learn and use, yet powerful and effective web scraping framework. It's most interesting part is a Web-scraping DSL built on HPricot and WWW::Mechanize, which allows to navigate to the page of interest, then extract and query data records with a few lines of code. It is hard to describe scRUBYt! in a few sentences - you have to see it for yourself!} 
  # Files containing Test::Unit test cases. 
  s.test_files = FileList['test/unittests/**/*'] 
  # List of other files to be included. 
  s.files = FileList['COPYING', 'README', 'CHANGELOG', 'Rakefile', 'lib/**/*.rb']
  s.author = 'Peter Szinek'
  s.email = 'peter@rubyrailways.com' 
  s.homepage = 'http://www.scrubyt.org'
  s.add_dependency('hpricot', '>= 0.5')
  s.add_dependency('mechanize', '>= 0.6.3') 
  s.has_rdoc = 'true'
end 

###################################################
# Tasks
###################################################

Rake::RDocTask.new do |generate_rdoc|
     files = ['lib/**/*.rb', 'README', 'CHANGELOG']
     generate_rdoc.rdoc_files.add(files)
     generate_rdoc.main = "README" # page to start on
     generate_rdoc.title = "Scrubyt Documentation"
     generate_rdoc.template = "resources/allison/allison.rb"
     generate_rdoc.rdoc_dir = 'doc' # rdoc output folder
     generate_rdoc.options << '--line-numbers' << '--inline-source'
end

Rake::TestTask.new do |test|           
  test.pattern = 'test/unittests/*_test.rb' 
end 

task "blackbox" do
  ruby "test/blackbox/run_blackbox_tests.rb"
end

task "cleanup_readme" do
  puts "Cleaning up README..."
  readme_in = open('./doc/files/README.html')
  content = readme_in.read
  content.sub!('<h1 id="item_name">File: README</h1>','')
  content.sub!('<h1>Description</h1>','')
  readme_in.close
  open('./doc/files/README.html', 'w') {|f| f.write(content)}
  #OK, this is uggly as hell and as non-DRY as possible, but
  #I don't have time to deal with it right now
  puts "Cleaning up CHANGELOG..."
  readme_in = open('./doc/files/CHANGELOG.html')
  content = readme_in.read
  content.sub!('<h1 id="item_name">File: CHANGELOG</h1>','')
  content.sub!('<h1>Description</h1>','')
  readme_in.close
  open('./doc/files/CHANGELOG.html', 'w') {|f| f.write(content)}
end

task "generate_rdoc" do
end

Rake::GemPackageTask.new(gem_spec) do |pkg|           
  pkg.need_zip = false
  pkg.need_tar = false 
end

Rake::PackageTask.new('scrubyt-examples', '0.2.8') do |pkg|
  pkg.need_zip = true
  pkg.need_tar = true
  pkg.package_files.include("examples/**/*")
end
