#!/usr/local/bin/php
<?php

/*
 * Copyright (C) 2019 Deciso B.V.
 * Copyright (C) 2019-2020 Franco Fichtner <franco@opnsense.org>
 * Copyright (C) 2017 Alexander Shursha <kekek2@ya.ru>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

require_once 'config.inc';
require_once 'util.inc';
require_once 'system.inc';
require_once 'interfaces.inc';
require_once 'filter.inc';
require_once 'auth.inc';

/**
 * retrieve config property by path (e.g. system.firmware), return simple type or json
 */
function get_config_prop($path, $cnf = null)
{
    global $config;
    $cnf = $cnf === null ? $config : $cnf;
    $path = !is_array($path) ? explode(".", $path) : $path;
    $node_name = array_shift($path);

    if (isset($cnf[$node_name])) {
        if (!empty($path)) {
            return get_config_prop($path, $cnf[$node_name]);
        } else {
            $content = $cnf[$node_name];
            if (is_array($content)) {
                return json_encode($content, true);
            } else {
                return $content;
            }
        }
    } else {
        return null;
    }
}

$opts = getopt('cshrg', array(), $optind);
$args = array_slice($argv, $optind);

if (isset($opts['h'])) {
    echo "Usage: pluginctl [-h] [-c] [-s] [-r] [-g] [...]\n\n";
    echo "\t-h show this help text and exit\n";
    echo "\t-c configure mode (default), executes plugin [_configure] hook\n";
    echo "\t-s service mode (e.g. myservice restart)\n";
    echo "\t-r run mode (e.g. command)\n";
    echo "\t-g get config property (raw, e.g. system.firmware.plugins)\n\n";
    echo "Without arguments, a list of plugins of the requested type is shown.\n";
} elseif (empty($args[0])) {
    // no arguments, list plugins of selected type
    $results = [];
    if (isset($opts['s'])) {
        foreach (plugins_services() as $service) {
            $results[$service['name']] = 1;
        }
    } else {
        foreach (plugins_scan() as $name => $path) {
            try {
                include_once $path;
            } catch (\Error $e) {
                error_log($e);
            }
            $func = sprintf('%s_configure', $name);
            if (function_exists($func)) {
                foreach ($func() as $when => $worker) {
                    $results[$when] = 1;
                }
            }
        }
    }

    $results = array_keys($results);
    sort($results);

    foreach ($results as $result) {
        echo "$result\n";
    }

    exit(0);
} elseif (isset($opts['s'])) {
    $name = !empty($args[0]) ? $args[0] : '';
    $act = !empty($args[1]) ? $args[1] : '';
    $filter = [];
    if (!empty($args[2])) {
        $filter['id'] = $args[2];
    }
    switch ($act) {
        case 'start':
            echo service_control_start($name, $filter) . PHP_EOL;
            break;
        case 'stop':
            echo service_control_stop($name, $filter) . PHP_EOL;
            break;
        case 'restart':
            echo service_control_restart($name, $filter) . PHP_EOL;
            break;
        default:
            echo "Unknown command `$act'\n";
            break;
    }
    exit (0);
} elseif (isset($opts['r'])) {
   $cmd = array_shift($args);
   $result = plugins_run($cmd, false, $args);
   if (!empty($result)) {
      $response = json_encode($result, JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT);
      echo $response;
   }
   exit (0);
} elseif (isset($opts['g'])) {
   echo get_config_prop($args[0]) . PHP_EOL;
   exit (0);
} else {
    /* second argument is hook */
    $hook = array_shift($args);
    /* other arguments are passed as is */
    plugins_configure($hook, true, $args);
}
