#!/usr/bin/env jruby
require 'rubygems'
require 'optparse'
require 'stringio'
require 'rspec'
require 'rbconfig'
require 'operawatir'

@options = {
  :manual         => false,
  :ng             => false,
  :color_enabled  => true,
  :check_syntax   => false,
  :output_stream  => IO.new(1, 'w'),
  :format         => 'progress',
  :no_quit        => false,
  :opera_idle     => false,
  #:full_backtrace => false  # TODO: Provide fix for RSpec
}

# TODO
#   Should steal https://github.com/rspec/rspec-core/blob/master/lib/rspec/core/option_parser.rb

begin
  OptionParser.new do |opts|
    opts.banner = <<EOS
Usage: operawatir [-m|--manual] [-l|--launcher=BINARY] [--binary=BINARY]
       [-a|--args=ARGUMENTS] [-q|--no-quit] [--opera-idle] [-b|--backtrace]
       [--no-color] [-t|--tag=TAG] [-f|--format=FORMAT] [-o|--out=FILE]
       [-d|--debug=LEVEL] [--log=FILE] [-h|--help] [-v|--version] FILES
EOS

    opts.separator ''
    opts.separator 'Specific options:'

    opts.on('-m', '--manual', 'Wait for a manual connection from opera:debug') do |c|
      @options[:manual] = c
    end

    opts.on('-l', '--launcher=BINARY', 'Path to launcher binary, will use environmental ',
            'variable OPERA_LAUNCHER if not specified') do |c|
      @options[:launcher] = c
    end

    opts.on('--binary=BINARY', 'Browser to run the test with, will use guess the ',
            'path or use environmental variable OPERA_PATH if ',
            'not specified') do |e|
      @options[:path] = e
    end

    opts.on('-a', '--args=ARGUMENTS', 'Arguments passed to the binary, will override  ',
            'environmental variable OPERA_ARGS') do |a|
      @options[:args] = a
    end

    opts.on('-q', '--no-quit', 'Disable quitting of Opera at the end of a test run') do |c|
      @options[:no_quit] = true
    end

    opts.on('--opera-idle', 'Enable OperaIdle') do |c|
      @options[:opera_idle] = true
    end

    opts.on('-b', '--backtrace', 'Enable full backtrace') do |c|
      @options[:full_backtrace] = true
    end

    opts.on('--no-color', 'Disable colorized output') do |c|
      @options[:color_enabled] = false
    end

    opts.on('-t', '--tag=TAG',
            'Specify tags to only run examples with the specified',
            'tag, to exclude examples, add ~ before the tag (e.g.',
            "`~slow')") do |tag|
      @options[:filter_run] = tag
    end

    opts.on('-o', '--out=FILE', 'Send output to a file instead of STDOUT') do |o|
      @options[:output_stream] = File.open(o, 'w') or
        abort "operawatir: Unable to write to file `#{o}'"
    end

    opts.on('-f', '--format=FORMAT',
            'Specify RSpec output formatter (documentation, ',
            'html, progress (default), textmate)') do |formatter|
      @options[:formatter] = formatter
    end

    opts.separator ''
    opts.separator 'Common options:'

    opts.on_tail('-d', '--debug=LEVEL', 'The logging level to use.  Available levels: SEVERE',
                 '(highest),  WARNING, INFO (default), CONFIG, FINE,',
                 'FINER, FINEST (lowest), ALL') do |l|
      available_levels = ['SEVERE', 'WARNING', 'INFO', 'CONFIG', 'FINE', 'FINER', 'FINEST', 'ALL']
      raise OptionParser::InvalidArgument, l.to_s.inspect unless available_levels.include?(l)
      @options[:logging_level] = l
    end

    opts.on_tail('--log=FILE', 'Output the log to a file') do |f|
      @options[:logging_file] = f
    end

    opts.on_tail('-h', '--help', 'Show this message') do
      abort opts.to_s
    end

    opts.on_tail('-v', '--version', 'Show version') do
      abort "OperaWatir version #{OperaWatir.version}"
    end
  end.parse!(ARGV)
rescue OptionParser::ParseError => e
  abort "operawatir: #{e}"
end

if ARGV.empty?
  abort 'operawatir: You need to specify at least one test file to run'
else
  @options[:files_or_directories_to_run] = ARGV
end

require 'operawatir/helper'
OperaWatir::Helper.run! @options