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

require 'tmpdir'

host = 'ac-ulwcmzm-shard-00-00.g6fyiaq.mongodb-dev.net'

output = `openssl s_client -showcerts -servername #{host} -connect #{host}:27017 </dev/null`

raise 'Something bad happened' if output.empty?

certs = output.scan(/(-----BEGIN CERTIFICATE(.|\n)+?END CERTIFICATE-----)/)
cert, ca_cert = certs.map { |g| g.first }

Dir.mktmpdir do |path|
  cert_path = File.join(path, 'cert.pem')
  File.open(cert_path, 'w') do |f|
    f << cert
  end
  output = `openssl x509 -noout -text -in #{cert_path}`
  File.open('atlas-ocsp.crt', 'w') do |f|
    f << output
    f << "\n"
    f << cert
  end

  cert_path = File.join(path, 'cert.pem')
  File.open(cert_path, 'w') do |f|
    f << ca_cert
  end
  output = `openssl x509 -noout -text -in #{cert_path}`
  File.open('atlas-ocsp-ca.crt', 'w') do |f|
    f << output
    f << "\n"
    f << ca_cert
  end
end
