#!/usr/bin/env php
<?php
/**
 * @package     Arastta eCommerce
 * @copyright   2015-2017 Arastta Association. All rights reserved.
 * @copyright   See CREDITS.txt for credits and other copyright notices.
 * @license     GNU GPL version 3; see LICENSE.txt
 * @link        https://arastta.org
 *
 *
 * DO NOT EDIT THIS FILE.!.!
 *
 */

if (php_sapi_name() !== 'cli') die('CLI Only');

use Symfony\Component\Console\Application;

define('AREXE', 1);

// define STDIN, STDOUT and STDERR if the PHP SAPI did not define them (e.g. creating console application in web env)
// http://php.net/manual/en/features.commandline.io-streams.php
defined('STDIN') or define('STDIN', fopen('php://stdin', 'r'));
defined('STDOUT') or define('STDOUT', fopen('php://stdout', 'w'));
defined('STDERR') or define('STDERR', fopen('php://stderr', 'w'));

define('IS_ADMIN', false);

// Directories
$base = __DIR__;

define('DIR_BASE',          $base . '/');
define('DIR_ROOT',          $base . '/');
define('DIR_CLI',           DIR_ROOT . 'cli/');
define('DIR_INSTALL',       DIR_ROOT . 'install/');
define('DIR_SYSTEM',        DIR_ROOT . 'system/');
define('DIR_ADMIN',         DIR_ROOT . 'admin/');
define('DIR_CATALOG',       DIR_ROOT . 'catalog/');
define('DIR_VQMOD',         DIR_ROOT . 'vqmod/');
define('DIR_IMAGE',         DIR_ROOT . 'image/');
define('DIR_DOWNLOAD',      DIR_ROOT . 'download/');
define('DIR_UPLOAD',        DIR_ROOT . 'upload/');
define('DIR_CONFIG',        DIR_SYSTEM . 'config/');
define('DIR_CACHE',         DIR_SYSTEM . 'cache/');
define('DIR_MODIFICATION',  DIR_SYSTEM . 'modification/');
define('DIR_LOG',           DIR_SYSTEM . 'log/');

if (!file_exists(DIR_SYSTEM.'vendor/autoload.php')) {
    die('You need to run Composer first. More details https://github.com/arastta/arastta');
}

if (!function_exists('is_installed')) {
    function is_installed()
    {
        if (!file_exists(DIR_ROOT . 'config.php')) {
            return false;
        }

        return true;
    }
}

// Define some required constants, must remove these later to make a totally app based system
$_SERVER['HTTP_HOST'] = '';

// Would love to have $_SERVER['HTTP_HOST'] variable sent, not available in CLI
foreach ($argv as $argument) {
    if (strpos($argument, '--http_host') !== false) {
        $_SERVER['HTTP_HOST'] = str_replace('--http_host=', '', $argument);

        define('HTTP_SERVER', 'http://' . $_SERVER['HTTP_HOST'] . '/');
        define('HTTPS_SERVER', (!empty($_SERVER['HTTPS']) ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . '/');
        define('HTTP_ADMIN', HTTP_SERVER . '/admin/');
        define('HTTPS_ADMIN', HTTPS_SERVER . '/admin/');

        break;
    }
}

// TODO: find a better way here
if (!defined('HTTP_SERVER')) {
    define('HTTP_SERVER', '');
}
if (!defined('HTTPS_SERVER')) {
    define('HTTPS_SERVER', '');
}

define('HTTP_CATALOG', HTTP_SERVER);

require_once(DIR_SYSTEM . 'library/client.php');

$admin = null;
$catalog = null;
$install = null;

if (is_installed()) {
    require_once(DIR_SYSTEM . 'startup.php');

    Client::setName('catalog');
    $catalog = new Catalog();
    $catalog->initialise();

    Client::setName('admin');
    $admin = new Admin();
    $admin->initialise();
} else {
    define('DIR_LANGUAGE', 		DIR_INSTALL . 'language/');
    define('DIR_TEMPLATE', 		DIR_INSTALL . 'view/theme/');

    Client::setName('install');

    require_once(DIR_SYSTEM . 'startup.php');

    $install = new Install();
    $install->initialise();
}

require_once(DIR_SYSTEM . 'library/command.php');

$console = new Application('Arastta CLI', '1.0');
$app = new Cli($console);

// set other apps for later usage
$app->set('admin', $admin);
$app->set('catalog', $catalog);
$app->set('install', $install);
$app->set('apps', array());

Client::setName('cli');

$app->initialise();

$console->run();
