#!/usr/bin/env ruby -w
# @author Eric Weinstein <eric.q.weinstein@gmail.com>

require 'optparse'
require_relative '../lib/ruumba'
require_relative '../lib/ruumba/version'

options = { arguments: [] }
opts_parser = OptionParser.new do |opts|
  opts.version = Ruumba::Version::STRING

  opts.banner = 'Usage: ruumba path/to/ERBs/'

  opts.on('-h', '--help', 'Display this screen') do
    puts opts_parser
    exit
  end

  opts.on('-a', '--auto-gen-config', 'Generate a configuration file acting as a TODO list.') do
    options[:arguments] << '--auto-gen-config'
  end

  opts.on('--exclude-limit COUNT', 'Used together with --auto-gen-config to set the limit for how many Exclude properties to generate. Default is 15.') do |count|
    options[:arguments] << '--exclude-limit'
    options[:arguments] << count
  end

  opts.on('--no-offense-counts', 'Do not include offense counts in configuration file generated by --auto-gen-config.') do
    options[:arguments] << '--no-offense-counts'
  end

  opts.on('-t', '--tmp-folder [TEMP_FOLDER]', 'Use this suffix for the temporary folder') do |f|
    options[:tmp_folder] = f
  end

  opts.on('-c', '--config [CONFIG]', 'Use this config for rubocop') do |c|
    options[:arguments] << '--config'
    options[:arguments] << File.expand_path(c)
  end

  opts.on('-C', '--cache FLAG', "Use result caching (FLAG=true) or don't", '(FLAG=false), default determined by', 'configuration parameter AllCops: UseCache.') do |flag|
    options[:arguments] << '--cache'
    options[:arguments] << flag
  end

  opts.on('-d', '--debug', 'Display debug info.') do
    options[:arguments] << '--debug'
  end

  opts.on('-D', '--display-cop-names', 'Display cop names') do
    options[:arguments] << '--display-cop-names'
  end

  opts.on('-F', '--fail-level [LEVEL]', 'Rubocop fail level') do |l|
    options[:arguments] << '--fail-level'
    options[:arguments] << l
  end

  opts.on('-e', '--disable-ruby-ext', 'Disable auto-append of .rb extension') do
    options[:disable_rb_extension] = true
  end

  opts.on('--only [COP1,COP2,...]', 'Run only the given cop(s).') do |cops|
    options[:arguments] << '--only'
    options[:arguments] << cops
  end

  opts.on('-l', '--lint', 'Run only lint cops.') do
    options[:arguments] << '--lint'
  end

  opts.on('-r', '--require [FILE]', 'Require Ruby file.') do |file|
    options[:arguments] << '--require'
    options[:arguments] << file
  end

  opts.on('-o', '--out [FILE]', 'Write output to a file instead of STDOUT.') do |file|
    options[:arguments] << '--out'
    options[:arguments] << file
  end

  opts.on('-f', '--format [FORMAT]', 'Choose an output formatter.') do |format|
    options[:arguments] << '--format'
    options[:arguments] << format
  end

  opts.on('-s', '--stdin [FILE]', 'Pipe source from STDIN, using FILE in offense reports. This is useful for editor integration') do |file|
    options[:stdin] = file
  end

  opts.on('--force-exclusion', 'Force excluding files specified in the configuration `Exclude` even if they are explicitly passed as arguments.') do
    options[:arguments] << '--force-exclusion'
  end

  opts.on('-P', '--parallel', 'Use available CPUs to execute inspection in parallel.') do
    options[:arguments] << '--parallel'
  end

  opts.on('-L', '--list-target-files', 'List all files Ruumba will inspect.') do
    options[:arguments] << '--list-target-files'
  end

  opts.on('-a', '--auto-correct', 'Auto-correct offenses.') do
    options[:arguments] << '--auto-correct'
    options[:auto_correct] = true
  end
end

begin
  opts_parser.parse!
rescue OptionParser::InvalidOption => e
  abort "An error occurred: #{e}. Run ruumba -h for help."
end

if !options[:arguments].include?('--config') && File.exist?('.ruumba.yml')
  options[:arguments] << '--config'
  options[:arguments] << File.expand_path('.ruumba.yml')
end

analyzer = Ruumba::Analyzer.new(options)
exit(analyzer.run)
