-- Copyright (c) 2026 Huawei Technologies Co., Ltd.
-- openUBMC is licensed under Mulan PSL v2.
-- You can use this software according to the terms and conditions of the Mulan PSL v2.
-- You may obtain a copy of Mulan PSL v2 at:
-- http://license.coscl.org.cn/MulanPSL2
-- THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
-- EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
-- MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
-- See the Mulan PSL v2 for more details.

local config_file = os.getenv('CONFIG_FILE')

if config_file ~= nil then
    loadfile(os.getenv('CONFIG_FILE'), 't', { package = package, os = os })()
else
    package.path = '/opt/bmc/apps/?.lua'
        .. ';/opt/bmc/apps/cli/?.lua;/opt/bmc/apps/cli/?/init.lua'
        .. ';/opt/bmc/apps/cli/lualib/?.lua;/opt/bmc/apps/cli/lualib/?/init.lua'
        .. ';/opt/bmc/apps/mdb_interface/?.lua;/opt/bmc/apps/mdb_interface/?/init.lua'
        .. ';/opt/bmc/libmc/lualib/?.lua;/opt/bmc/libmc/lualib/?/init.lua'
        .. ';/opt/bmc/lualib/?.lua;/opt/bmc/lualib/?/init.lua'
        .. ';/opt/bmc/lualib/lmc/?.lua'
    package.cpath = '/opt/bmc/libmc/luaclib/?.so;/opt/bmc/luaclib/?.so'
        .. ';/opt/bmc/apps/cli/luaclib/?.so;/opt/bmc/skynet/luaclib/?.so;/usr/lib64/?.so;'
end

local log = require('mc.logging')
local sd_bus = require('sd_bus')
local cli = require('ipmc')
local common = require('common')
local config = require('config')

log:set_log_module_name('cli')

local params = { ... }
params = common.parse_verbose_parameter(params)

local bus = sd_bus.open_user(true, true)
local cli_obj = cli.new(config.IPMCSET_PATH, bus)
local ok, err_msg = pcall(cli_obj.command_request, cli_obj, 'Patch', params)
bus:close()
if not ok then
    if not common.is_interrupt_signal(err_msg) then
        common.print_internal_err()
    end
    log:error('execute ipmcset failed, %s', err_msg)
end