#!/usr/bin/ruby.ruby3.4 
# Encoding: utf-8

$LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')))

require 'optparse'
require 'meez/meez'

options = {
  path: './'
}

opt_parser = OptionParser.new do |opt|
  opt.banner = 'Usage: chef exec meez [options] <cookbook name>'
  opt.separator ''
  opt.separator 'Options'
  opt.on('-o', '--cookbook-path PATH', 'The directory where the cookbook will be created') do |path|
    options[:path] = path
  end
  opt.on('-C', '--copyright COPYRIGHT_HOLDER', 'The  name  of  the  copyright holder.') do |copyright|
    options[:copyright] = copyright
  end
  opt.on('-I', '--license LICENSE', 'The type of license under which a cookbook is distributed: apachev2, gplv2, gplv3, mit, or none (default).') do |license|
    options[:license] = license
  end
  opt.on('-m', '--email EMAIL', 'The  email  address  for the individual who maintains the cookbook.') do |email|
    options[:email] = email
  end
  opt.on('-d', '--kitchen-driver DRIVER', 'The driver which use test-kitchen for creating platform instances: vagrant (default), docker') do |driver|
    options[:driver] = driver
  end
  opt.on('-h', '--help', 'help') do
    options[:help] = true
    puts opt_parser
  end
  opt.on('-v', '--version', 'version') do
    options[:version] = true
    puts 'meez version 0.2.7'
  end
end

opt_parser.parse!

cookbook_name = ARGV.pop

unless options[:help] || options[:version]
  if cookbook_name
    Meez.init(cookbook_name, options)
    puts "Cookbook #{cookbook_name} created successfully"
    puts 'Next steps...'
    puts "  $ cd #{File.join(options[:path], cookbook_name)}"
    puts '  $ export USE_SYSTEM_GECODE=1'
    puts '  $ chef exec rake prepare'
    puts '  $ chef exec rake test'
  else
    puts 'Need to specify a cookbook'
    puts opt_parser
  end
end
