=begin
 ******************************************************************************
 * Copyright 1990-2009 MBARI
 * MBARI Proprietary Information. All rights reserved.
 ******************************************************************************
 * Summary  : Maintain the run-time options for a Rover mission deployment
 * Filename : options.rb
 * Author   : Henthorn
 * Project  : Benthic Rover
 * Version  : 1
 * Created  : Dec 08
 * Modified : 
 ******************************************************************************
=end

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

##########
# Modeled after the old "clo" (command-line-options) class of earlier versions.
# Singleton class can be instantiated anytime during a mission or test.
#
class Options
  include Singleton

  attr_accessor :sim, :warp, :deployment_file, :resume

  #####
  # Parse the options from the command line using the GetOptLong module
  #
  def initialize
    # Defaults
    @deployment_file = nil
    @sim = false
    @warp = false
    @resume = false

    parse
  end
  #####

  #####
  # Display the proper usage. Must be in-sync with parse method.
  #
  def usage
    puts("supervisor.rb usage:")
    s = "  ruby supervisor.rb filename [--sim | -s] [--warp | -w] " +
        "[--resume | -r] [--help | -h | -?]"
    puts(s)
    puts("  filename              Name of deployment file (or \"test\")")
    puts("  [--sim | -s]          Run in simulation mode")
    puts("  [--warp | -w]         Warp extended wait times to 1 second")
    puts("  [--resume | -r]       Resume hibernating deployment")
    puts("  [--monitor | -m]      Monitor threads")
    puts("  [--help | -h | -?]    Print this usage text")
  end
  #####


  #####
  # Use Ruby GetoptLong class to parse command-line. Set state of
  # elements based on values and switches on the command-line.
  # 
  def parse
  
    # List expected/allowed command-line options here
    #
    opts = GetoptLong.new(
                          ["--sim",        "-s",  GetoptLong::NO_ARGUMENT],
                          ["--warp",       "-w",  GetoptLong::NO_ARGUMENT],
                          ["--resume",     "-r",  GetoptLong::NO_ARGUMENT],
                          ["--monitor",    "-m",  GetoptLong::NO_ARGUMENT],
                          ["--help", "-?", "-h",  GetoptLong::NO_ARGUMENT]
                          )

    # Iterate through options, set state of elements
    #
    opts.each do |opt, argm|
      case opt
        # Get simulation, monitor, help, and warp flags
        #
        when '--sim'
        @sim = true
        when '--warp'
        @warp = true
        when '--resume'
        @resume = true
        when '--monitor'
        @monitor_threads = true
        when '--help'
        usage
        exit
        else
        puts("Unknown option: #{opt}")
        exit
      end
    end

    # Whatever is leftover should be the required deployment filename
    #
    @deployment_file = ARGV[0]
    unless (@deployment_file || @resume)
      puts("ERROR: No deployment file specified")
      puts("Use deployment file of \"test\" to run supervisor test")
      usage()
      exit
    end
  end
  
  # Create and return summary string
  #
  def to_s
    s = ""
    s += "  :Deployment file = "
    s += "#{@deployment_file}\n" if @deployment_file
    s += "none specified\n"      unless @deployment_file
    s += "  :Sim    = #{@sim}\n"
    s += "  :Warp   = #{@warp}\n"
    s
  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
  Options.instance.usage
  puts("ARGV is: " + ARGV.to_s)

  # Push a dummy deployment file onto the argument list
  Options.instance.parse
  puts(Options.instance.to_s)
end
