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

$:.unshift File.expand_path('../lib', __dir__)

require 'rubybench_runner'
require 'optparse'

RUN = "run"
HELP = "help"

command = ARGV[0]
repo = ""
script = ""

if command == RUN
  repo, script = ARGV[1].split("/", 2)
end

options = {}
OptionParser.new do |opts|
  opts.banner = <<~BANNER
    Usage: rubybench_runner [command] [args] [options]

    commands:
      * #{RUN} <repo_name>/<script_name> [options]
        <repo_name> is one the repos that are benchmarked on rubybench.org. Valid options are: `rails`
        <script_name> is the name of the benchmark from rubybench.org e.g. bm_activerecord_finders_find_by_attributes.rb.

      * #{HELP}
        Show help

    Available options:
  BANNER
  opts.on("--db=DB", "Select database to use for the benchmarks. Valid options are: `postgres` and `mysql`") do |v|
    v = v.strip.downcase
    pg_options = %w{pg postgres postgresql}
    mysql_options = %w{mysql mysql2}
    if pg_options.include?(v)
      options[:db] = "postgres"
    elsif mysql_options.include?(v)
      options[:db] = "mysql2"
    else
      puts <<~OUTPUT
        Invalid --db value \"#{v}\". Valid options are: (case insensitive)
          #{pg_options.first} (aliases: #{pg_options[1..-1].join(", ")})
          #{mysql_options.first} (aliases: #{mysql_options[1..-1].join(", ")})
      OUTPUT
      exit 1
    end
  end

  opts.on("-c", "--repeat-count=COUNT", Integer, "Number of times to run the benchmark (default 1)") do |c|
    options[:repeat_count] = c
  end

  opts.on("--skip-dep-check", "Skip dependency checks performed before the benchmarks are run") do
    options[:skip_dependencies_check] = true
  end

  opts.on("-r", "--round=ROUND", Integer, "Round results to the nearest value with a precision of N digits (default 2)") do |r|
    options[:round] = r
  end

  opts.on("--quiet", "Be quite when the the benchmarks are being run") do
    options[:quiet] = true
  end

  opts.on("--verbose", "Show extra informative output while the benchmarks are running") do
    options[:verbose] = true
  end

  opts.on("--fresh", "Remove installed gems from previous runs and reinstall them for this run") do
    options[:fresh_run] = true
  end

  opts.on("--cleanup", "Remove gems installed during this run after the run is complete") do
    options[:cleanup] = true
  end

  opts.on("--wps", "--with-prepared-statements", "Run benchmarks with prepared statements") do
    options[:wps] = true
  end

  opts.on("-v", "--version", "Display installed version of RubybenchRunner") do
    puts RubybenchRunner::VERSION
    exit 0
  end

  opts.on("-h", "--help", "Show help") do
    puts opts
    exit 0
  end

  opts.on("--url=URL", "Direct link to a benchmark script") do |url|
    options[:url] = url
  end

  RubybenchRunner::SUPPORTED_REPOS.each do |name, _|
    if repo == name.to_s
      opts.on("--#{name}=PATH", "Provide local path to #{name}") do |path|
        options[name] = File.expand_path(path)
      end
    end
  end
end.parse!

if command == RUN
  RubybenchRunner.run(repo, script, options)
elsif command == HELP
  puts opts
else
  puts "Unknown command #{command}"
  exit 1
end

# "https://github.com/ruby-bench/ruby-bench-suite/blob/master/rails/benchmarks/bm_activerecord_destroy.rb"
