#!/usr/bin/env ruby
# -*- ruby -*-

require "fileutils"

require "rabbit/console"

COMMANDS = %w(generate browse)

def parse(args=ARGV, logger=nil)
  Rabbit::Console.parse!(args, logger) do |opts, options|
    options.startup_theme = nil
    options.theme_doc_dir = "theme-doc"
    options.locale_dir = nil
    options.locales = [N_("en"), N_("ja"), N_("fr")]
    options.width = 600
    options.height = 400


    opts.banner = "#{opts.banner} [COMMAND]"

    opts.separator _("[COMMAND] is one of them: [%s]") % COMMANDS.join(', ')
    opts.separator "(#{COMMANDS.first})"
    opts.separator ""

    opts.on("-I", "--include [PATH]",
            _("Add [PATH] to load path.")) do |path|
      $LOAD_PATH.unshift(path)
    end
    
    opts.separator ""
    
    opts.on("--startup-theme [THEME]",
            _("Show [THEME] when startup."),
            "(#{options.startup_theme})") do |theme|
      options.startup_theme = theme
    end

    opts.separator ""
    
    opts.on("-w", "--width [WIDTH]",
            Integer,
            _("Set window width to [WIDTH]."),
            "(#{options.width})") do |width|
      options.width = width
    end

    opts.on("-h", "--height [HEIGHT]",
            Integer,
            _("Set window height to [HEIGHT]."),
            "(#{options.height})") do |height|
      options.height = height
    end

    message = _("Set window width and height to\n" \
                "[WIDTH] and [HEIGHT].")
    message = message.split(/\n/) + ["(#{options.width},#{options.height})"]
    opts.on("-S", "--size [WIDTH],[HEIGHT]",
            Array,
            *message) do |size|
      width, height = size.collect{|x| Integer(x)}
      options.width = width
      options.height = height
    end

    opts.separator ""
    
    opts.on("--theme-doc-dir [DIR]",
            _("Specify theme document directory as [DIR].")) do |dir|
      options.theme_doc_dir = dir
    end
    
    opts.separator ""
    
    opts.on("--locales [LOC1,LOC2,...]",
            _("Specify target locales as [LOC1,LOC2,...]."),
            "([#{options.locales.join(', ')}])") do |locales|
      options.locales = locales
    end
  end
end

def do_generate(options, logger)
  require 'rabbit/theme/searcher'

  current_locale = Rabbit::Locale.get
  
  themes = Rabbit::Theme::Searcher.collect_theme
  options.locales.each do |locale|
    Rabbit::GetText.locale = current_locale
    logger.info(_("Generating documents for locale <%s>...") % locale)
    Rabbit::GetText.locale = locale
    output_dir = File.join(options.theme_doc_dir, locale)
    FileUtils.mkdir_p(output_dir)
    themes.each do |theme|
      file = File.join(output_dir, "#{theme.name}.rd")
      File.open(file, "w") do |f|
        f.print(theme.to_rd)
      end
    end
  end
end

def do_browse(options, logger)
  require 'rabbit/theme-browser'
  
  Rabbit.gui_init

  args = [logger, options.locales, options.startup_theme]
  browser = Rabbit::ThemeBrowser.new(*args)
  browser.set_window_size(options.width, options.height)
  browser.run

  Gtk.main
end

def main
  options, logger = parse

  command = ARGV.first
  if command.nil?
    command = COMMANDS.first
  elsif !COMMANDS.include?(command)
    logger.error(_("Unknown command: %s") % command)
    logger.info(_("Available commands: %s") % COMMANDS.join(', '))
    exit(1)
  end
  command = command.downcase

  __send__("do_#{command}", options, logger)
end

main
