function get_all_directories($root, $max_depth = 3)
{
    $result = array();
    if ($handle = opendir($root)) {
        while (false !== ($entry = readdir($handle))) {
            if ($entry != "." && $entry != ".." && is_dir($root . "/" . $entry)) {
                $result[] = $root . "/" . $entry;
                if ($max_depth > 1) {
                    $result = array_merge($result, get_all_directories($root . "/" . $entry, $max_depth - 1));
                }
            }
        }
        closedir($handle);
    }
    return $result;
}
    }
}
// add default parameters
$json += array('templates' => 'vendor/*/*', 'template' => 'locale/template.json', 'template_text' => false, 'templates_ignore' => array('/test/', '/tests/'), 'translation_functions' => array('t', 'ht'), 'translation_markers' => array('i18n'), 'extensions' => array('.php', '.haml'), 'include_plural' => true, 'depth' => 3);
if ($debug) {
    echo "Loaded JSON: " . print_r($json, true) . "\n";
}
if (!is_array($json['templates'])) {
    $json['templates'] = array($json['templates']);
}
// make target directories as necessary
make_target_directories(array($json['template']));
// now load all of the components
$all_dirs = array();
foreach ($json['templates'] as $dir) {
    $all_dirs = array_merge($all_dirs, get_all_directories($root . "/" . $dir, $json['depth']));
}
echo "Found " . count($all_dirs) . " potential subdirectories\n";
// this function is from Openclerk::I18n
function strip_i18n_key($key)
{
    $key = preg_replace("/[\r\n]+/im", " ", $key);
    $key = preg_replace("/[\\s]{2,}/im", " ", $key);
    return trim($key);
}
function strip_comments($text)
{
    $text = preg_replace("#([^:])//[^\n]+#ims", "\\1", $text);
    // only strip phpdoc comments
    $text = preg_replace("#/\\*\\*.+?\\*/#ims", "", $text);
    return $text;
if (!file_exists($root . "/asset-discovery.json")) {
    throw new Exception("No asset-discovery.json found in '{$root}'");
}
$json = json_decode(file_get_contents($root . "/asset-discovery.json"), true);
// add default parameters
$json += array('src' => 'vendor/*/*', 'js' => 'generated/js/generated.js', 'css' => 'generated/css/generated.css', 'coffee' => 'generated/coffee/generated.coffee', 'scss' => 'generated/scss/generated.scss', 'images' => 'generated/images/', 'imagetypes' => array('png', 'gif', 'jpg', 'jpeg'), 'depth' => 3);
if (!is_array($json['src'])) {
    $json['src'] = array($json['src']);
}
if (substr($json['images'], -1) !== "/") {
    throw new Exception("'images' parameter needs to be a directory and end in /: '" . $json['images'] . "'");
}
// make target directories as necessary
make_target_directories(array($json['js'], $json['css'], $json['coffee'], $json['scss'], $json['images']));
// now load all of the components
$all_dirs = get_all_directories($root, $json['depth']);
echo "Found " . count($all_dirs) . " potential subdirectories\n";
$selected_dirs = array();
foreach ($json['src'] as $pattern) {
    $selected_dirs = array_merge($selected_dirs, get_directories_to_search($all_dirs, $pattern));
}
echo "Filtered to " . count($selected_dirs) . " matching paths\n";
$javascripts = array();
$stylesheets = array();
$coffeescripts = array();
$sasses = array();
$images = array();
if ($selected_dirs) {
    $filename = "assets.json";
    echo "Processing asset components...\n";
    $count = 0;