#!/usr/bin/ruby.ruby3.4 
# frozen_string_literal: true

#
# Copyright (c) [2024] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.

# TEMPORARY overwrite of Y2DIR to use DBus for communication with dependent yast modules
require "yast"
$LOAD_PATH.unshift File.expand_path("../lib", __dir__)

# Set the PATH to a known value
ENV["PATH"] = "/sbin:/usr/sbin:/usr/bin:/bin"

# Disable AutoYaST XML validation. It will be enabled in the future.
ENV["YAST_SKIP_XML_VALIDATION"] = "1"

require "rubygems"
# find Gemfile when D-Bus activates a git checkout
Dir.chdir(__dir__) do
  require "bundler/setup"
end

require "agama/commands/agama_autoyast"

if ARGV.length != 2
  warn "Usage: #{$PROGRAM_NAME} URL DIRECTORY"
  exit 1
end

begin
  url, directory = ARGV
  result = Agama::Commands::AgamaAutoYaST.new(url, directory).run
  if !result
    warn "Did not convert the profile (canceled by the user)."
    exit 2
  end
rescue Agama::Commands::CouldNotFetchProfile
  warn "Could not fetch the AutoYaST profile."
  exit 3
rescue Agama::Commands::CouldNotWriteAgamaConfig
  warn "Could not write the Agama configuration."
  exit 4
end
