#!/usr/bin/php
<?php

/******************************************************************************
*
* repovers
* part of lfs-ryco
*
* Copyright (c) 2020-2025 Ryan Coe <bluemrp9@gmail.com>
*
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
******************************************************************************/

if (@file_exists('./vendor/_autoload.php')) {
    require_once('./vendor/_autoload.php');
} else {
    require_once((isset($_SERVER['LFS_ROOTFS']) ? $_SERVER['LFS_ROOTFS'] : '').'/usr/share/php/vendor/_autoload.php');
}

use LFS\LFS;
use LFS\Repo;
use Ryco\Log;

LFS::_init();

// Program name and version
Log::info(LFS::DISTRO.' repovers version 2025.06.05');

$short = ($argc >= 2 && $argv[1] === "--long" ? false : true);
$repo_file = LFS::get_prefix().'/rpmbuild/REPO/lfsrepo.json.gz';
$local_repo = Repo::createFromRepoFile($repo_file);
$ver_arr = $local_repo->buildVersionTable()->getVersionTable();

// parse packages
$arches = [];
$versions = [];
foreach ($ver_arr as $arch => $packages) {
	if ($arch === 'noarch' || $arch === 'source') {
		continue;
	}
	if (!in_array($arch, $arches)) {
		$arches[] = $arch;
	}
	foreach ($packages as $name => $ver_arr) {
		$key = REPO::makeKey($name, $ver_arr[0], $arch);
		$pkg = $local_repo->getPackage($key);
		$name = $pkg->source_name;
		if (!array_key_exists($name, $versions)) {
			$versions[$name] = [];
		}
		if (!array_key_exists($arch, $versions[$name])) {
			$versions[$name][$arch] = $ver_arr[0];
		}
	}
}
unset($db);
unset($arch);
unset($packages);
unset($name);
unset($package);
sort($arches);
ksort($versions);

// build columns
$cols = [];
$cols[] = "package";
foreach ($arches as $v) {
	$cols[] = $v;
}
unset($v);

// build rows
$rows = [];
foreach ($versions as $k=>$v) {
	$row = [];
	$row[] = $k;
	foreach ($arches as $arch) {
		if (array_key_exists($arch, $v)) {
			$row[] = $v[$arch];
		} else {
			$row[] = "";
		}
	}
	$rows[] = $row;
}
unset($k);
unset($v);
unset($row);
unset($arch);
unset($versions);

// calculate widths
$total_width = 0;
$widths = [];
foreach ($cols as $v) {
	$widths[] = strlen($v);
}
foreach ($rows as $row) {
	foreach ($row as $k=>$v) {
		$l = strlen($v);
		if ($l > $widths[$k]) {
			$widths[$k] = $l;
		}
	}
}
foreach ($widths as $k=>&$v) {
	$v += 2;
	$total_width += $v;
}
unset($row);
unset($k);
unset($v);
unset($l);

// display table
$c = 0;
echo "\n";
foreach ($cols as $k=>$v) {
	echo str_pad($v, $widths[$k]);
}
echo "\n".str_repeat("-", $total_width)."\n";
$t = "";
foreach ($rows as $row) {
	$l = "";
	$x = array();
	foreach ($row as $k=>$v) {
		$l .= str_pad($v, $widths[$k]);
		if ($k > 0 && strlen($v) > 0) {
			$x[] = $v;
		}
	}
	$m = 0;
	if (count($x) > 1) {
		for ($i = 1; $i < count($x); $i++) {
			if ($x[0] !== $x[$i]) {
				$m++;
			}
		}
	}
	if (!$short || $m > 0) {
		if (!$short) {
			if ($m > 0) {
				$t .= "\033[01;31m".$l."\033[00m\n";  // red
			} else {
				$t .= $l."\n";
			}
		} else {
			$t .= $l."\n";
		}
		$c++;
	}
}
if ($c > 0) {
	echo $t;
}
echo "\n";
