#! /usr/bin/env ruby

require 'vizkit'
require 'optparse'
require 'rock/bundle'

@show_all = false
@proxy = true
@clean = false
Bundles.public_logs = false 

parser = OptionParser.new do |opt|
    opt.banner = "rock-display [--host hostname] task_name[.port_name] [task_name[.port_name]]"
    opt.on('--host=HOSTNAME', String, "the corba nameserver we should contact") do |name|
        Orocos::CORBA.name_service.ip = name
    end
    opt.on('--debug',"Show debug information") do
        Vizkit.logger.level = Logger::DEBUG
        Typelib.logger.level = Logger::DEBUG
        Bundles.public_logs = true
    end
    opt.on('-c','--clean',"Removes dangling corba tasks from the name service.") do
        @clean = true
    end
    opt.on('-M', '--maxMessageSize SIZE', 
           'maximum size of a message transported via corba (e.g. 2145, 3K, 3.5M, 0.1G)') do |size|
        size_match = /^(\d+(\.\d+)?)([MGK])?$/.match(size)
        if !size_match
            Vizkit::warn "cannot parse #{size} for a message size value"
            return
        end
        factors = {'K' => 1024, 'M' => 1024**2, 'G'=> 1024**3}
        factor = size_match[3] ? factors[size_match[3]] : 1
        value =  (size_match[1].to_f * factor).to_i
        Vizkit::info "set corba max message size to #{value}"
        Orocos::CORBA::max_message_size=value
    end
    opt.on('-M', '--maxMessageSize SIZE', 
           'maximum size of a message transported via corba (e.g. 2145, 3K, 3.5M, 0.1G)') do |size|
        size_match = /^(\d+(\.\d+)?)([MGK])?$/.match(size)
        if !size_match
            Vizkit::warn "cannot parse #{size} for a message size value"
            return
        end
        factors = {'K' => 1024, 'M' => 1024**2, 'G'=> 1024**3}
        factor = size_match[3] ? factors[size_match[3]] : 1
        value =  (size_match[1].to_f * factor).to_i
        Vizkit::info "set corba max message size to #{value}"
        Orocos::CORBA::max_message_size=value
    end
    opt.on('--help') do
        puts parser
        exit 0
    end
end

def corba_error
    Vizkit.error "Corba error. Maybe Orocos is not initialized"
    Vizkit.error "or the corba name service is answering on a wrong network interface."
    Vizkit.error "Try to reset the nameserver and to disable all virtual network interfaces."
end

remaining = parser.parse(ARGV)
begin
    Bundles.initialize
    Orocos::CORBA.cleanup if @clean
rescue Orocos::CORBAError => e
    Vizkit.error "Corba name service is not answering. Cannot start rock-display."
    Vizkit.error ""
    corba_error
    exit 1
end

task_inspector,widget = nil,nil
if remaining.empty?
    task_inspector = Vizkit.task_inspector
    task_inspector.show
    task_inspector.raise
else
    remaining.each do |spec|
        task_name, port_name = spec.split('.')
        if !task_name
            Vizkit.warn "Wrong parameter. Ignoring empty task name"
            next
        end

        task  = Orocos.name_service.get(task_name)
        proxy = task.to_proxy
        if !port_name
            task_inspector ||= Vizkit.default_loader.TaskInspector
            task_inspector.add_task(task)
            task_inspector.show
            task_inspector.raise
        else
            begin
                port = task.port(port_name)
                port.wait
                widget = Vizkit.display port
                widget.raise
                Vizkit.warn "Cannot find a display widget for #{port_name}" unless widget
            rescue Orocos::NotFound => e
                Vizkit.warn "Skipping #{task_name}.#{port_name}: cannot be found"
            end
        end
    end
    exit 1 if !widget && !task_inspector
end
Vizkit.exec

