=begin
 ******************************************************************************
 * Copyright 1990-2009 MBARI
 * MBARI Proprietary Information. All rights reserved.
 ******************************************************************************
 * Summary  : Run-time configuration manager.
 * Filename : runtime_config.rb
 * Author   : Henthorn
 * Project  : Benthic Rover
 * Version  : 1
 * Created  : Nov 2008
 * Modified : 
 ******************************************************************************
=end

##
# Refer to Rover-code home directory as base
#
require "#{ENV['ROVER_HOME']}/utils/rover_environment"
#

##
# RuntimeConfig provides simple interface to run-time configuration items.
#
# Hardware configuration items = Items that describe the instruments installed
#                                on the vehicle.
# Run-time configuration items = Items that don't belong in Behaviors but that
#                                likely change more often than hardware items.
#

class ConfigException < Exception
end

class RuntimeConfig

# Create all the configuration items here and provide default values.
# when appropriate. Both the item and value should always be strings,
# except when an item has no default value and must be specified.
# In those cases, enter nil as the default value (not "nil").
# Use dash-dot notation style ("system-component.setting", "value").
#
  @items = {
                                       # Video stuff
    "video.ip"=>nil,                   # Video ip (in.dot.notation)
    "video.boot-time"=>"35",           # Video boot time (in seconds)

                                       # Other stuff

    "last"=>"item"                     # Place-holder for last item in list
    }


  ##
  # Change the default item values in the initalization
  #
  def initialize
    # Here are the configuaration items
    #
    @items = {
                                         # Video stuff
      "video.ip"=>"134.89.12.147",       # Video ip (in.dot.notation)
      "video.boot-time"=>"30",           # Video boot time (in seconds)

                                         # Other stuff

      "last"=>"item"                     # Place-holder for last item in list
    }

    # Perform consistency check
    #
    @items.each do |key,val|
      raise (Exception.new("'#{key}' must have a value")) unless (val)
    end

  end

  ##
  # Retreive the value for the given item name.
  # Raises a ConfigException if the item name is not in the list.
  #
  #        key:str
  def get(p_item_name)
    unless (p_item_name)
      raise (ConfigException.new("nil passed as key"))
    end
    unless (value = @items[p_item_name.to_s])
      raise (ConfigException.new("Unknown item: #{p_item_name}"))
    end
    value
  end

end

##
# Standalone unit test (recommended)
# Include at the end of the file as a hook to run a unit test of
# the code from the command line (e.g., "$ ruby  my_class.rb")
#
if __FILE__ == $0
  # Test code here
  begin
    rt = RuntimeConfig.new
    puts("last = #{rt.get("last")}")
    puts("last = #{rt.get("not.there")}")  # exception
    rt.get(nil)
  rescue ConfigException => no_item
    puts(no_item)
  end
  begin
    rt.get(nil)                            # exception
  rescue ConfigException => no_item
    puts(no_item)
  end

end
