#!/usr/bin/php
<?php
include('src/RESTful/Settings.php');

function exit_unless($condition, $msg = null) {
    if ($condition)
        return;
    echo "[FAIL] $msg";
    exit(1);
}

echo "Building Phar... ";
$base_dir = dirname(__FILE__);
$source_dir = $base_dir . '/src/RESTful/';
$phar_name = 'restful.phar';
$phar_path = $base_dir . '/' . $phar_name;
$phar = new Phar($phar_path, 0, $phar_name);
$stub = <<<HEREDOC
<?php
    // Phar Stub File
    Phar::mapPhar('restful.phar');
    include('phar://restful.phar/RESTful/Bootstrap.php');
    \RESTful\Bootstrap::pharInit();

    __HALT_COMPILER();
HEREDOC;
$phar->setStub($stub);
exit_unless($phar, "Unable to create a phar. Make sure you have phar.readonly=0 set in your ini file.");
$phar->buildFromDirectory(dirname($source_dir));
echo "[ OK ]\n";

echo "Renaming Phar... ";
$phar_versioned_name = 'restful-' . \RESTful\Settings::VERSION . '.phar'; 
$phar_versioned_path = $base_dir . '/' . $phar_versioned_name; 
rename($phar_path, $phar_versioned_path);
echo "[ OK ]\n";