#!/usr/bin/env php
<?php

use Aplia\Bootstrap\Base;

// Disable cache loading, we want to create it from scratch
$GLOBALS['STARTER_APP_CACHE'] = false;
$GLOBALS['STARTER_BASE_INIT'] = false;
require_once __DIR__ . '/../../../../config.php';

if (version_compare(PHP_VERSION, "5.4", '<')) {
    echo "bootstrap_build requires PHP version 5.4 or higher\n";
    exit();
}

$generator = new \Aplia\Bootstrap\Generator;
$frameworks = isset($GLOBALS['STARTER_ALL_FRAMEWORKS']) ? $GLOBALS['STARTER_ALL_FRAMEWORKS'] : array('ezp', 'plain');
foreach ($frameworks as $framework) {
    $GLOBALS['STARTER_FRAMEWORK'] = $framework;

    $app = \Aplia\Bootstrap\Base::createApp(array());

    // Configure the app unless STARTER_BASE_CONFIGURE tells us not to
    $app->configure(\Aplia\Bootstrap\Base::fetchConfigNames());
    $app->postConfigure();

    $buildPath = $app->config->get("app.buildPath");
    $configCache = $app->makePath(array($buildPath, "config_${framework}.json"));
    $generator->writeOptimizedConfig($app, $configCache);
    echo "Created $configCache\n";

    $bootstrap = $app->makePath(array($buildPath, "bootstrap_${framework}.php"));
    $generator->writeAppBundle($app, $bootstrap);
    echo "Created $bootstrap\n";
}
