# typed: true
# frozen_string_literal: true

module UnpackStrategy
  # Strategy for unpacking RAR archives.
  class Rar
    include UnpackStrategy

    using Magic

    def self.extensions
      [".rar"]
    end

    def self.can_extract?(path)
      path.magic_number.match?(/\ARar!/n)
    end

    def dependencies
      @dependencies ||= [Formula["unrar"]]
    end

    private

    def extract_to_dir(unpack_dir, basename:, verbose:)
      system_command! "unrar",
                      args:    ["x", "-inul", path, unpack_dir],
                      env:     { "PATH" => PATH.new(Formula["unrar"].opt_bin, ENV["PATH"]) },
                      verbose: verbose
    end
  end
end
