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

interrupt_handler = ->(*) do
  $stderr.write "\nInterrupted. Exiting.\n"
  exit 130
end
Signal.trap('INT', &interrupt_handler)

# Make it safe to run via symlink by resolving the real path
root = File.expand_path('..', File.realpath(__FILE__))
lib  = File.join(root, 'lib')
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

# Fail fast with a helpful message if Ruby is too old for this gem
begin
  require 'rubygems'
  if Gem::Version.new(RUBY_VERSION) < Gem::Version.new('3.2')
    $stderr.puts "cov-loupe requires Ruby >= 3.2 (current: #{RUBY_VERSION})."
    $stderr.puts 'Please run with a supported Ruby version (e.g., via rbenv, rvm, asdf).'
    exit 1
  end
rescue
  # If anything goes wrong, let the app load and potentially fail with a more specific error.
end

begin
  require 'cov_loupe'

  CovLoupe.run(ARGV)
rescue Interrupt
  interrupt_handler.call
end
