#!/usr/bin/ruby.ruby3.2 
# 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"

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

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

begin
  url, directory = ARGV
  converter = Agama::AutoYaST::Converter.new(url)
  converter.to_agama(directory)
rescue RuntimeError => e
  warn "Could not load the profile from #{url}: #{e}"
  exit 2
end
