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

use Dredd\Server;
use Dredd\Hooks;

ini_set('implicit_flush', 'on');
ini_set('output_buffering', 'off');

$loaded = false;
foreach ([__DIR__ . '/../../../autoload.php', __DIR__ . '/../vendor/autoload.php'] as $file) {
    if (file_exists($file)) {
        require $file;
        $loaded = true;
        break;
    }
}
if (!$loaded) {
    die(
        'You need to set up the project dependencies using the following commands:' . PHP_EOL .
        'wget http://getcomposer.org/composer.phar' . PHP_EOL .
        'php composer.phar install' . PHP_EOL
    );
}

// Get options from the command line
$options = getopt('', [
    'host:',
    'port:',
    'force',
]);

Hooks::loadHooks($argv);

$host = array_key_exists('host', $options)
    ? $options['host']
    : '127.0.0.1';
$port = array_key_exists('port', $options)
    ? $options['port']
    : 61321;

$server = new Server($host, $port);

fprintf(STDOUT, "Starting server\n");
flush();

$server->run(array_key_exists('force', $options));
