version_file = File.expand_path("../.rails-version", __FILE__)

# This is required for Ruby 3.1, because in Ruby 3.1 these gems were moved to
# bundled gems from default gems. This issue was fixed in Rails Rails 7.0.1.
# Discussion can be found here - https://github.com/mikel/mail/pull/1439
def add_net_gems_dependency
  if RUBY_VERSION >= '3.1'
    gem 'net-smtp', require: false
    gem 'net-imap', require: false
    gem 'net-pop', require: false
  end
end

def add_sqlite3_gem_dependency(rails_version)
  # sqlite3 is an optional, unspecified, dependency and Rails 8.0 only supports `~> 2.0`
  if RUBY_VERSION.to_f < 3
    # sqlite3 1.7.x doesn't work on all platforms for Ruby 2.x
    gem 'sqlite3', '~> 1.4', '< 1.7', platforms: [:ruby]
  elsif rails_version.to_f >= 8
    gem 'sqlite3', '~> 2.0', platforms: [:ruby]
  else
    gem 'sqlite3', '~> 1.7', platforms: [:ruby]
  end
end

gem 'puma'

case version = ENV['RAILS_VERSION'] || (File.exist?(version_file) && File.read(version_file).chomp) || ''
when /main/
  gem "rails", :git => "https://github.com/rails/rails.git"
  gem 'activerecord-jdbcsqlite3-adapter', git: 'https://github.com/jruby/activerecord-jdbc-adapter', platforms: [:jruby]
  gem 'selenium-webdriver', require: false

  gem 'sqlite3', '~> 2.0', platforms: [:ruby]
when nil, false, ""
  if RUBY_VERSION.to_f > 3.1
    gem "rails", "~> 8.0.0"
    gem 'sqlite3', '~> 2.0', platforms: [:ruby]
  else
    gem "rails", "~> 7.2.0"
    gem 'sqlite3', '~> 1.7', platforms: [:ruby]
  end

  gem 'activerecord-jdbcsqlite3-adapter', platforms: [:jruby]
  gem 'selenium-webdriver', require: false
else
  version_number = version.split(' ').last

  gem "rails", version
  gem 'activerecord-jdbcsqlite3-adapter', platforms: [:jruby]

  gem 'selenium-webdriver', require: false
  add_sqlite3_gem_dependency(version_number)
end
