#!/usr/bin/ruby -rubygems
#
# Usage information at the end of the script.
#

require 'getoptlong'
require 'custodian/parser'
require 'custodian/queue'
require 'custodian/settings'


if RUBY_VERSION =~ /1.9/
  Encoding.default_external = Encoding::UTF_8
  Encoding.default_internal = Encoding::UTF_8
end

#
#  Entry-point to our code.
#
if __FILE__ == $PROGRAM_NAME

  help   = false
  manual = false

  begin
    opts = GetoptLong.new(
      ['--dump', '-d', GetoptLong::NO_ARGUMENT],
      ['--test', GetoptLong::NO_ARGUMENT],
      ['--file', '-f', GetoptLong::REQUIRED_ARGUMENT],
      ['--help', '-h', GetoptLong::NO_ARGUMENT],
      ['--manual', '-m', GetoptLong::NO_ARGUMENT]
    )
    opts.each do |opt, arg|
      case opt
      when '--dump' then
        ENV['DUMP'] = '1'
      when '--test' then
        ENV['TEST'] = '1'
      when '--file' then
        ENV['FILE'] = arg
      when '--help' then
        help = true
      when '--manual' then
        manual = true
      end
    end
  rescue StandardError => ex
    puts "Option parsing failed: #{ex}"
    exit
  end

  #
  #  Show the help information.
  #
  if manual || help
    DATA.read.split("\n").each do |line|
      puts Regexp.last_match(1).dup if line =~ /^# ?(.*)/
    end
    exit 0
  end

  #
  # Connected to the queue.
  #
  queue = Custodian::RedisQueueType.new
  unless queue
    puts "Failed to connect to the #{settings.queue_type} queue"
    exit 1
  end

  #
  # Create the parser object.
  #
  mon = Custodian::Parser.new

  #
  # Parse our configuration file.  If there are errors then we'll
  # exit this script.
  #
  begin
    mon.parse_file(ENV['FILE'])
  rescue => e
    puts "Failure in parsing the configuration file : #{ENV['FILE']}"
    puts e.to_s
    exit(1)
  end

  #
  #  Did we fail to add any tests to the queue?
  #
  failed = false

  mon.jobs.each do |test|
    if ENV['TEST']
      # nop
    elsif ENV['DUMP']
      puts test
    else
      failed = true unless queue.add(test.to_s)
    end
  end

  if failed
    puts 'We failed to add at least one job to the queue.'
    exit(1)
  end
end


__END__
#
# NAME
#  custodian-enqueue - Parse tests from a file and enqueue them.
#
# SYNOPSIS
#  custodian-enqueue  [ -h | --help ]
#                     [ -m | --manual]
#                     [ -q | --queue NAME]
#                     [ -f | --file FILE]
#                     [ -d | --dump ]
#                     [    | --test ]
#
# OPTIONS
#
#  -h, --help          Show a help message, and exit.
#
#  -m, --manual        Show this manual, and exit.
#
#  -d, --dump          Dump the parsed tests to the console.
#                      (They are not inserted to the queue.)
#
#  --test              Test the parsing of the given file, alert on errors.
#
#  -f, --file FILE     Parse the given configuration file.
#
#
#
# ABOUT
#
# This tool reads a single configuration file and parses it into a
# series of network & protocol tests.   These tests are then stored in
# a queue from which workers can retrieve and execute them.
#
# The dequeing process may occur up numerous other hosts.
#
# CONFIGURATION FILE
#
# The configuration file is 99% compatible with that used in the tool
# custodian replaces.
#
#
# AUTHOR
#
#  Steve Kemp  <steve@bytemark.co.uk>
#