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

$autoloadPath1 = __DIR__ . '/../../../autoload.php';
$autoloadPath2 = __DIR__ . '/../vendor/autoload.php';

if (file_exists($autoloadPath1)) {
    require_once $autoloadPath1;
} else {
    require_once $autoloadPath2;
}

use function Differ\Differ\genDiff;

$doc = <<<DOC
Generate diff

Usage:
  gendiff (-h|--help)
  gendiff (-v|--version)
  gendiff [--format <fmt>] <firstFile> <secondFile>

Options:
  -h --help                     Show this screen
  -v --version                  Show version
  --format <fmt>                Report format [default: stylish]
DOC;

$args = Docopt::handle($doc, array('version'=>'Gendiff 1.0'));

$argsDictionary = [];
foreach ($args as $key => $value) {
    $argsDictionary[$key] = $value;
}

$format = $argsDictionary['--format'];

$path1 = $argsDictionary['<firstFile>'];
$path2 = $argsDictionary['<secondFile>'];

try {
    $differences = genDiff($path1, $path2, $format);
} catch (Exception $e) {
    $msg = sprintf("Error: %s\n", $e->getMessage());
    exit($msg);
}

print_r($differences . "\n");
