#
# 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"

require 'rubygems'
require 'rake'
require 'rake/packagetask'
require 'rake/gempackagetask'

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

task :default => [:makemo]

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

    file = open("lib/gettext/rmsgfmt.rb", "w")

    file.print "=begin\n"
    file.print <<-EOS
  rmsgfmt.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/rmsgfmt.tmp.rb")
    file.print tmpfile.read
    file.close
    tmpfile.close
    File.delete("src/rmsgfmt.tmp.rb")
  end
end

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

desc "Create *.mo from *.po"
task :makemo => [:rmsgfmt, :makemo_test] do
  require 'gettext/rmsgfmt'

  $stderr.puts "Create rgettext/rmsgfmt mo files."
  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")

end

def msgmerge(name, po_root, tmp_file = "tmp.pot")
  system("msgmerge #{po_root}/#{name}.pot #{tmp_file} -o #{po_root}/#{name}.pot")

  Dir.glob("#{po_root}/*/#{name}.po"){ |f|
    lang = /#{po_root}\/(.*)\//.match(f).to_a[1]
    system("msgmerge #{po_root}/#{lang}/#{name}.po #{tmp_file} -o #{po_root}/#{lang}/#{name}.po")
    cont =IO.read(f)
    cont.sub!(/(Project-Id-Version\:.*)(\d\.\d\.\d)/, "\\1#{GetText::VERSION}")
    File.open(f, "w") do |out| 
      out.write(cont)
    end
  }
end

desc "Update *po files to match new version.(for me)"
task :updatepo do
  require 'gettext/rgettext'
  # Update rgettext po files.
  GetText.rgettext("lib/gettext/rgettext.rb", "tmp.pot")
  msgmerge("rgettext", "po")

  # Update rmsgfmt po files.
  GetText.rgettext("lib/gettext/rmsgfmt.rb", "tmp.pot")
  msgmerge("rmsgfmt", "po")
  File.delete("tmp.pot")
end

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

############################################################
# Package tasks
############################################################
require 'gettext/version'

PKG_VERSION = GetText::VERSION
desc "Create gem and tar.gz"
spec = Gem::Specification.new do |s|
  s.name = 'gettext'
  s.version = PKG_VERSION
if /mswin32/ =~ RUBY_PLATFORM
  s.platform = Gem::Platform::WIN32
  FileUtils.cd 'ext/gettext/gettext/' do
    system "#{RUBYBIN} extconf.rb"
    system "nmake"
    FileUtils.cp '_locale.so', '../../../lib/'
  end
else
  s.extensions << "ext/gettext/gettext/extconf.rb"
  s.extensions.concat Dir.glob('**/extconf.rb')
end
  s.summary = 'Ruby-GetText-Package is Native Language Support Library and Tools which modeled after GNU gettext package.'
  s.author = 'Masao Mutoh'
  s.email = 'mutoh@highway.ne.jp'
  s.homepage = 'http://ponx.s5.xrea.com/hiki/ruby-gettext.html'
  s.rubyforge_project = "gettext"
  s.files = FileList['**/*'].to_a.select{|v| v !~ /pkg|CVS/}
  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 |p|
    p.package_files = FileList['**/*'].to_a.select{|v| v !~ /pkg|CVS/}
    p.need_tar_gz = true
    p.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 :test => [:makemo_test] do 
  unless /mswin32/ =~ RUBY_PLATFORM
    sh 'cd test; sh test.sh'
  end
end
