#!/usr/bin/ruby.ruby4.0 
# frozen_string_literal: true

# This is the main script to execute the Aspera CLI

old_verbose = $VERBOSE
$VERBOSE = nil
# internal representation of strings (ruby) is UTF-8
Encoding.default_internal = Encoding::UTF_8
# external representation of strings (terminal, files)
Encoding.default_external = Encoding::UTF_8
$VERBOSE = old_verbose

require 'aspera/log'
require 'aspera/cli/info'

# Early debug for parser
# Note: does not accept short option names, nor extended values
Aspera::Log.instance.program_name = Aspera::Cli::Info::CMD_NAME
ARGV.each do |arg|
  case arg
  when '--' then break
  when /^--log-level=(.*)/ then Aspera::Log.instance.level = Regexp.last_match(1).to_sym
  when /^--log-format=(.*)/ then Aspera::Log.instance.formatter = Regexp.last_match(1) unless Regexp.last_match(1).start_with?('@ruby:')
  when /^--logger=(.*)/ then Aspera::Log.instance.logger_type = Regexp.last_match(1).to_sym
  end
rescue => e
  $stderr.puts("Error: #{e}") # rubocop:disable Style/StderrPuts
  Process.exit(1)
end

require 'aspera/coverage'
require 'aspera/environment'
require 'aspera/cli/main'
Aspera::Environment.instance.fix_home
Aspera::Cli::Main.new(ARGV).process_command_line
