#!/usr/bin/php
<?php
chdir(__DIR__);

$template_php = <<< EOT
---
document: modulemd
version: 2
data:
  name: remi-php
  stream: @VERSION@
  version: @DATE@
  context: 00000000
  arch: @ARCH@
  summary: PHP scripting language
  description: >-
    php @VERSION@ module
  license:
    module:
    - GPLv2+
    content:
    - BSD
    - BSD and LGPLv3+
    - PHP
    - PHP and BSD
    - PHP and LGPLv2 and OpenLDAP
    - PHP and LGPLv2+
    - PHP and Zend and BSD and MIT and ASL 1.0
  dependencies:
  - buildrequires:
      platform: [@DIST@]
    requires:
      platform: [@DIST@]
  references:
    documentation: http://php.net/
    tracker: https://bugs.php.net/
  profiles:
    default:
      rpms:
      - php-cli
      - php-common
      - php-fpm
      - php-json
      - php-mbstring
      - php-xml
    devel:
      rpms:
      - libzip
      - php-cli
      - php-common
      - php-devel
      - php-fpm
      - php-json
      - php-mbstring
      - php-pear
      - php-pecl-zip
      - php-process
      - php-xml
    minimal:
      rpms:
      - php-cli
      - php-common
  api:
    rpms:
    - php-fpm
  components:
    rpms:
      php:
        rationale: Module API.
      php-pecl-zip:
        rationale: ZIP extension
  artifacts:
    rpms:

EOT;

$template_glpi = <<< EOT
---
document: modulemd
version: 2
data:
  name: remi-glpi
  stream: @VERSION@
  version: @DATE@
  context: 00000000
  arch: @ARCH@
  summary: Free IT asset management software
  description: >-
    glpi @VERSION@ module
  license:
    module:
    - GPLv2+
    content:
    - GPLv2+ and GPLv3+ and MIT
  dependencies:
  - buildrequires:
      platform: [@DIST@]
    requires:
      platform: [@DIST@]
  references:
    documentation: https://glpi-project.org/
    tracker: https://github.com/glpi-project/glpi/issues
  profiles:
    default:
      rpms:
      - glpi
  api:
    rpms:
    - glpi
  components:
    rpms:
      glpi:
        rationale: Module API.
  artifacts:
    rpms:

EOT;

$defaults = <<< EOT
---
document: modulemd-defaults
version: 1
data:
  module: remi-php
  profiles:
    7.2: [default]
    7.3: [default]
...
---
document: modulemd-defaults
version: 1
data:
  module: remi-glpi
  profiles:
    9.2: [default]
    9.3: [default]
...

EOT;

function findPackages($from, $dest, $pat) {
	$rpms = [];
	foreach (glob("$from/$pat") as $file) {
		$info = rpminfo($file);
		$name = basename($file);
		if (!file_exists("$dest/$name")) {
			if (link($file, "$dest/$name")) {
				echo ".";
			}
		}
		$rpms[] = 
			$info['Name'] . '-' . 
			($info['Epoch'] ?? '0') . ':' . 
			$info['Version'] . '-' . 
			$info['Release'] . '.' . 
			$info['Arch'];
	}
	echo "\n  " . count($rpms) . " rpms\n";
	return $rpms;
}

function genModule($srce, $dest, $pat, $ver, $arch, $dist, $template) {

	$rpms = findPackages($srce, $dest, $pat);
	$mod = str_replace(['@VERSION@', '@ARCH@', "@DATE@", "@DIST@"], [$ver, $arch, date("YmdHis"), $dist], $template);
	foreach($rpms as $rpm) {
		$mod .= "    - $rpm\n";
	}
	$mod .= "...\n";
	return $mod;
}

function cleanup($dest) {
	foreach (glob("$dest/*rpm") as $file) {
		unlink($file);
	}
}

function createRepo($dest, $mod) {
chdir($dest);

exec("mkrepo nocheck noclean");
$data = '/tmp/modules.yaml';
file_put_contents($data, $mod);
$hash = hash('sha256', $mod);
exec("modifyrepo_c --mdtype=modules $data repodata");
$arch = basename($dest);
rename($data, "$data.$arch");
echo "  in $data.$arch\n";

chdir(__DIR__);
}

$dest = 'fedora/29/modular/x86_64';
echo "Populate $dest\n";
cleanup($dest);
$mod  = genModule('fedora/29/remi/x86_64',   $dest, 'php-*7.2*.rpm', '7.2', 'x86_64', 'f29', $template_php);
$mod .= genModule('fedora/29/php73/x86_64',  $dest, '*.rpm',         '7.3', 'x86_64', 'f29', $template_php);
$mod .= genModule('fedora/29/glpi92/x86_64', $dest, '*.rpm',         '9.2', 'x86_64', 'f29', $template_glpi);
$mod .= genModule('fedora/29/glpi93/x86_64', $dest, '*.rpm',         '9.3', 'x86_64', 'f29', $template_glpi);
$mod .= $defaults;
createRepo($dest, $mod);

$dest = 'fedora/29/modular/i386';
echo "Populate $dest\n";
cleanup($dest);
$mod  = genModule('fedora/29/remi/i386',   $dest, 'php-*7.2*.rpm', '7.2', 'i386', 'f29', $template_php);
$mod .= genModule('fedora/29/php73/i386',  $dest, '*.rpm',         '7.3', 'i386', 'f29', $template_php);
$mod .= genModule('fedora/29/glpi92/i386', $dest, '*.rpm',         '9.2', 'i386', 'f29', $template_glpi);
$mod .= genModule('fedora/29/glpi93/i386', $dest, '*.rpm',         '9.3', 'i386', 'f29', $template_glpi);
$mod .= $defaults;
createRepo($dest, $mod);