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

if (1 == $argc)
    $argv = [0, ''];

$d = 'd' == $argv[1];
if ($d || 'php' == $argv[1]) {
    require __DIR__ . '/w2/rare.php';
    $dirs = Rare::walk_dirs('.');
    if ($d) {
        print_r($dirs);
    } else foreach($dirs as $one) {
        $files = Rare::list_path($one, 'is_file');
        foreach($files as $one)
            system("php -l $one");
    }

} else {
    define('DIR_R', getcwd());
    $found = [];
    $test = function ($cd, $m) use (&$found) {
        $app = is_file($fn = "$m/conf.php") && strpos(file_get_contents($fn), 'DIR_M');
        $git = is_dir('.git');
        $ware = is_file('conf.php') && strpos(file_get_contents('conf.php'), '$plans');
        $found = [$app, $git, $ware];
        return $app || $git || $ware;
    };
    $m = basename(__DIR__);
    $m2 = 'air' == $m ? 'main' : $m;
    do {
        if ($test($cd = getcwd(), $m) || $test($cd, $m = $m2))
            break;
        $m = basename($cd);
        chdir('..');
        if ($cd == getcwd()) {
            chdir(DIR_R);
            break;
        }
    } while(true);

    if ($found[0]) {
        define('DIR', $cd);
        require "$m/conf.php";
        $sky = new SKY;
    } else {
        define('DIR_S', __DIR__);
        require DIR_S . '/w2/console.php';
    }
    new Console($argv, $found);
}

echo "\n";
