require 'rubygems'
require 'rake'
require 'rake/clean'
require 'rake/testtask'
require 'rake/packagetask'
require 'rake/gempackagetask'
require 'rake/rdoctask'
require 'rake/contrib/rubyforgepublisher'
require 'fileutils'
require 'hoe'
include FileUtils
require File.join(File.dirname(__FILE__), 'lib', 'estraier', 'version')

AUTHOR = 'Mikio Hirabayashi'  # can also be an array of Authors
EMAIL = "brett@librum.org"
DESCRIPTION = "Native Ruby Hyperestraier interface"
GEM_NAME = 'estraier' 
RUBYFORGE_PROJECT = 'estraier'
HOMEPATH = "http://#{RUBYFORGE_PROJECT}.rubyforge.org"


NAME = "estraier"
REV = nil # UNCOMMENT IF REQUIRED: File.read(".svn/entries")[/committed-rev="(d+)"/, 1] rescue nil
VERS = ENV['VERSION'] || (Estraier::VERSION::STRING + (REV ? ".#{REV}" : ""))
CLEAN.include %w[
  **/.*.sw?
  *.gem
  .config
  ext/*.o
  ext/*.so
  ext/Makefile
  ext/mkmf.log
  ext/*.bundle
  **/*~
  email.txt
  **/.DS_Store
]

RDOC_OPTS = %w[
  --quiet
  --title estraier
  --opname index.html
  --line-numbers
  --main README
  --inline-source
  --exclude "^ext/.*\.c"
]

desc 'Update Manifest.txt'
task :update_manifest => :clean do
  sh "find . -type f | sed -e 's%./%%' | grep -v \".svn\" | grep -v \".log\" | grep -v \".cache\" | sort > Manifest.txt"
end

class Hoe
  def extra_deps 
    @extra_deps.reject { |x| Array(x).first == 'hoe' } 
  end 
end

# Generate all the Rake tasks
# Run 'rake -T' to see list of generated tasks (from gem root directory)
hoe = Hoe.new(GEM_NAME, VERS) do |p|
  p.author = AUTHOR 
  p.description = DESCRIPTION
  p.email = EMAIL
  p.summary = DESCRIPTION
  p.url = HOMEPATH
  p.rubyforge_name = RUBYFORGE_PROJECT if RUBYFORGE_PROJECT
  p.test_globs = ["test/**/test_*.rb"]
  p.clean_globs = CLEAN  #An array of file patterns to delete on clean.
  # == Optional
  p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
  p.rdoc_pattern = /(^lib\/estraier\/estraier-doc\.rb|README\.txt)/
  #p.extra_deps = []     # An array of rubygem dependencies [name, version], e.g. [ ['active_support', '>= 1.3.1'] ]
  p.spec_extras = {
    :extensions => ['ext/extconf.rb'],
    :rdoc_options => RDOC_OPTS
  }
end


desc 'Generate website files'
task :website_generate do
  Dir['website/**/*.txt'].each do |txt|
    sh %{ ruby scripts/txt2html #{txt} > #{txt.gsub(/txt$/,'html')} }
  end
end

desc 'Upload website files to rubyforge'
task :website_upload do
  config = YAML.load(File.read(File.expand_path("~/.rubyforge/user-config.yml")))
  host = "#{config["username"]}@rubyforge.org"
  remote_dir = "/var/www/gforge-projects/#{RUBYFORGE_PROJECT}/"
  # remote_dir = "/var/www/gforge-projects/#{RUBYFORGE_PROJECT}/#{GEM_NAME}"
  local_dir = 'website'
  sh %{rsync -av #{local_dir}/ #{host}:#{remote_dir}}
end

desc 'Generate and upload website files'
task :website => [:website_generate, :website_upload]
