#! /usr/bin/ruby.ruby2.5
# Note: The ruby interpreter path above is replaced at the installation
# to use a hardcoded versioned path.

# set env as first part, otherwise dummy UI will be created.
# for insts-sys it will be overwritten later
ENV["YAST_IS_RUNNING"] = "yes"

require "socket"

require "yast"
require "yast/y2start_helpers"

Yast::Y2StartHelpers.setup_signals

File.umask(0o022)

Yast.y2milestone("y2base called with #{ARGV.inspect}")

begin
  args = Yast::Y2StartHelpers.parse_arguments(ARGV.dup)
rescue RuntimeError => e
  $stderr.puts e
  $stderr.puts Yast::Y2StartHelpers.help
  exit 1
end

if args[:generic_options][:help]
  $stdout.puts Yast::Y2StartHelpers.help
  exit 0
end

if args[:client_name] == "installation" &&
    args[:client_options][:params].include?("initial")
  ENV["YAST_IS_RUNNING"] = "instsys"
end

if !Yast::WFM.ClientExists(args[:client_name])
  $stderr.puts "No such client module #{args[:client_name]}"
  exit 1
end

NO_CLI_CLIENTS = ["installation", "view_anymsg", "firstboot", "scc"].freeze
Yast.ui_create(args[:server_name], args[:server_options])
# set application title bsc#1033161
Yast.import "UI"
# do not fail if gethostname failed
hostname = Socket.gethostname rescue ""
hostname = "" if hostname == "(none)"
hostname = " @ #{hostname}" unless hostname.empty?

title = "YaST2 - #{args[:client_name]}#{hostname}"
# set title only if it is not CLI (bsc#1033993)
# modules do not have CLI arguments, except installation (bsc#1037891)
set_title = args[:client_options][:params].empty? || NO_CLI_CLIENTS.include?(args[:client_name])
Yast::UI.SetApplicationTitle(title) if set_title

Yast::WFM.CallFunction(args[:client_name], args[:client_options][:params])

0
