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

$files = array(
  __DIR__ . '/../vendor/autoload.php',
  __DIR__ . '/../../../autoload.php',
  __DIR__ . '/../Mf2/Parser.php'
);

foreach ($files as $file) {
	if (file_exists($file)) {
		require $file;
		define('COMPOSER_INSTALL_LOCATION', $file);
		break;
	}
}

if (!defined('COMPOSER_INSTALL_LOCATION')) {
	die('A composer /vendor/autoload.php file could not be found. You need to install composer: https://getcomposer.org/download');
}

if (!empty($argv[1])) {
	$url = $argv[1];
	if (!preg_match('|https?://|', $url)) {
		$url = 'http://' . $url;
	}	
} else {
	$url = null;
}

$result = Mf2\parse(fgets(STDIN), $url);

if (defined('JSON_PRETTY_PRINT')) {
	echo json_encode($result, JSON_PRETTY_PRINT);
} else {
	echo json_encode($result);
}
