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

# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

$stderr.sync = true
require "optparse"
require "fileutils"

def export_templates_to export_path
  if File.exist? export_path
    puts "Cannot export to an existing file path"
    exit 1
  end
  FileUtils.mkdir_p export_path
  source_dir = File.join File.dirname(__dir__), "templates", "default", "."
  FileUtils.cp_r source_dir, export_path
end

def build_gem_to generator_name, output_path
  if File.exist? output_path
    puts "Cannot generate gem to an existing file path"
    exit 1
  end
  FileUtils.mkdir_p output_path

  require "gapic/gem_builder"

  builder = Gapic::GemBuilder.new generator_name, output_path
  builder.bootstrap
  examples_path = File.join output_path, "example_templates", generator_name
  export_templates_to examples_path
  templates_path = File.join output_path, "templates", generator_name
  FileUtils.mkdir_p templates_path
  FileUtils.touch File.join(templates_path, ".gitkeep")

  Dir.chdir output_path do
    system "git init && git add . && git commit -m " \
           "'Created new client generator gem using gapic-generator'"
  end
end

ARGV << "-h" if ARGV.empty?

options = {}
OptionParser.new do |opts|
  opts.banner = "Usage: gapic-generator [options]"

  opts.on "-e", "--export export_path", String,
          "Export templates to the given path" do |export_path|
    options[:export] = export_path
  end

  opts.on "-g", "--gem name", String, "Generate a new rubygem" do |name|
    options[:generator_name] = name
  end

  opts.on "--output path", String, "Set the gem output directory" do |path|
    options[:output_path] = path
  end

  opts.on_tail "-h", "--help", "Show this message" do
    puts opts
    exit
  end
end.parse!

if options[:export]
  export_path = File.expand_path options[:export]

  export_templates_to export_path

  puts "Template files exported to #{export_path}"
  puts "You can use these files when calling protoc with:"
  puts ""
  puts "$ GOOGLE_GAPIC_GENERATOR_RUBY_TEMPLATES=#{export_path} protoc ..."
end

if options[:generator_name]
  generator_name = options[:generator_name]
  unless generator_name =~ /^[a-z]([a-z_]*[a-z])?$/
    puts "Generator name must be lower_snake_case"
    exit 1
  end
  output_path = options[:output_path] || "gapic-generator-#{generator_name}"
  output_path = File.expand_path output_path

  build_gem_to generator_name, output_path

  puts "Generator for #{generator_name} created at #{output_path}"
end