#!/usr/bin/env ruby
# frozen_string_literal: true

#--
# Copyright (c) 2011 SUSE LINUX Products GmbH
#
# Author: Duncan Mac-Vicar P. <dmacvicar@suse.de>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#++
$LOAD_PATH.push(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'bicho'
require 'highline/import'

# Set the console color scheme
ft = HighLine::ColorScheme.new do |cs|
  cs[:headline]        = [:bold]
  cs[:horizontal_line] = [:bold, :white]
  cs[:even_row]        = [:green]
  cs[:odd_row]         = [:magenta]
  cs[:error]           = [:red]
  cs[:remove] = [:red]
  cs[:add] = [:green]
  cs[:changeset] = [:blue]
end

HighLine.color_scheme = ft
# Our 'terminal' object
t = HighLine.new

# Scan all installed pluggable commands
Dir.glob(File.join(File.dirname(__FILE__), '..', 'lib', 'bicho', 'cli', 'commands', '*.rb')).each do |cmd|
  load cmd
end

# Setup a logger and connect it with stupid libraries
# that can't use a logger
Bicho::Logging.logger = Logger.new(STDERR)
# Don't show by default anything
Bicho::Logging.logger.level = Logger::FATAL

ret = 0
begin
  # create subcommands automatically from the available
  # symbols in BzConsole module
  SUB_COMMANDS = Bicho::CLI::Commands.constants.sort.map(&:to_s).map(&:downcase)

  global_opts = Optimist.options do
    banner <<-EOS
    Usage: #{File.basename(__FILE__)} [global options] <command> ...

    Bugzilla url can be given as https://bugs.kde.org or bko
EOS
    opt :config, 'Read FILE as configuration file.', type: :io
    opt :debug, 'Show debug messages', default: false
    opt :bugzilla, 'Bugzilla URL or alias', type: :string
    stop_on SUB_COMMANDS
  end

  Bicho::Logging.logger.level = Logger::DEBUG if global_opts[:debug]

  # get the subcommand
  cmd = begin
          ARGV.shift
        rescue
          nil
        end

  if !cmd || !SUB_COMMANDS.include?(cmd)
    Optimist.die "available subcommands: #{SUB_COMMANDS.join(' ')}"
  end

  # Create an instance for the command
  mod = Bicho::CLI::Commands
  cmd_class = mod.const_get(cmd.capitalize)
  cmd_instance = cmd_class.new
  opts = cmd_instance.parse_options

  ret = cmd_instance.do(global_opts, opts, ARGV)
rescue StandardError => e
  Bicho::Logging.logger.error e.message
  Bicho::Logging.logger.error e.backtrace
  t.say("#{t.color('ERROR: ', :error)} #{t.color(e.message, :bold)}")
  ret = 1
end
exit ret
