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

use GuzzleHttp\Client;
use Symfony\Component\Console\Application;
use Dominikb\ComposerLicenseChecker\CheckCommand;
use Dominikb\ComposerLicenseChecker\LicenseLookup;
use Dominikb\ComposerLicenseChecker\ReportCommand;
use Symfony\Component\Console\Output\ConsoleOutput;
use Dominikb\ComposerLicenseChecker\DependencyLoader;
use Dominikb\ComposerLicenseChecker\JSONDependencyParser;
use Dominikb\ComposerLicenseChecker\ConstraintViolationDetector;

function includeIfExists(string $file): bool
{
    return file_exists($file) && include $file;
}

if (
    !includeIfExists(__DIR__ . '/../autoload.php') &&
    !includeIfExists(__DIR__ . '/../../autoload.php') &&
    !includeIfExists(__DIR__ . '/../../../autoload.php') &&
    !includeIfExists(__DIR__ . '/../../../../autoload.php') &&
    !includeIfExists(__DIR__ . '/vendor/autoload.php')
) {
    fwrite(STDERR, 'Install dependencies using Composer.'.PHP_EOL);
    exit(1);
}

$output = new ConsoleOutput();

$httpClient = new Client;

$licenceLookUp = new LicenseLookup($httpClient);

$checkCommand = new CheckCommand;
$checkCommand->setLicenseLookup($licenceLookUp);
$checkCommand->setLicenseConstraintHandler(new ConstraintViolationDetector);
$checkCommand->setDependencyLoader(new DependencyLoader(new JSONDependencyParser()));

$reportCommand = new ReportCommand;
$reportCommand->setLicenseLookup($licenceLookUp);
$reportCommand->setDependencyLoader(new DependencyLoader(new JSONDependencyParser()));

$application = new Application('composer-license-checker', '2.6.0');

$application->addCommands([$checkCommand, $reportCommand]);

$application->run();
