#!/usr/bin/env ruby -w

$:.unshift(File.expand_path("../../lib", __FILE__))

require 'optparse'
require 'whois'


options = {}
OptionParser.new do |opts|
  opts.banner     = "Whois: an intelligent pure Ruby Whois client"
  opts.define_head  "Usage: ruby-whois [options] object"
  opts.separator    ""
  opts.separator    "Examples:"
  opts.separator    " ruby-whois ruby-lang.com"
  opts.separator    " ruby-whois 213.149.247.64"
  opts.separator    ""
  opts.separator    "Options:"

  opts.on("-t", "--timeout [SECONDS]", Integer, "set the timeout") do |seconds|
    options[:timeout] = seconds
  end

  opts.on("-h", "--host [HOST]", String, "connect to server HOST") do |host|
    options[:host] = host
  end

  opts.on("--[no-]referral", "skip referral queries") do |boolean|
    options[:referral] = boolean
  end

  opts.on_tail("--help", "show this help and exit") do
    puts opts
    exit
  end

  opts.on_tail("--version", "output version information and exit") do
    puts "#{Whois::NAME} #{Whois::VERSION}"
    exit
  end

  begin
    opts.parse!
  rescue OptionParser::ParseError
    puts opts
    exit 1
  end

  if ARGV.size.zero?
    puts opts
    exit 1
  end
end

object = ARGV.shift

begin
  @client = Whois::Client.new(options)
  puts @client.lookup(object)
rescue Whois::Error => e
  abort(e.message)
rescue Timeout::Error
  abort("Request Timeout")
end
