#!/usr/bin/env ruby
# Vendors the MIME type config from the mime-db list
# usage: script/vendor-mimes

require 'colorator'
require 'json'
require 'open-uri'

# ---- Helpers ----

{
  :info    => :cyan,
  :success => :green,
  :error   => :red,
}.each do |type, color|
  define_method("log_#{type}") do |msg|
    puts "  #{msg}".send(color)
  end
end

# ----

json = begin
  log_info "Reading remote data.."
  URI.open("https://raw.githubusercontent.com/jshttp/mime-db/master/db.json").read
rescue StandardError => e
  log_error "Error reading remote data!"
  log_error e.message
  log_error "Aborting."
  exit 1
end

log_info "Parsing remote data.."
data = JSON.parse(json)
data.reject! { |mime, meta| meta["extensions"].nil? || meta["extensions"].empty? }

log_info "Generating interim mime data-hashes.."
mimes = {}
charset_data = {}
data.each do |mime, meta|
  # Normalize extensions and mime-types
  mime = mime.downcase.strip
  extensions = meta["extensions"].map { |e| e.downcase.strip }.compact

  # If a given extension is listed multiple times, prefer the first one listed
  extensions.reject! { |extension| mimes.values.flatten.include?(extension) }

  next if extensions.empty?
  mimes[mime] = [] if mimes[mime].nil?
  mimes[mime].concat extensions

  # Extract mime-types with "charset" metadata
  charset_data[mime] = meta["charset"] if meta.key?("charset")

  # Assign `UTF-8` charset for mime-types under the `text` domain if not already assigned upstream
  charset_data[mime] ||= "UTF-8" if mime.start_with?("text/")
end

log_info "Formatting primary hash and writing to file.."
strlen = mimes.keys.max_by(&:length).length
output = ""
output << "# Woah there. Do not edit this file directly.\n"
output << "# This file is generated automatically by script/vendor-mimes.\n\n"
mimes = mimes.sort_by { |k,v| k }
output << mimes.map { |mime,extensions| "#{mime.ljust(strlen)} #{extensions.sort.join(" ")}" }.join("\n")

config = File.expand_path "../lib/jekyll/mime.types", __dir__
File.write(config, output)
log_info "Done! See: #{config.inspect.white}"

# --- Generate JSON file from charset_data ----
puts

log_info "Dumping mimetype-charset mapping as JSON.."
json_file = File.expand_path "../lib/jekyll/commands/serve/mime_types_charset.json", __dir__
File.write(json_file, JSON.pretty_generate(charset_data) + "\n")
log_success "and done! See: #{json_file.inspect.white}"
