#!/usr/bin/php
<?php
set_time_limit(180);

if (version_compare('5.2.0', PHP_VERSION, '>')) {
  echo 'This version of Program requires PHP 5.2.0.'.PHP_EOL;
  exit;
}

$functions = array(
  'curl_init',
  'json_encode',
  'openssl_csr_export',
  'openssl_csr_new',
  'openssl_pkey_export',
  'openssl_pkey_new',
  'openssl_private_encrypt',
  'openssl_x509_parse',
);
foreach ($functions as $func) {
  if (!function_exists($func)) {
    echo $func . ' function is not supported.'.PHP_EOL;
    exit;
  }
}

define('FUJISSL_GO_ROOT', dirname(dirname(__FILE__)));

$autoload = array(
  '/lib/Config.php',
  '/lib/Cache.php',
  '/lib/CmdClear.php',
  '/lib/CmdCreate.php',
  '/lib/CmdHelp.php',
  '/lib/Fhandle.php',
  '/lib/Log.php',
  '/lib/Openssl.php',
  '/src/Constant.php',
  '/src/Directory.php',
  '/src/Application.php',
  '/src/Request.php',
  '/src/Router.php',
);

foreach ($autoload as $file) {
  if (file_exists(FUJISSL_GO_ROOT . $file)) {
    require FUJISSL_GO_ROOT . $file;
  }
}

$router = new Router();
$router->configure($_SERVER['argv']);

$Class = ucfirst($router->getMode());

try {
  $app = new $Class();
  $app->run($router);
  
} catch (Exception $e) { 
  
  trigger_error($e->getMessage(), E_USER_ERROR);

}

exit;
