#!/usr/bin/php

<?php

if (!isset($argv[1])) die(help());

$argv[1]($argv);
// module | controller | help | 
function help() {
    echo "Use `./create` with params 

    ./create module <module_Name>                           - create module 
    ./create controller <module_Name> <controller_Name>     - create controller in moodule
    ./create db upgrade                                     - db update
    ./create ramdrive <size>                                - mount /cache dir as RAM DRIVE with specified size (64M,128K...)
";
}

function db($params) {
    $n = $params[2] ?? 'name';
    $fname = 'db_up'.time().'_'.$n;

    $template = <<<TEMPLATE
<?php

function {$fname}_up(){
    return "create table example";
};


function {$fname}_down(){
    return "drop table example";
};
?>
TEMPLATE;

    file_put_contents("site/db/upgrades/{$fname}.php",$template);
    echo "\nFile $fname created\n";
}


function module($params) {
    $root = dirname(__FILE__).'/../../';
    $m = $params[2];
    mkdir($root.'/site/modules/'.$m.'/controllers',0750, true);
    mkdir($root.'/site/modules/'.$m.'/views',0750, true);
    file_put_contents($root.'/site/modules/'.$m.'/views/index.php', "it's module $m ");

    controller([
        2   => $m,
        3   => 'index'
    ]);
}

function controller($params) {
    $m = $params[2];
    $c = $params[3];

    if (!$m || !$c) die("\nPlease specify module and controller name to create\n");

    $root = dirname(__FILE__).'/../../';

    file_put_contents($root.'/site/modules/'.$m."/controllers/{$c}Controller.php", getControllerTemplate($params));
    

    file_put_contents("{$root}/site/modules/{$m}/views/{$c}.php","
        <div style='padding: 5%;line-height: 180%;font-size: 24px;'>
            <p>Hello its a <strong>{$c}</strong> controller of <strong>{$m}</strong> module.</p>
            <p>Located in <strong>/site/modules/{$m}/controllers/{$c}Controller.php</strong></p>
            <p>This view locates in <strong>/site/modules/{$m}/views/{$c}.php</strong></p>
        </div>
    ");

    echo "\nController {$c} created in module {$m}

    Controller file: /site/modules/{$m}/controllers/{$c}Controller.php
    View file:       /site/modules/{$m}/views/{$c}.php
    ";
}

function ramdrive($params) {
    $size = $params[2] ?? '64M';

    if (!is_dir("./cache")) 
        mkdir("./cache", 0777, true);

    $result = 19;
    system("sudo mount -t tmpfs -o size={$size} tmpfs ./cache", $result);

    if ($result==0)
        echo "\n'./cache' dir mounted as RAMDRIVE with size {$size}\n\n";
    else   
        echo "\nSomething goes wrong on mount ./cache as ramdrive code is ".$result;

    
}

function getControllerTemplate($params) {
    $template = $params[3];
    $m = ucfirst($params[2]);
    $c = ucfirst($params[3]);
    
    $className = "{$m}{$c}Controller";

    $controllerTemplate = <<<CONTROLLER
    <?php
    
    use olala\controller;
    
    class $className extends controller {
        function indexAction() {
            return \$this->render("{$template}");
        }
    }
    
    ?>
    CONTROLLER;
    return $controllerTemplate;
}
?>

