# -*- ruby -*-

require 'rubygems'
require 'hoe'
require 'rake/contrib/rubyforgepublisher'

require 'color'
require './lib/color/rgb/jp/version.rb'

require "helper/helper"


RUBYFORGE_PROJECT = 'color-japanese'
PROJECT_NAME = RUBYFORGE_PROJECT
PROJECT_VERSION = Color::RGB::JP::VERSION::STRING

RDOC_DIR = "./html"
EXAMPLES_DIR = "#{RDOC_DIR}/examples"

PROJECT_URL = "http://rubyforge.org/projects/#{RUBYFORGE_PROJECT}"
HOME_URL = "http://#{RUBYFORGE_PROJECT}.rubyforge.org/"

NEWS_FILE = "news.txt"

directory EXAMPLES_DIR


hoe = Hoe.new(PROJECT_NAME, PROJECT_VERSION) do |p|
  p.rubyforge_name = RUBYFORGE_PROJECT
  p.author = ["MIYAMUKO Katsuyuki"]
  p.email = "miyamuko@gmail.com"
  p.url = HOME_URL
  p.extra_deps << "color-tools"

  p.summary = p.paragraphs_of('README.txt', 2..2)[0]
  p.description = p.paragraphs_of('README.txt', 2..3).join("\n\n")
  p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")

  p.need_zip = true
  p.need_tar = true
end


#
# new task
#

desc 'Uninstall the gem package'
task_for_windows :uninstall_gem do
  sh "gem.bat uninstall #{PROJECT_NAME}"
end


#
# overwrite hoe defined tasks.
#

remove_tasks(:clobber_docs, :docs, :redocs)
Rake::RDocTask.new(:docs) do |rd|
  rd.main = "README.txt"
  rd.rdoc_dir = RDOC_DIR
  files = File.read("Manifest.txt").split.grep(/^(lib|bin)|txt$/)
  files -= ['Manifest.txt']
  rd.rdoc_files.push(*files)

  rd.options << "-t #{PROJECT_NAME}-#{PROJECT_VERSION} Documentation"
  rd.options << '--charset=UTF-8'
end

desc "Build the color list HTML Files."
task :example => [EXAMPLES_DIR] do
  %W(Traditional JISZ8102).each do |e|
    sh "ruby examples/color2html.rb -p #{e} > #{EXAMPLES_DIR}/#{e.downcase}.html"
  end
end
task :docs => [:example]

task_for_windows :install_gem do
  sh "gem.bat install pkg/*.gem"
end

task_for_windows :ridocs do
  sh "rdoc.bat --ri -o ri ."
end

task_for_windows :publish_docs do
  config = YAML.load(File.read(File.expand_path("~/.rubyforge/user-config.yml")))
  Rake::RubyForgePublisher.new(RUBYFORGE_PROJECT, config["username"]).upload
end

# rake publish_docs SCP=pscp
if ENV["SCP"] and ENV["SCP"] != "scp"
  add_sh_filter_for(/\Ascp /) {|cmd|
    cmd[0].sub!(/\Ascp/, ENV["SCP"])
    cmd[0].sub!(/-rq/, "-r")
    cmd
  }
end


task :create_news do
  subject, title, body, urls = hoe.announcement
  open(NEWS_FILE, "w") do |w|
    w.puts "#{title}\n\n#{body}\n\n"
    w.puts "--"
    w.puts "Home URL:    #{HOME_URL}"
    w.puts "Project URL: #{PROJECT_URL}"
  end
end

override_task :post_news => [:create_news] do
  require 'rubyforge'
  rf = RubyForge.new
  rf.login
  rf.post_news(rubyforge_name, subject, File.read(NEWS_FILE))
  puts "Posted to rubyforge"
  rm_f NEWS_FILE
end
task :clean do
  rm_f NEWS_FILE
end

# vim: syntax=Ruby
