#!/usr/bin/env php
<?php declare(strict_types=1);
/*
 * Copyright (c) 2010-2014 Pierrick Charron
 * Copyright (c) 2016-2018 Holger Woltersdorf
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

namespace hollodotme\FastCGI;

use hollodotme\FastCGI\Requests\GetRequest;
use hollodotme\FastCGI\SocketConnections\NetworkSocket;
use hollodotme\FastCGI\SocketConnections\UnixDomainSocket;

if ( file_exists( __DIR__ . '/../vendor/autoload.php' ) )
{
	require __DIR__ . '/../vendor/autoload.php';
}
elseif ( file_exists( __DIR__ . '/../../../autoload.php' ) )
{
	require __DIR__ . '/../../../autoload.php';
}
else
{
	die( "No proper composer installation. autoload.php not found.\n" );
}

if ( !isset( $_SERVER['argc'] ) )
{
	die( "Command line only\n" );
}

if ( $_SERVER['argc'] < 2 )
{
	echo 'Usage: ' . $_SERVER['argv'][0] . "  URI\n\n";
	echo 'Ex: ' . $_SERVER['argv'][0] . " localhost:9000/status\n";
	echo 'Ex: ' . $_SERVER['argv'][0] . " /var/run/php-fpm/web.sock/status\n";
	exit( 1 );
}

if ( preg_match( '#^unix:(.*.sock)(/.*)$#', $_SERVER['argv'][1], $reg ) )
{
	$url  = parse_url( $reg[2] );
	$sock = $reg[1];
	if ( !file_exists( $sock ) )
	{
		die( "UDS {$sock} not found\n" );
	}

	if ( !is_writable( $sock ) )
	{
		die( "UDS {$sock} is not writable\n" );
	}
}
else
{
	$url  = parse_url( $_SERVER['argv'][1] );
	$sock = false;
}
if ( !$url || !isset( $url['path'] ) )
{
	die( 'Malformed URI' );
}

$req = '/' . basename( $url['path'] );
if ( isset( $url['query'] ) )
{
	$uri = $req . '?' . $url['query'];
}
else
{
	$url['query'] = '';
	$uri          = $req;
}
if ( $sock )
{
	$client = new Client( new UnixDomainSocket( (string)$sock ) );
	echo "Call: {$uri} on UDS unix://{$sock}\n\n";
}
else
{
	$host   = (string)($url['host'] ?? 'localhost');
	$port   = (int)($url['port'] ?? 9000);
	$client = new Client( new NetworkSocket( $host, $port ) );

	echo "Call: {$uri} on {$host}:{$port}\n\n";
}

$request = new GetRequest( $url['path'], '' );
$request->addCustomVars(
	[
		'SCRIPT_NAME'  => $req,
		'QUERY_STRING' => $url['query'],
		'REQUEST_URI'  => $uri,
		'DOCUMENT_URI' => $req,
	]
);

$response = $client->sendRequest( $request );

echo $response->getRawResponse() . "\n\n";
echo 'Duration: ' . $response->getDuration() . " Sec.\n";
