#
# Rakefile for Ruby-GetText-Package
#
# This file maintains Ruby-GetText-Package.
#
# Use setup.rb or gem for installation.
# You don't need to use this file directly.
#
# Copyright(c) 2005 Masao Mutoh
# This program is licenced under the same licence as Ruby.
#

$:.unshift "./lib"
$:.unshift "./ext/gettext"

require 'rubygems'
require 'rake'
require 'rake/packagetask'
require 'rake/gempackagetask'
require 'rake/rdoctask'
require 'gettext/version'

RUBYBIN = File.join(Config::CONFIG['bindir'], Config::CONFIG['ruby_install_name']) + " "

PKG_VERSION = GetText::VERSION

task :default => [:makemo]

############################################################
# GetText tasks
############################################################
desc "Create lib/gettext/poparser.rb"
task :poparser do
  poparser_path = "lib/gettext/poparser.rb"
  racc = File.join(Config::CONFIG['bindir'], "racc")
  if ! FileTest.exist?(racc)
    puts "racc was not found: #{racc}"
    exit 1
  else FileTest.exist?(racc)
    cmd = "#{RUBYBIN} #{racc} -g src/poparser.ry -o src/poparser.tmp.rb"
    system(cmd)
    $stderr.puts cmd

    file = open(poparser_path, "w")

    file.print "=begin\n"
    file.print <<-EOS
  poparser.rb - Generate a .mo

  Copyright (C) 2003-2005 Masao Mutoh <mutoh@highway.ne.jp>

  You may redistribute it and/or modify it under the same
  license terms as Ruby.
EOS
    file.print "=end\n\n"

    tmpfile = open("src/poparser.tmp.rb")
    file.print tmpfile.read
    file.close
    tmpfile.close
    File.delete("src/poparser.tmp.rb")
    $stderr.puts "Create #{poparser_path}."
    require 'gettext/utils'
  end
end

############################################################
# Manage po/mo files
############################################################
begin
  require 'gettext'
  require 'gettext/poparser'
  require 'gettext/utils'
rescue LoadError
  puts "gettext/poparser was not found."
end

task :makemo_test do
  require 'gettext/utils'
  $stderr.puts "Create test mo files."
  GetText.create_mofiles(false, "test/po", "test/locale")
end

desc "Create *.mo from *.po"
task :makemo => [:makemo_test] do
  $stderr.puts "Create rgettext/rails mo files."
  mkdir_p "samples/rails/log" unless FileTest.exist? "samples/rails/log"

  GetText.create_mofiles(false)

  $stderr.puts "Create samples mo files."
  GetText.create_mofiles(false, "samples/po", "samples/locale")

  $stderr.puts "Create samples/cgi mo files."
  GetText.create_mofiles(false, "samples/cgi/po", "samples/cgi/locale")

  $stderr.puts "Create samples/rails mo files."
  GetText.create_mofiles(false, "samples/rails/po", "samples/rails/locale")

  $stderr.puts "Create samples/rails plugin mo files."
  GetText.create_mofiles(false, "samples/rails/vendor/plugins/gettext/po", "samples/rails/vendor/plugins/gettext/locale")
end

desc "Update pot/po files to match new version."
task :updatepo do
  #lib/gettext/*.rb -> rgettext.po
  GetText.update_pofiles("rgettext", 
			 Dir.glob("lib/**/*.rb") + ["src/poparser.ry"] - ["lib/gettext/rails.rb"], 
			 "ruby-gettext #{GetText::VERSION}")

  #lib/gettext/rails.rb -> rails.po
  GetText.update_pofiles("rails", ["lib/gettext/rails.rb"], 
			 "ruby-gettext #{GetText::VERSION}")
end

desc "Setup Ruby-GetText-Package. (for setup.rb)"
task :setup => [:makemo]

desc "Gather the newest po files. (for me)"
task :gatherpo => [:updatepo] do
  mkdir_p "pofiles/original" unless FileTest.exist? "pofiles/original"
  Dir.glob("**/*.pot").each do |f|
    unless /^(pofiles|test)/ =~ f
      copy f, "pofiles/original/"
    end
  end
  Dir.glob("**/*.po").each do |f|
    unless /^(pofiles|test)/ =~ f
      lang = /po\/([^\/]*)\/(.*.po)/.match(f).to_a[1]
      mkdir_p "pofiles/#{lang}" unless FileTest.exist? "pofiles/#{lang}"
      copy f, "pofiles/#{lang}/"
      Dir.glob("pofiles/original/*.pot").each do |f|
	newpo = "pofiles/#{lang}/#{File.basename(f, ".pot")}.po"
	copy f, newpo unless FileTest.exist? newpo
      end
    end
  end	
end

############################################################
# Package tasks
############################################################

if /mswin32/ =~ RUBY_PLATFORM
  task :gem => [:mswin32so]
  task :mswin32so do
    FileUtils.cd 'ext/gettext' do
      system "#{RUBYBIN} extconf.rb"
      system "nmake"
      FileUtils.cp 'locale_system.so', '../../lib/'
    end
  end
end

desc "Create gem and tar.gz"
spec = Gem::Specification.new do |s|
  s.name = 'gettext'
  s.version = PKG_VERSION

  if /mswin32|mingw/ =~ RUBY_PLATFORM
    s.platform = Gem::Platform::WIN32
  else
    s.extensions << "ext/gettext/extconf.rb"
    s.extensions.concat Dir.glob('**/extconf.rb')
  end

  s.summary = 'Ruby-GetText-Package is a libary and tools to localize messages.'
  s.author = 'Masao Mutoh'
  s.email = 'mutoh@highway.ne.jp'
  s.homepage = 'http://www.yotabanana.com/hiki/ruby-gettext.html'
  s.rubyforge_project = "gettext"
  files = FileList['**/*'].to_a.select{|v| v !~ /pkg|CVS/}
  files << "lib/locale_system.so" if /mswin32/ =~ RUBY_PLATFORM
  s.files = files
  s.require_path = 'lib'
  s.executables = Dir.entries('bin').delete_if {|item| /^\.|CVS|~$/ =~ item }
  s.bindir = 'bin'
  s.description = <<-EOF
        Ruby-GetText-Package is a GNU GetText-like program for Ruby.
        The catalog file(po-file) is same format with GNU GetText.
        So you can use GNU GetText tools for maintaining.
  EOF
end

unless /mswin32/ =~ RUBY_PLATFORM
  Rake::PackageTask.new("ruby-gettext-package", PKG_VERSION) do |o|
    o.package_files = FileList['**/*'].to_a.select{|v| v !~ /pkg|CVS/}
    o.need_tar_gz = true
    o.need_zip = false
  end
end

Rake::GemPackageTask.new(spec) do |p|
  p.gem_spec = spec
  p.need_tar_gz = false
  p.need_zip = false
end

############################################################
# Misc tasks
############################################################
desc "Test task (for shell environment only)"
task :testunit => [:makemo_test] do 
  unless /mswin32/ =~ RUBY_PLATFORM
    sh 'cd test; sh test.sh'
  end
end

Rake::RDocTask.new do |rd|
  rd.main = "README"
  rd.rdoc_files.include("README", "lib/**/*.rb")
end

PKG_GID = 855
PKG_ID = 978
def release_package(target)
  cmd = "rubyforge add_release #{PKG_GID} #{PKG_ID} 'Ruby-GetText-Package-#{PKG_VERSION}' pkg/#{target}"
  puts cmd
  system(cmd)
end

desc "Publish the release files to RubyForge."
task :rubyforge do
  `rubyforge login`
  release_package("gettext-#{PKG_VERSION}.gem")
  release_package("ruby-gettext-package-#{PKG_VERSION}.tar.gz")
end

task :rubyforge_delete do
  `rubyforge login`
  cmd =  "rubyforge delete_package #{PKG_GID} #{PKG_ID}"
  puts cmd
  system(cmd)
end
