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

require 'rubygems'
require 'bundler/setup'
require 'optparse'

def rails_path(relname)
  Pathname.new(__FILE__).dirname.join('../..').join(relname).realpath
end

class GeoLogCursorOptionParser
  def self.parse(argv)
    options = {}

    op = OptionParser.new
    op.banner = 'GitLab Geo: Log Cursor'
    op.separator ''
    op.separator 'Usage: ./geo_log_cursor [options]'
    op.separator ''
    op.on('-d', '--debug', 'Enable detailed logging with extra debug information') { options[:debug] = true }

    op.separator 'Common options:'
    op.on('-h', '--help') do
      puts op.to_s
      exit
    end
    op.on('-v', '--version') do
      # Load only necessary libraries for faster startup
      require rails_path('ee/lib/gitlab/geo/log_cursor/daemon.rb')

      puts Gitlab::Geo::LogCursor::Daemon::VERSION
      exit
    end
    op.separator ''

    op.parse!(argv)

    options
  end
end

if $0 == __FILE__
  options = GeoLogCursorOptionParser.parse(ARGV)

  # We load rails environment / initializers only here to get faster command line startup when `--help` and `--version`
  require rails_path('config/environment.rb')

  Gitlab::Geo::LogCursor::Daemon.new(options).run!
end
