<?php

    define('APPLICATION_PATH', dirname(__FILE__) . '/../');
    set_include_path(get_include_path() . PATH_SEPARATOR . APPLICATION_PATH);

    require_once('includes/bot_challenge.php');
    require_once('includes/autoload.php');
    require_once('includes/functions.php');

    Helpers_Sentry::install();

    if (!empty($_REQUEST['xh'])) {
        define('DO_XHPROF', true);
        startXHProf();
    } else {
        define('DO_XHPROF', false);
    }

    if (doPhpDebugBar()) {
        define('DO_PHP_DEBUG_BAR', true);
        $debugbar = Helpers_PhpDebugBar::getInstance();
    } else {
        define('DO_PHP_DEBUG_BAR', false);
    }

    $conf = Helpers_Config::getInstance();
    $site = Helpers_Site::getInstance();

    $site->addHeader('Content-Type: text/html; charset=UTF-8');

    require_once('includes/session.php');

    session_start();

    Controllers_Default_Apple::setSessionState();

    // if the dude has an autoLogin cookie
    // then we want to log him in...
    switch ($site->getSkin()->app_dir) {
    case 'admin':
        Controllers_Admin_Login::autoLogin();
        break;
    case 'dash':
        Controllers_Dash_Login::autoLogin();
        break;
    case 'sales':
        Controllers_Sales_Login::autoLogin();
        break;
    default:
        Controllers_Default_Login::autoLogin();
    }

    apache_setenv('tbg_session_id', session_id());
    apache_setenv('tbg_skin_domain', $_SERVER['SERVER_NAME']);
    if (!empty($_SESSION['customer_id'])) { apache_setenv('tbg_customer_id', $_SESSION['customer_id']); }

    // track 'affiliate' traffic
    $site->setBid();

    // track any promotional info
    $site->setPromo();

    $tmpl = Helpers_Tmpl::getInstance();
    $geoip = Helpers_GeoIP::getInstance();

    if (DO_PHP_DEBUG_BAR) {
        $tmpl->assign('debugbar', $debugbar);
    }

    $tmpl->assign('tmpl', $tmpl);
    $tmpl->assign('site', $site);
    $tmpl->assign('geoip', $geoip);
    $tmpl->assign('conf', $conf);
    $tmpl->assign('shopping_cart', $site->getShoppingCart());

    $site->runSite();

    if (DO_XHPROF) {
        stopXHProf();
    }

?>
