<?php
/**
 * Autoload composer libraries
 *
 */
require "vendor/autoload.php";
/**
 * The default extension of resource files. If you change this, all resources
 * must be renamed to use the new extension.
 *
 * @link http://kohanaframework.org/guide/about.install#ext
 */
define('EXT', '.php');

/**
 * Set the PHP error reporting level. If you set this in php.ini, you remove this.
 * @link http://www.php.net/manual/errorfunc.configuration#ini.error-reporting
 *
 * When developing your application, it is highly recommended to enable notices
 * and strict warnings. Enable them by using: E_ALL | E_STRICT
 *
 * In a production environment, it is safe to ignore notices and strict warnings.
 * Disable them by using: E_ALL ^ E_NOTICE
 *
 * When using a legacy application with PHP >= 5.3, it is recommended to disable
 * deprecated notices. Disable with: E_ALL & ~E_DEPRECATED
 */
if (isset($_SERVER['KOHANA_ENV']) && strtoupper($_SERVER['KOHANA_ENV']) == 'PRODUCTION') {
    // Turn off notices and strict errors
    error_reporting(E_ALL | E_STRICT);
} else {
    error_reporting(E_ALL ^ E_NOTICE);
}

/**
 * End of standard configuration! Changing any of the code below should only be
 * attempted by those with a working knowledge of Kohana internals.
 *
 * @link http://kohanaframework.org/guide/using.configuration
 */

// Set the full path to the docroot
define('DOCROOT', realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR);
define('BASEPATH', realpath(DOCROOT).DIRECTORY_SEPARATOR);


// Define the absolute paths for configured directories
define('APPPATH', realpath(BASEPATH . 'application').DIRECTORY_SEPARATOR);
define('MODPATH', realpath(BASEPATH . 'modules').DIRECTORY_SEPARATOR);
define('COMPOSERPATH', realpath(BASEPATH.DIRECTORY_SEPARATOR.'vendor').DIRECTORY_SEPARATOR);
define('SYSPATH', realpath(COMPOSERPATH.'kohana'.DIRECTORY_SEPARATOR.'core').DIRECTORY_SEPARATOR);


if (file_exists('install'.EXT))
{
    // Load the installation check
    return include 'install'.EXT;
}

/**
 * Define the start time of the application, used for profiling.
 */
if ( ! defined('KOHANA_START_TIME'))
{
    define('KOHANA_START_TIME', microtime(TRUE));
}

/**
 * Define the memory usage at the start of the application, used for profiling.
 */
if ( ! defined('KOHANA_START_MEMORY'))
{
    define('KOHANA_START_MEMORY', memory_get_usage());
}

// Bootstrap the application
require APPPATH.'bootstrap'.EXT;

if (PHP_SAPI == 'cli') // Try and load minion
{
    class_exists('Minion_Task') OR die('Please enable the Minion module for CLI support.');
    set_exception_handler(array('Minion_Exception', 'handler'));

    Minion_Task::factory(Minion_CLI::options())->execute();
}
else
{
    /**
     * Execute the main request. A source of the URI can be passed, eg: $_SERVER['PATH_INFO'].
     * If no source is specified, the URI will be automatically detected.
     */
    try
    {
        $request = Request::factory();
        $response = $request->execute();
    }
    catch (HTTP_Exception_404 $e)
    {
        // Log the error
        Kohana::$log->add(Log::WARNING, 'Not found: '.$request->uri().', referrer: '.$request->referrer()); // Kohana_Exception::text($e));

        // Reset the initial request
        Request::$initial = FALSE;
        $request = Request::factory(Route::get('error')->uri(array('action' => '404')));
        $response = $request->execute()->status(404);
    }
    catch (Exception $ke)
    {
        // Log the error
        $strace = Kohana_Exception::text($ke)."\n--\n" . $ke->getTraceAsString();
        Kohana::$log->add(Log::EMERGENCY, $strace);

        // Reset the initial request
        Request::$initial = FALSE;
        $request = Request::factory(Route::get('error')->uri(array('action' => '500')));
        $response = $request->execute()->status(500);
    }

    echo $response->send_headers()->body();
}
