#!/usr/bin/php
<?php

/******************************************************************************
*
* pushsources
* part of lfs-ryco
*
* The MIT License (MIT)
*
* Copyright 2020-2023 by Ryan Coe <bluemrp9@gmail.com>
*
* 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.
*
******************************************************************************/

declare(strict_types=1);

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\Utils\Process;
use Ryco\Assert;
use Ryco\Log;

LFS::_init();
LFS::timer_start();

$prefix = LFS::get_prefix();
$rpms_dir = $prefix.'/rpmbuild/RPMS';
$srpms_dir = $prefix.'/rpmbuild/SRPMS';
$tools_dir = $prefix.'/rpmbuild/TOOLS';
$repo_dir = $prefix.'/rpmbuild/REPO';
$repo_rpm_dir = $repo_dir.'/rpm';
$repo_key_dir = $repo_dir.'/keys';
$repo_ini_file = $prefix.'/rpmbuild/TOOLS/repo.ini';

// Print name and version
Log::info(LFS::DISTRO.' pushsources 2025.06.01');

// Read repo settings into config
$repo = Repo::newRepo('temp');
$config = $repo->getConfig($repo_ini_file);

$source_name = $config->getString('repo.source');
Assert::stringNotEmpty($source_name);
$remote_user = $config->getString('repo.remote_user');
Assert::stringNotEmpty($remote_user);
$remote_host = $config->getString('repo.remote_host');
Assert::stringNotEmpty($remote_host);
$source_dir = $config->getString('repo.source_dir').'/'.$source_name;

$basedir = LFS::get_prefix().'/rpmbuild/SOURCES/';
$destdir = sprintf(
    '%s@%s:%s',
    $remote_user,
    $remote_host,
    $source_dir,
);
$exec = [
    '/usr/bin/rsync',
    '--recursive',
    '--times',
    '--links',
    '--hard-links',
    '--stats',
    '--verbose',
    '--delete',
    '--delete-after',
    '--exclude .empty',
    '--exclude .gitempty',
    $basedir,
    $destdir,
];
$proc = Process::fromArray(['exec'=>$exec])->start()->wait();
$retval = $proc->getExitCode();
if ($retval !== 0) {
    Log::error($proc->getOutputArray('STDERR'));
    Log::error('Failed to rsync source files to remote directory');
    exit(1);
}
Log::info($proc->getOutputArray('STDOUT'));
exit(0);
