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

/**
 * Copyright 2015, AJ Michels
 *
 * This file is part of Note-Script.
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if
 * not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

$possibleAutoloadPaths = [
    __DIR__ . '/../../../autoload.php',
    __DIR__ . '/../../vendor/autoload.php',
    __DIR__ . '/../vendor/autoload.php',
];

$autoload = null;
foreach ($possibleAutoloadPaths as $autoloadPath) {
    if (file_exists($autoloadPath)) {
        $autoload = require $autoloadPath;
        break;
    }
}

if (!$autoload) {
    fwrite(STDERR, sprintf(
        'You need to set up the project dependencies using Composer: %1$s%1$s' .
        '    composer require ajmichels/note-script%1$s%1$s' .
        'You can learn all about Composer on https://getcomposer.org/.%1$s',
        PHP_EOL
    ));

    exit(1);
}

set_error_handler([new NoteScript\ErrorHandler(), 'handle']);

$container = new NoteScript\Container();
include __DIR__ . '/../src/bootstrap.php';

$response = $container->get(NoteScript\Main::class)->process();
if ($response->getError()) {
    fwrite(STDERR, $response->getError());
}
if ($response->getOutput()) {
    fwrite(STDOUT, $response->getOutput());
}
exit($response->getReturnCode());
