<?php
namespace Hanyuu;

use Index\Db\DbBackends;
use Index\Config\Fs\FsConfig;

define('HAU_STARTUP', microtime(true));
define('HAU_ROOT', __DIR__);
define('HAU_CLI', PHP_SAPI === 'cli');
define('HAU_DEBUG', is_file(HAU_ROOT . '/.debug'));
define('HAU_DIR_ASSETS', HAU_ROOT . '/assets');
define('HAU_DIR_PUBLIC', HAU_ROOT . '/public');
define('HAU_DIR_SOURCE', HAU_ROOT . '/src');
define('HAU_DIR_MIGRATIONS', HAU_ROOT . '/database');
define('HAU_DIR_TEMPLATES', HAU_ROOT . '/templates');

require_once HAU_ROOT . '/vendor/autoload.php';

error_reporting(HAU_DEBUG ? -1 : 0);
mb_internal_encoding('UTF-8');
date_default_timezone_set('GMT');

$cfg = FsConfig::fromFile(HAU_ROOT . '/hanyuu.cfg');

if($cfg->hasValues('sentry:dsn'))
    (function($cfg) {
        \Sentry\init([
            'dsn' => $cfg->getString('dsn'),
            'traces_sample_rate' => $cfg->getFloat('tracesRate', 0.2),
            'profiles_sample_rate' => $cfg->getFloat('profilesRate', 0.2),
        ]);

        set_exception_handler(function(\Throwable $ex) {
            \Sentry\captureException($ex);
        });
    })($cfg->scopeTo('sentry'));

$dbc = DbBackends::create($cfg->getString('database:dsn', 'null'));
$dbc->execute('SET SESSION time_zone = \'+00:00\', sql_mode = \'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION\';');

$hau = new HanyuuContext($cfg, $dbc);