#!/usr/bin/php8.4 -c/etc/openmediavault
<?php

/**
 * This file is part of OpenMediaVault.
 *
 * @license   https://www.gnu.org/licenses/gpl.html GPL Version 3
 * @author    Volker Theile <volker.theile@openmediavault.org>
 * @copyright Copyright (c) 2009-2026 Volker Theile
 *
 * OpenMediaVault is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * OpenMediaVault 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 OpenMediaVault. If not, see <https://www.gnu.org/licenses/>.
 */
require_once("openmediavault/autoloader.inc");
require_once("openmediavault/globals.inc");
require_once("openmediavault/functions.inc");

///////////////////////////////////////////////////////////////////////////////
// Helper functions.
///////////////////////////////////////////////////////////////////////////////

/**
 * Display command usage.
 */
function usage()
{
    global $argv;
    $text = <<<EOF
Usage:
  %s [options] <service> <method> [params]

OPTIONS:
  -u --user  The name of the user
  -h --help  Print a help text

EOF;
    printf($text, basename($argv[0]));
}

///////////////////////////////////////////////////////////////////////////////
// Global variables.
///////////////////////////////////////////////////////////////////////////////

$cmdargs = array(
      "u:" => "user:",
      "h::" => "help::"
  );
$username = \OMV\Environment::get("OMV_WEBGUI_ADMINUSER_NAME", "admin");
$rc = 0;

// Check the command line arguments. Exit and display usage if
// necessary.
$options = getopt(implode("", array_keys($cmdargs)), $cmdargs);
foreach ($options as $optionk => $optionv) {
    switch ($optionk) {
        case "h":
        case "help":
            usage();
            exit(0);
            break;
        case "u":
        case "user":
            $username = $options[$optionk];
            $argc -= 2;
            array_splice($argv, 1, 2);
            break;
    }
}
if (($argc < 2) || ($argc > 4)) {
    print gettext("ERROR: Invalid number of arguments\n");
    usage();
    exit(1);
}

// Create the RPC caller context.
$context = array(
    "username" => $username,
    "role" => ($username === \OMV\Environment::get(
        "OMV_WEBGUI_ADMINUSER_NAME",
        "admin"
    )) ?
        OMV_ROLE_ADMINISTRATOR : OMV_ROLE_USER
);

// Process command arguments.
$service = $argv[1];
$method = $argv[2];
$params = null;

// Decode RPC method parameters from JSON to a PHP object.
if ($argc > 3) {
    if (!is_json($argv[3])) {
        print gettext("ERROR: The params argument is no valid JSON\n");
        exit(1);
    }
    $params = json_decode_safe($argv[3]);
}

// Execute the RPC and print the response.
$fh = STDOUT;
try {
    $response = \OMV\Rpc\Rpc::call(
        $service,
        $method,
        $params,
        $context,
        \OMV\Rpc\Rpc::MODE_REMOTE
    );
} catch (\Exception $e) {
    $response = [
        "response" => null,
        "error" => [
            "code" => $e->getCode(),
            "message" => $e->getMessage(),
            "trace" => $e->__toString()
        ]
    ];
    $fh = STDERR;
    $rc = 1;
}
fwrite($fh, sprintf("%s\n", json_encode_safe($response)));

exit($rc);
