#!/usr/bin/env ruby
# -*- mode: ruby; coding: utf-8 -*-
#
# Copyright (C) 2011-2012  Kouhei Sutou <kou@cozmixng.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

require "rabbit/console"

require "rabbirack/version"
require "rabbirack/gettext"

include RabbiRack::GetText

base_dir = nil
$LOAD_PATH.each do |path|
  base_dir = File.join(path, "rabbirack")
  if File.exist?(base_dir)
    break
  else
    base_dir = nil
  end
end
public_path = File.join(base_dir, "public")
config_ru_path = File.join(base_dir, "config.ru")

parse = lambda do
  Rabbit::Console.parse!(ARGV) do |parser, options|
    options.version = RabbiRack::VERSION
    options.rabbit_uri = options.druby_uri
    options.port = 10102
    options.bonjour = false

    parser.banner += " [shotgun|rackup] [-- [RACK_RUNNER_OPTIONS]]"

    parser.separator ""

    parser.on("--rabbit-uri=URI",
              _("Rabbit's dRuby URI"),
              "(#{options.rabbit_uri})") do |uri|
      options.rabbit_uri = uri
    end

    parser.on("--port=PORT", Integer,
              _("Use PORT as Web server port"),
              "(#{options.port})") do |port|
      options.port = port
    end

    parser.on("-e", "--environment=ENVIRONMENT",
              _("Use ENVIRONMENT as Rack application environment.")) do |environment|
      ENV["RACK_ENV"] = environment
    end

    parser.on("--[no-]bonjour",
              _("Use service discovery for bonjour"),
              "(#{options.bonjour})") do |bool|
      options.bonjour = bool
    end
  end
end

options, logger = parse.call

ENV["RABBIT_URI"] = options.rabbit_uri

if options.bonjour
  require "easyjour"
  jour_title = "Rabbit - #{rabbit.title}"
  jour = Easyjour::Service.new(jour_title, "http", options.port)
end

rack_runner = ARGV.first
case rack_runner
when "shotgun"
  require "shotgun"
  shotgun_rb = nil
  $LOAD_PATH.each do |path|
    shotgun_rb = File.join(path, "shotgun.rb")
    if File.exist?(shotgun_rb)
      break
    else
      shotgun_rb = nil
    end
  end
  shotgun_base_dir = File.dirname(File.dirname(shotgun_rb))
  shotgun_bin = File.join(shotgun_base_dir, "bin", "shotgun")
  ARGV.shift
  ARGV.unshift("--public", public_path)
  ARGV.unshift("--port", options.port.to_s)
  ARGV << config_ru_path
  load shotgun_bin
else
  require "rack"
  ARGV.shift if rack_runner == "rackup"
  ARGV.unshift("--port", options.port.to_s)
  if Rack.release >= "1.3"
    ARGV.unshift("--option", "config=#{config_ru_path}")
  else
    ARGV.unshift(config_ru_path)
  end
  Rack::Server.start
end
