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
  # sqlite3 is an optional, unspecified, dependency and Rails 6.0 only supports `~> 1.4`
  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]
  else
    gem 'sqlite3', '~> 1.4', platforms: [:ruby]
  end
end

if RUBY_VERSION.to_f < 2.7
  gem 'puma', '< 6.0.0'
else
  gem 'puma'
end

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

  # Rails 8 requires 2.0.0
  gem 'sqlite3', '~> 2.0', platforms: [:ruby]
when /stable$/
  gem_list = %w[rails railties actionmailer actionpack activerecord activesupport activejob actionview]
  gem 'activerecord-jdbcsqlite3-adapter', git: 'https://github.com/jruby/activerecord-jdbc-adapter', platforms: [:jruby]

  gem_list.each do |rails_gem|
    gem rails_gem, :git => "https://github.com/rails/rails.git", :branch => version
  end
  add_sqlite3_gem_dependency
when nil, false, ""
  gem "rails", "~> 7.0.0"
  gem 'activerecord-jdbcsqlite3-adapter', platforms: [:jruby]
  gem 'selenium-webdriver', require: false
  add_sqlite3_gem_dependency
else
  version_number = version.split(' ').last
  add_net_gems_dependency if version_number < '7.0'

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

  # This version number reflects our build version not our actual version,
  # this will be fixed in 7.1.4 and should be removed then.
  if version_number == '7.1.0'
    gem 'selenium-webdriver', '< 4.20.0', require: false
  else
    gem 'selenium-webdriver', require: false
  end
  add_sqlite3_gem_dependency
end
