#!@PHP_BIN@
<?php

/*
 * Copyright 2005 - 2025 Centreon (https://www.centreon.com/)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * For more information : contact@centreon.com
 *
 */

// Error Level
error_reporting(E_ERROR | E_PARSE);

function usage()
{
    echo $argv[0] . " check|install [file]\n";
    echo "\tcheck\tcheck if the package list is installed\n";
    echo "\tupgrade\tupgrade the packages\n";
    echo "\tinstall\tinstall the packages\n";
}

function check_file($file)
{
    if (! file_exists($file)) {
        fwrite(STDERR, "The file with the list of packages does not exist\n");

        exit(2);
    }
    if (! is_readable($file)) {
        fwrite(STDERR, "The file with the list of packages cannot be read\n");

        exit(2);
    }
}

function get_list($file)
{
    $packages = [];
    $fd = fopen($file, 'r');
    while ($line = fgets($fd)) {
        [$name, $version, $status] = preg_split('/::/', trim($line));
        $package = ['name' => $name, 'version' => $version];
        if ($status) {
            $package['status'] = $status;
        }
        $packages[] = $package;
    }
    fclose($fd);

    return $packages;
}

function check($packages)
{
    $config = & PEAR_Config::singleton();
    $reg = & $config->getRegistry();
    $ret = 0;
    foreach ($packages as $package) {
        // echo "\033[s\033[1;37m" . $package['name'] . "\033[0m\033[33G\033[0;37m" . $package['version'] . "\033[0m\033[45G";
        echo "\033[s" . $package['name'] . "\033[0m\033[33G" . $package['version'] . "\033[0m\033[45G";
        $package_info = & $reg->getPackage($package['name']);
        if (is_null($package_info)) {
            $ret = 1;
            echo "\033[u\033[60G\033[1;31mNOK\033[0m\n";
        } else {
            $version = $package_info->getVersion();
            echo $version;
            if (version_compare($package['version'], $version, '<=')) {
                echo "\033[u\033[60G\033[1;32mOK\033[0m\n";
            } else {
                $ret = 1;
                echo "\033[u\033[60G\033[1;31mNOK\033[0m\n";
            }
        }
    }

    return $ret;
}

function install($packages)
{
    $config = & PEAR_Config::singleton();
    $reg = & $config->getRegistry();
    PEAR_Command::setFrontendType('CLI');
    $cmd = PEAR_Command::factory('install', $config);
    $ret = 0;
    foreach ($packages as $package) {
        if (! $reg->packageExists($package['name'])) {
            echo "\033[s" . $package['name'] . "\033[0m\033[33G" . $package['version'] . "\033[0m\033[45G";
            $name = $package['name'];
            if (isset($package['status'])) {
                $name .= '-' . $package['status'];
            }
            ob_start();
            $ok = $cmd->run('install', ['soft' => true, 'onlyreqdeps' => true], [$name]);
            ob_end_clean();
            $package_info = & $reg->getPackage($package['name']);
            if (! is_null($package_info)) {
                echo $package_info->getVersion();
                echo "\033[u\033[60G\033[1;32mOK\033[0m\n";
            } else {
                $ret = 1;
                echo "\033[u\033[60G\033[1;31mNOK\033[0m\n";
            }
        }
    }

    return $ret;
}

function upgrade($packages)
{
    $config = & PEAR_Config::singleton();
    $reg = & $config->getRegistry();
    PEAR_Command::setFrontendType('CLI');
    $cmd = PEAR_Command::factory('install', $config);
    $ret = 0;
    foreach ($packages as $package) {
        $package_info = & $reg->getPackage($package['name']);
        if (is_null($package_info)) {
            continue;
        }

        if ($package['name'] == 'PEAR') {
            ob_start();
            $ok = $cmd->run('install', ['soft' => true, 'nodeps' => true, 'force' => true], [$package['name']]);
            ob_end_clean();
        }

        $installed_version = $package_info->getVersion();
        if (version_compare($package['version'], $installed_version, '>')) {
            echo "\033[s" . $package['name'] . "\033[0m\033[33G" . $package['version'] . "\033[0m\033[45G" . $installed_version . "\t";
            $name = $package['name'];
            if (isset($package['status'])) {
                $name .= '-' . $package['status'];
            }
            ob_start();
            $ok = $cmd->run('install', ['soft' => true, 'onlyreqdeps' => true, 'upgrade' => true], [$name]);
            ob_end_clean();
            $package_info = & $reg->getPackage($package['name']);
            if (! is_null($package_info)) {
                echo $package_info->getVersion();
                echo "\033[u\033[60G\033[1;32mOK\033[0m\n";
            } else {
                $ret = 1;
                echo "\033[u\033[60G\033[1;31mNOK\033[0m\n";
            }
        }
    }
}

if (count($argv) < 2 || count($argv) > 3) {
    fwrite(STDERR, "Incorrect number of arguments\n");
    usage();

    exit(2);
}

$file = count($argv) == 3 ? $argv[2] : 'pear.lst';
check_file($file);

$packages = get_list($file);

require_once 'PEAR.php';
require_once 'PEAR/Config.php';
require_once 'PEAR/Command.php';

$ret = 0;

switch ($argv[1]) {
    case 'check':
        $ret = check($packages);
        break;
    case 'install':
        $ret = install($packages);
        break;
    case 'upgrade':
        $ret = upgrade($packages);
        break;
    default:
        fwrite(STDERR, "Incorrect argument\n");
        usage();

        exit(2);
}

exit($ret);
