#!/usr/bin/env ruby

# create_default_gems_packages version 1.0 (for Chromebrew)
# This creates a default_gems package and a bundled_gems package based
# upon:
# https://stdgems.org/default_gems.json
# https://stdgems.org/bundled_gems.json
#
# Author: Satadru Pramanik (satmandu) satadru at gmail dot com
# Usage in root of cloned chromebrew repo with a new branch checked out:
# tools/create_default_gems_packages.rb

require 'fileutils'
require 'json'
require_relative '../lib/color'
require_relative '../lib/const'
require_relative '../lib/require_gem'

require_gem('httpparty')

def create_default_gems_package
  # https://stdgems.org/default_gems.json
  default_gem_json = JSON.parse(HTTParty.get('https://stdgems.org/default_gems.json').body)
  default_gems = default_gem_json['gems'].map { |i| i['gem'] }
  default_gems.delete('win32ole')

  puts 'Default Gems are:'
  puts default_gems
  dependencyblock = ''
  dependencyblock << default_gems.sort.map { |d| "  depends_on 'ruby_#{d.gsub('-', '_')}'" }.join("\n").to_s

  default_gems_pkg = <<~GEM_PKG_EOF
    # Generated by tools/create_default_gems_package.rb
    require 'package'

    class Default_gems < Package
      description 'Ruby Default Gem Packages.'
      homepage 'https://stdgems.org/'
      version "#{Time.now.utc.strftime('%Y%m%d%H')}-\#{CREW_RUBY_VER}"
      license 'GPL-3+'
      compatibility 'all'
      source_url 'SKIP'

      is_fake

    #{dependencyblock}
    end
  GEM_PKG_EOF
  puts 'Package for default_gems:'.orange
  puts default_gems_pkg.to_s.lightblue
  puts
  File.write(File.join('packages', 'default_gems.rb'), default_gems_pkg)
  system "rubocop -c .rubocop.yml -A  #{File.join('packages', 'default_gems.rb')}"
  FileUtils.chmod 0o644, File.join('packages', 'default_gems.rb')
end

def create_bundled_gems_package
  # https://stdgems.org/bundled_gems.json
  bundled_gem_json = JSON.parse(HTTParty.get('https://stdgems.org/bundled_gems.json').body)
  bundled_gems = bundled_gem_json['gems'].map { |i| i['gem'] }

  puts 'Bundled Gems are:'
  puts bundled_gems
  dependencyblock = ''
  dependencyblock << bundled_gems.sort.map { |d| "  depends_on 'ruby_#{d.gsub('-', '_')}'" }.join("\n").to_s

  bundled_gems_pkg = <<~GEM_PKG_EOF
    # Generated by tools/create_default_gems_package.rb
    require 'package'

    class Bundled_gems < Package
      description 'Ruby Bundled Gem Packages.'
      homepage 'https://stdgems.org/'
      version "#{Time.now.utc.strftime('%Y%m%d%H')}-\#{CREW_RUBY_VER}"
      license 'GPL-3+'
      compatibility 'all'
      source_url 'SKIP'

      is_fake

    #{dependencyblock}
    end
  GEM_PKG_EOF
  puts 'Package for bundled_gems:'.orange
  puts bundled_gems_pkg.to_s.lightblue
  puts
  File.write(File.join('packages', 'bundled_gems.rb'), bundled_gems_pkg)
  system "rubocop -c .rubocop.yml -A  #{File.join('packages', 'bundled_gems.rb')}"
  FileUtils.chmod 0o644, File.join('packages', 'bundled_gems.rb')
end

puts "Creating default_gems package from https://stdgems.org/default_gems.json'...".orange
create_default_gems_package

puts "Creating bundled_gems package from https://stdgems.org/bundled_gems.json'...".orange
create_bundled_gems_package
