#!/usr/bin/env php
<?php
declare(strict_types=1);

use Commando\{Command};
use function Which\{which};

/**
 * @var string The version number of this package.
 */
const VERSION = '6.2.0';

/**
 * Application entry point.
 */
function main() {
  // Initialize the application.
  @cli_set_process_title('Which.php');

  // Parse the command line arguments.
  $program = new Command;
  $program->setHelp('Find the instances of an executable in the system path.');

  $program->flag('a')->aka('all')
    ->description('List all instances of executables found (instead of just the first one).')
    ->boolean();

  $program->flag('s')->aka('silent')
    ->description('Silence the output, just return the exit code (0 if any executable is found, otherwise 1).')
    ->boolean();

  $program->flag('v')->aka('version')
    ->description('Output the version number.')
    ->boolean();

  $program->argument()->referToAs('command')
    ->description('The program to find.');

  if ($program['version']) {
    echo VERSION, PHP_EOL;
    exit(0);
  }

  if (!is_string($program[0])) {
    $program->printHelp();
    exit(64);
  }

  // Run the program.
  $executables = which($program[0], $program['all'], function() { exit(1); });
  if (!$program['silent']) {
    if (!is_array($executables)) $executables = [$executables];
    foreach ($executables as $path) echo $path, PHP_EOL;
  }

  exit(0);
}

// Start the application.
try {
  $fileInfo = new SplFileInfo(__DIR__.'/../../../autoload.php');
  require_once $fileInfo->isFile() ? $fileInfo->getPathname() : __DIR__.'/../vendor/autoload.php';
  main();
}

catch (\Throwable $error) {
  echo $error, PHP_EOL;
  exit(2);
}
