#!/usr/bin/env php
<?php

// define('FEED', '/tmp/phars.json');
define('FEED', 'https://github.com/civicrm/civicrm-buildkit/raw/master/phars.json');
define('COMMANDS', 'civistrings civix coworker cv');
define('COMPOSER_JSON', dirname(__DIR__) . '/composer.json');

if (PHP_SAPI !== 'cli') {
  die("Update can only be run from command line.");
}

###############################################################################

/**
 * Take the list of current PHAR releases from buildkit:phars.json
 * and update cli-tools:composer.json.
 */
function main(): void {
  $tag = 'v' . gmdate('Y.m.d.') . time();
  echo "[tag=$tag]\n";

  $pharsJson = json_decode(file_get_contents(FEED), TRUE);
  $composerJson = json_decode(file_get_contents(COMPOSER_JSON), TRUE);
  $commands = explode(' ', COMMANDS);

  foreach ($commands as $command) {
    $url = $pharsJson[$command]['url'];
    $version = trim(strtr(basename($url), [
      $command => "",
      ".phar" => "",
    ]), "-");
    $composerJson['extra']['downloads']["{$command}.phar"] = [
      'version' => ltrim($version, "v"),
      'url' => $url,
    ];
  }

  file_put_contents(COMPOSER_JSON, json_encode($composerJson, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
  runf('composer update --lock');
  runf('git commit composer.json composer.lock -m %s', "Update $tag");
  runf('git tag %s', $tag);
}

/**
 * Run a command with printf notation.
 *
 * Variables with "%s" are shell-escaped.
 */
function runf(string $cmd, ...$args): void {
  $escArgs = array_map('escapeshellarg', $args);
  $cmd = sprintf($cmd, ...$escArgs);
  passthru($cmd, $result);
  if ($result !== 0) {
    throw new \Exception("Command ($cmd) failed");
  }
}

###############################################################################
main();
