#! /usr/bin/env ruby

require 'irb'
require 'orocos'
require 'optparse'

parser = OptionParser.new do |opt|
    opt.on('--host=HOST', String, "the host we should connect to for naming") do |hostname|
        Orocos::CORBA.name_service.ip = hostname
    end
end

components = parser.parse(ARGV)
ARGV.clear

IRB.setup(nil)

Orocos.initialize

b = nil
Module.new do
    singleton = class << self; self; end
    components.each do |name|
        *task_name, var_name = name.split(':')
        var_name ||= task_name.last

        singleton.class_eval do
            attr_reader var_name
        end
        instance_variable_set("@#{var_name}", Orocos.name_service.get(task_name.join(":")))
    end

    include Orocos

    b = binding
end

ws  = IRB::WorkSpace.new(b)
irb = IRB::Irb.new(ws)
IRB.conf[:MAIN_CONTEXT] = irb.context

trap("SIGINT") do
    irb.signal_handle
end

catch(:IRB_EXIT) do
    irb.eval_input
end

