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

error_reporting(E_ALL);

include 'scss.inc.php';

use Leafo\ScssPhp\Compiler;
use Leafo\ScssPhp\Parser;

$opts = getopt('hvTf:', array('help', 'version'));

function has() {
    global $opts;

    foreach (func_get_args() as $arg) {
        if (isset($opts[$arg])) {
            return true;
        }
    }

    return false;
}

if (has("h", "help")) {
    $exe = array_shift($argv);

$HELP = <<<EOT
Usage: $exe [options] < input-file

Options include:

    -h, --help     Show this message
    -v, --version  Print the version
    -f=format      Set the output format (compressed, crunched, expanded, or nested)
    -T             Dump formatted parse tree

EOT;
    exit($HELP);
}

if (has("v", "version")) {
    exit(Compiler::$VERSION . "\n");
}

$data = "";

while (!feof(STDIN)) {
    $data .= fread(STDIN, 8192);
}

if (has("T")) {
    $parser = new Parser("STDIN");

    print_r($parser->parse($data));

    exit();
}

$scss = new Compiler();

if (has("f")) {
    $scss->setFormatter('Leafo\\ScssPhp\\' . ucfirst($opts["f"]));
}

echo $scss->compile($data, "STDIN");
