#!/usr/bin/env php
<?php
/**
 * ======================================================================
 * 
 *     _____ _____ __                   _____         _ _           
 *    |     |     |  |      ___ ___ ___|     |___ ___|_| |_ ___ ___ 
 *    |-   -| | | |  |__   | .'| . | . | | | | . |   | |  _| . |  _|
 *    |_____|_|_|_|_____|  |__,|  _|  _|_|_|_|___|_|_|_|_| |___|_|  
 *                             |_| |_|                              
 *                                                                                                                             
 *    CLIENT AS CLI APP
 * 
 * ---------------------------------------------------------------------
 * 2025-03-04  v0.1    Initial version
 * 2025-03-05  v0.2    slack notification as hash from ini; check params can be an hash value or JSON
 * 2025-03-07  v0.3    set argc and argv from $_SERVER; update help output
 * 2025-03-09  v0.4    more colors in output of debug and help
 * 2025-03-14  v0.5    add build infos
 * 2025-03-14  v0.6    add meta tags, check count+visual; add _json2array()
 * 2025-03-17  v0.7    update help
 * 2025-03-17  v0.8    check missing 'function' in check. get php client version
 * ======================================================================
 */

// needed when starting compiled binary built from a PHAR file
$argc = $_SERVER['argc'];
$argv = $_SERVER['argv'];

$FLAG_DEBUG = 0;
$VERSION = "0.8";
$PHPCLIENTVERSION = "not-detected";

$AMCLI_BUILD_DATE = "never";

// ---MARK---INCLUDE-CHECKS---START---

@include "amcli__build.php";

if (!file_exists(__DIR__ . "/include_checks.php")) {
    echo "❌ ERROR: File 'include_checks.php' does not exist yet..\n";
    echo "Run the ../installer.php first!\n";
    exit(1);
}

if (!include __DIR__ . "/include_checks.php") {
    echo "❌ ERROR: Include of generated 'include_checks.php' failed.\n";
    echo "Check its generation by installer or run the installer again.\n";
    exit(2);
}
// ---MARK---INCLUDE-CHECKS---END


// --------------------------------------------------------------------
//
// FUNCTIONS
//
// --------------------------------------------------------------------


/**
 * Call $oMonitor-><METHODNAME> with 1 or 2 params.
 * This function was introduced to shorten the code.
 * 
 * @param string $sMethod  method name of appmonitor class
 * @param mixed  $value
 * @param mixed  $value2
 * @return void
 */
function _set(string $sMethod, mixed $value = null, mixed $value2 = null)
{
    global $oMonitor;
    if (!isset($value)) {
        _wd("SKIP \$oMonitor->$sMethod(<no_value>)");

    } else {
        if (!isset($value2)) {
            _wd("calling \$oMonitor->$sMethod('$value')");
            $oMonitor->$sMethod($value);
        } else {
            _wd("calling \$oMonitor->$sMethod('$value', '$value2')");
            $oMonitor->$sMethod($value, $value2);
        }
    }
}

/**
 * Write debug output if FLAG_DEBUG is true (use -v or --verbose on command line)
 * @param string $s  message to show; a prefix "DEBUG:" will be added in front
 * @return void
 */
function _wd($s): void
{
    global $FLAG_DEBUG;
    if ($FLAG_DEBUG) {
        fwrite(STDERR, "\e[90mDEBUG: $s\e[0m\n");
    }
}

/**
 * Show help text
 * @return void
 */
function _showHelp(): void
{
    global $VERSION, $PHPCLIENTVERSION;
    $_self = str_replace('.php', '', basename(__FILE__));
    echo "
   _____ _____ __                   _____         _ _           
  |     |     |  |      ___ ___ ___|     |___ ___|_| |_ ___ ___ 
  |-   -| | | |  |__   | .'| . | . | | | | . |   | |  _| . |  _|
  |_____|_|_|_|_____|  |__,|  _|  _|_|_|_|___|_|_|_|_| |___|_|  
                           |_| |_|             \e[1mCLI client v$VERSION\e[0m


This client performs appmonitor checks and puts the results as JSON to stdout.
It contains all checks that are available in the PHP appmonitor client v$PHPCLIENTVERSION.
You can use the compiled binary to monitor any non PHP webapp without 
implementing the checks for your programming language.

You need to reference an INI file that contains the metadata and all checks.
Have a look to the online documentation for details.
You find example snippets in the source code of this project in tests/config/.

  👤 Author: Axel Hahn
  📄 Source: https://git-repo.iml.unibe.ch/iml-open-source/appmonitor-cli-client
  📜 License: GNU GPL 3.0
  📗 Docs: https://os-docs.iml.unibe.ch/appmonitor-cli-client/

(c) 2025 Institute for Medical Education * University of Bern

...............................................................................


 ✨ \e[1mSYNTAX:\e[0m

    $_self [OPTIONS] --ini=<INI-FILE>


 🔷 \e[1mOPTIONS:\e[0m

    -h, --help        Print this help and exit

    -i, --ini         Set an INI File to parse
    -v, --verbose     Enable verbose output (written to STDERR)

    -b, --buildinfos  show build information and exit
    -l, --list        list available checks and exit
    -m, --modules     list available Php modules in this binary and exit
    -V, --version     Show version and exit


 👉 \e[1mEXAMPLES:\e[0m

    $_self -i=my.ini
    $_self --ini=my.ini
        Execute checks from INI file 'my.ini'.

    $_self --list
        List available checks.
    
";
}

/**
 * JSON helper:
 * If a given value is a JSON string then return an array, otherwise return the value
 * 
 * @param mixed $value
 * @return mixed
 */
function _json2array(mixed $value=null): mixed {
    if (!is_string($value)) {
        return $value;
    }
    $aArray = json_decode($value, 1);
    if (is_array($aArray)){
        _wd("JSON found in\n$value\n... and was converted to an array: " . print_r($aArray, 1));
    }
    return is_array($aArray) ? $aArray : $value;
}


// --------------------------------------------------------------------
//
// MAIN
//
// --------------------------------------------------------------------

// put params to $ARGS
if ($argc > 1) {
    parse_str(implode('&', array_slice($argv, 1)), $ARGS);
}

// check params

if (isset($ARGS['-v']) || isset($ARGS['--verbose'])) {
    $FLAG_DEBUG = 1;
    _wd("Verbose mode enabled. Showing debug infos on STDERR.");
}
_wd("CLI ARGS: " . print_r($ARGS ?? [], 1));


_wd("Initializing appmonitor class");

$oMonitor = new appmonitor();
$PHPCLIENTVERSION = $oMonitor->getVersion();
$sPreSpace = " - ";

// show version
if (isset($ARGS['-V']) || isset($ARGS['--version'])) {
    _wd("Showing version");
    echo "amcli $VERSION\n";
    exit(0);
}

// show build infos
if (isset($ARGS['-b']) || isset($ARGS['--buildinfos'])) {
    $aMods = get_loaded_extensions();
    sort($aMods);

    _wd("Showing build infos");
    echo "amcli v$VERSION (".PHP_OS.")\n\n";
    echo "Build date: $AMCLI_BUILD_DATE\n";
    echo "\n";
    echo "Compiled PHP client v$PHPCLIENTVERSION with PHP ".PHP_VERSION."\n";
    echo "Including these modules:\n";
    $sModules="    ";
    $i=0;
    foreach($aMods as $sModulename){
        $i++;
        $sModules.="$sModulename ";
        if($i % 10 == 0){
            $sModules.="\n    ";
        }
    }
    echo "$sModules\n\n";

    exit(0);
}

// show help
if (isset($ARGS['-h']) || isset($ARGS['--help'])) {
    _wd("Showing help");
    _showHelp();
    exit(0);
}

// ----------------------------------------------------------------------


// show builtin checks
if (isset($ARGS['-l']) || isset($ARGS['--list'])) {
    _wd("Showing checks");
    echo $sPreSpace . implode("\n$sPreSpace", $oMonitor->listChecks())."\n";
    exit(0);
}

// show builtin modules
if (isset($ARGS['-m']) || isset($ARGS['--modules'])) {
    _wd("Showing php modules");
    $aMods = get_loaded_extensions();
    sort($aMods);
    echo $sPreSpace . implode("\n$sPreSpace", $aMods)."\n";
    exit(0);
}

$inifile = $ARGS["--ini"] ?? ($ARGS["-i"] ?? "");
if (!$inifile) {
    echo "❌ ERROR: Missing INI File. Use -h (or --help) for more infos.\n";
    exit(3);
}

_wd("Using ini file '$inifile'.");

if (!file_exists($inifile)) {
    echo "❌ ERROR: INI File '$inifile' does not exist.\n";
    exit(4);
}

try {
    $aIni = parse_ini_file($inifile, true);
} catch (Exception $e) {
    echo "❌ ERROR: INI File '$inifile' could not be parsed.\n";
    exit(5);
}
if (!is_array($aIni)) {
    echo "❌ ERROR: INI File '$inifile' could not be parsed as array.\n";
    exit(5);
}

_wd("Parsed INI data: " . print_r($aIni, 1));

// loop over all values and detect JSON strings to convert
$aIni['meta']['tags']=_json2array($aIni['meta']['tags'] ?? null);
$aIni['notifications']['email']=_json2array($aIni['notifications']['email'] ?? null);
$aIni['notifications']['slack']=_json2array($aIni['notifications']['slack'] ?? null);

// ----------------------------------------------------------------------

// set metadata
_set("setHost", $aIni['meta']['host'] ?? null);
_set("setWebsite", $aIni['meta']['website'] ?? null);
_set("setTtl", $aIni['meta']['ttl'] ?? null);
_set("setVersion", "amcli-$VERSION-using");

foreach ($aIni['meta']['tags'] ?? [] as $sValue) {
    _set("addTag", $sValue);
}

foreach ($aIni['notifications']['email'] ?? [] as $sValue) {
    _set("addEmail", $sValue);
}
foreach ($aIni['notifications']['slack'] ?? [] as $sChannel => $sWebhook) {
    _set("addSlackWebhook", $sChannel, $sWebhook);
}

// ----------------------------------------------------------------------

// loop over checks
$aChecks = $aIni;
unset($aChecks["meta"]);
unset($aChecks["notifications"]);

foreach ($aChecks as $sKey => $aCheck) {
    $aChecks[$sKey]['name'] = $aCheck['name'] ?? $sKey;
    $aParams = _json2array($aCheck['params'] ?? []);
    if (!is_array($aParams)) {
        echo "❌ ERROR: key 'params' for check [$sKey] must be \n- a hash or\n- valid JSON\n- not set\n";
        echo "Value in $inifile: $aCheck[params]??[]\n";
        echo "Try to use multiple lines 'params[<KEY>]=<VALUE>'.\n";
        exit(6);
    }

    if(!isset($aCheck['function'])){
        echo "❌ ERROR: Missing key 'function' in check [$sKey].\n";
        exit(7);
    }
    $aAddCheck = [
        "name" => $aCheck['name'] ?? $sKey,
        "description" => $aCheck['description'] ?? "",
        "check" => [
            "function" => $aCheck['function'] ?? "not set",
            "params" => $aParams,
        ],
    ];
    foreach (["group", "parent", "worstresult", "count", "visual"] as $sCustomKey) {
        if (isset($aCheck[$sCustomKey])) {
            $aAddCheck[$sCustomKey] = $aCheck[$sCustomKey];
        }
    }
    _wd("Execute Check '$sKey': " . print_r($aAddCheck, 1));
    $oMonitor->addCheck($aAddCheck);
}


// ----------------------------------------------------------------------
// send the response

_wd("Setting result");
$oMonitor->setResult();

_wd("Send response");
$oMonitor->render();

// ----------------------------------------------------------------------