#! /usr/bin/env ruby
#
require 'orocos'
require 'optparse'
Orocos.initialize

gui = false
optparse = OptionParser.new do |opt|
    opt.banner = "run [--gui] DEVICE"
    opt.on '--gui', "starts the Rock task inspection widget on the running task" do
        gui = true
    end
end

dev_id = *optparse.parse(ARGV)
if !dev_id
    puts optparse
    exit 1
end

if gui
    require 'vizkit'
end

Orocos.run 'ucm_schilling::Task' => 'ucm' do
    Orocos.logger.level = Logger::DEBUG
    Orocos.log_all
    puts "deployed the ucm_schilling::Task task"

    ucm = Orocos::TaskContext.get 'ucm'
    ucm.io_read_timeout = Time.at(2);
    Orocos.apply_conf_file(ucm, 'ucm.yml', [dev_id])
    ucm.configure
    ucm.start

    if gui
        task_inspector = Vizkit.default_loader.task_inspector
        task_inspector.config(ucm)
        task_inspector.show
        Vizkit.exec
    else
	reader = ucm.ucm_samples.reader
        Orocos.watch(ucm) do
            if sample = reader.read
                pp sample
            end
        end
    end
end

