#!/usr/bin/php
<?php /* -*- mode: php -*- */

/**
 * Development script.
 *
 * This file is part of Assegai
 *
 * Assegai is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * Assegai is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Assegai.  If not, see <http://www.gnu.org/licenses/>.
 */

function error($message, $error = false, $exit_code = -1)
{
    if(!$error) {
        echo '[WARNING]: ';
    } else {
        echo '[ERROR]: ';
    }

    echo $message . PHP_EOL;

    if($exit_code > -1) {
        exit($exit_code);
    }
}

function usage($exit_code = -1)
{
    echo <<<'EOT'
Assegai command line management tool
Usage:
    assegai <action> [options...]
Actions:
    help
        Displays this help.
    app <name>
        Creates an new application skeleton for app 'name'.

EOT;

    if($exit_code > -1) {
        exit($exit_code);
    }
}

function check_parms($argv, $number, $error_message, $error_code = -1) {
    if(count($argv) < $number + 1) {
        error($error_message, true);
        usage($error_code);
    }
}

function path($relative)
{
    // Absolute paths.
    if($relative[0] == '/' || preg_match('/^[A-Z]:/i', $relative)) {
        return $relative;
    }
    // Relative.
    return $_SERVER['PWD'] . '/' . $relative;
}

function make_app($app_name) {
    // Getting the config.
    $conf = array(
        'apps_path' => 'apps',
        );
    $success = @include(path('conf.php'));
    if(!$success) {
        error("Couldn't open `conf.php'. Are you in the framework's root?", true, 3);
    }

    if(!file_exists(path($conf['apps_path']))) {
        mkdir(path($conf['apps_path']));
    }

    if(file_exists(path($conf['apps_path'] . '/' . $app_name))) {
        error("Application `$app_name' already exists.", true, 4);
    }

    $app_path = path($conf['apps_path'] . '/' . $app_name);

    mkdir($app_path);
    mkdir($app_path . '/controllers');
    mkdir($app_path . '/models');
    mkdir($app_path . '/views');
    mkdir($app_path . '/tests');
    mkdir($app_path . '/exceptions');
    file_put_contents($app_path . '/conf.php', '<?php' . PHP_EOL
                      . '// App configuration file' . PHP_EOL
                      . '//$app["route"] = array("/" => "MyApp_Controller_Foo::bar");' . PHP_EOL);
    echo 'DONE' . PHP_EOL;
}

function main($argv) {
    check_parms($argv, 1, 'Not enough arguments.', 1);

    switch($argv[1]) {
    case 'app':
        check_parms($argv, 2, 'Not enough parameters to create application.', 2);
        make_app($argv[2]);
        break;
    default:
        usage(0);
    }
}

main($_SERVER['argv']);

?>
