#!/usr/bin/ruby.ruby3.3 
#
# encoding: UTF-8
#
# Copyright (c) 2016 SUSE LINUX Products GmbH
#
# Author: Klaus Kämpf <kkaempf@suse.de>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#

$:.push(File.join(__dir__, '..', 'lib'))

require 'kitbuilder'
require 'getoptlong'

def usage msg = nil
  STDERR.puts "*** #{msg}" if msg
  STDERR.puts "kitbuilder [-D <m2-dir>] [<pomfile>|<pomspec>]"
  STDERR.puts
  STDERR.puts "Options:"
  STDERR.puts " -D <m2-dir>     directory of maven cache"
  STDERR.puts " -s <script-name> strip maven cache from .jars, create <script-name> to re-create them"
  STDERR.puts " -g <gradle-dir> convert gradle cache to maven cache"
  STDERR.puts " -b <build.log>  parse build log for missing pom/jar"
  STDERR.puts " -s <script>  strip jars from <m2-dir>, write <script> to re-create them"
  STDERR.puts " -v           verbose"
end

buildlog = nil
m2dir = nil
gradledir = nil
pomdef = nil
strip_jars = nil
verbose = nil

GetoptLong.new(
  [ '-h', '--help', GetoptLong::NO_ARGUMENT ],
  [ '-g', '--gradle', GetoptLong::REQUIRED_ARGUMENT ],
  [ '-D', '--m2dir', GetoptLong::REQUIRED_ARGUMENT ],
  [ '-b', '--buildlog', GetoptLong::REQUIRED_ARGUMENT ],
  [ '-s', '--strip-jars', GetoptLong::REQUIRED_ARGUMENT ],
  [ '-v', '--verbose', GetoptLong::NO_ARGUMENT ]
).each do |opt, arg|
  case opt
  when '-D', '--m2dir'
    m2dir = File.expand_path(arg)
    puts "Dir #{m2dir}"
  when '-g', '--gradle'
    gradledir = File.expand_path(arg)
    puts "Gradle #{gradledir}"
  when '-b', '--buildlog'
    buildlog = File.expand_path(arg)
  when '-s', '--strip-jars'
    strip_jars = File.expand_path(arg)
    puts "Removing jars from #{m2dir}"
  when '-v', '--verbose'
    verbose = true
  when '-h', '--help'
    usage
    exit 0
  end
end

kitbuilder = Kitbuilder::Kitbuilder.new m2dir, verbose

if buildlog
  puts "Reading buildlog #{buildlog}"
  File.open(buildlog) do |f|
    f.each do |l|
      pom = jar = nil
      case l
      # > Could not resolve com.google.http-client:google-http-client-jackson2:1.21.0.
      when /\> Could not resolve ([^\s]+)\.$/
        pom = $1
      # > Could not find com.google.http-client:google-http-client-jackson2:1.21.0.
      when /\> Could not find ([^\s]+)\.$/
        pom = $1
      # > Could not download junit4-ant.jar (com.carrotsearch.randomizedtesting:junit4-ant:2.5.0): No cached version available for offline mode
      when /\> Could not download (([^\s]+)\.jar) \(([^\s]+)\)\: No cached version available for offline mode$/
        pom = $3
        kitbuilder.jar = $1
        puts ".build.log: Found jar #{jar}"      
      # > Could not find hadoop-common-tests.jar (org.apache.hadoop:hadoop-common:2.7.1).
      when /\> Could not find (([^\s]+)\.jar) \(([^\s]+)\)\.$/
        pom = $3
        kitbuilder.jar = $1
        puts ".build.log: Found jar #{jar}"
      else
        next
      end
      puts ".build.log: Found pom #{pom}"
      kitbuilder.handle pom
    end
  end
elsif gradledir
  puts "Converting gradle to maven"
  kitbuilder.gradle gradledir
elsif strip_jars
  puts "Writing #{strip_jars}" if verbose
  kitbuilder.strip strip_jars
else
  pom = ARGV.shift
  jar = ARGV.shift
  puts "Starting with #{pom}"
  kitbuilder.jar = jar
  kitbuilder.handle pom
end
