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

@options = {
  :ng             => false,
  :color_enabled  => true,
  :check_syntax   => false,
  :output_stream  => IO.new(1, 'w'),
  :format         => 'progress',
  :no_quit        => false,
  :no_restart     => false,
  :opera_idle     => false,
  :full_backtrace => false
}

# 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: desktopwatir [-l|--launcher=BINARY] [-e|--executable=BINARY]
       [-a|--args=ARGUMENTS] [--no-color] [-q|--no-quit] [-r|--no-restart]
       [--opera-idle] [-b|--backtrace] [-t|--tag=TAG] [-f|--format=FORMAT]
       [-o|--out=FILE] [-h|--help] [-v|--version] FILES
EOS

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

    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('-e', '--executable=BINARY', 'Browser to run the test with, will use environmental ',
            'variable OPERA_PATH if not specified') do |e|
      @options[:path] = e
    end

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

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

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

    opts.on('-r', '--no-restart', 'Disables automatic relaunching of Opera') do |c|
      @options[:no_restart] = 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('-c', '--check-syntax', 'Check syntax only') do |c|
    #  @options[:check_syntax] = true
    #end

    #opts.on('--ng', 'Connect to Nailgun server instead of starting out ',
    #        'own JVM') do |ng|
    #  @options[:ng] = ng
    #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 "desktopwatir: 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.on('-t', '--tag=TAG',
            'Specify tags to only run examples with the specified tag
            To exclude examples, add ~ before the tag (e.g. ~slow)
            (TAG is always converted to a symbol)') do |tag|
      @options[:filter_run] = tag
    end
 
    opts.separator ''
    opts.separator 'Common options:'

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

    opts.on_tail('-v', '--version', 'Show version') do
      abort "OperaWatir version #{OperaWatir.version}"
    end
  end.parse!(ARGV)
rescue OptionParser::InvalidOption => e
  abort "desktopwatir: Unknown option `#{e.to_s.sub(/invalid option:\s+/, '')}'"
end

if ARGV.empty?
  abort 'desktopwatir: You need to specify at least one test file to run'
else
  @options[:files_to_run] = ARGV
  @options[:files_to_run].map! { | f | Dir.glob(f) }.flatten!
end

#abort 'desktopwatir: --ng and --check-syntax are disabled for now' if @options[:ng] || @options[:check_syntax]

OperaWatir::DesktopHelper.run! @options