#! /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
io_port = *optparse.parse(ARGV)
if !io_port
    puts optparse
    exit 1
end

if gui
    require 'vizkit'
end

Orocos.run 'ctd_seabird::Task' => 'ctd' do
#Orocos.run 'ctd_seabird_test' do
    Orocos.logger.level = Logger::DEBUG
    Orocos.log_all
    puts "deployed the ctd_seabird::Task task"

    ctd = Orocos::TaskContext.get 'ctd'
    ctd.io_port = io_port
    ctd.io_read_timeout = Time.at(2);
    ctd.configure
    ctd.start

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

