/*
 * folderify
 * https://github.com/parroit/folderify
 *
 * Copyright (c) 2013 parroit
 * Licensed under the MIT license.
 */



var brfs = require("brfs"),
    concat = require("concat-stream"),
    includeFolder = require("include-folder"),
    falafel = require('falafel'),
    through = require('through'),
    data,
    pending,
    ifNames,
    tr;



function isIF (node) {
    var c = node.callee;
    return c &&
        node.type === 'CallExpression' &&
        c.type === 'Identifier' &&
        c.name === 'require'
        ;
}


function write (buf) {
    data += buf;
}

function end () {



    try {
        parse();
    }
    catch (err) {
        console.log("%s\n%s",err.message,err.stack);
        this.emit('error', err);
    }


}

function finish (output) {
    tr.queue(String(output));
    tr.queue(null);
}


function unrequireIF(node) {
    function unrequire(n){
        n.update("undefined");
    }
    if (isIF(node) && node.arguments[0].value === 'include-folder' &&
        node.parent.type === 'VariableDeclarator'  &&
        node.parent.id.type === 'Identifier') {

        ifNames[node.parent.id.name] = true;
        //console.dir(node.parent)
        unrequire(node.parent.init);
    }
    if (isIF(node) && node.arguments[0].value === 'include-folder'   &&
        node.parent.type === 'AssignmentExpression'    &&
        node.parent.left.type === 'Identifier') {

        ifNames[node.parent.left.name] = true;

        unrequire(node.parent.right);
    }
}

function buildOriginalSource(folder) {
    var fnBody = includeFolder._testHook.buildSource(folder);
    return "(function(){" +
        fnBody +
        "})()";
}

function parse() {

    var output = falafel(data, function (node) {
        unrequireIF(node);

        if (node.type === 'CallExpression' && node.callee && ifNames[node.callee.name]) {
            var folder = node.arguments[0].value;
            if (folder.charAt(folder.length - 1) !== "/") {
                folder += "/";
            }
            var originalSource;

            originalSource = buildOriginalSource(folder);

            var brfsStream = brfs(folder+"bogus.txt");

            var brfsResult = concat(function (data) {
                //console.log("\n\npending:%s\n\n",pending);
                node.update(data);
                pending--;
                if (pending === 0) {
                    finish(output);
                }
            });

            brfsStream.on("error",function(err){
                //console.log(err);
                console.log("%s\n%s",err.message,err.stack);
                this.emit('error', err);
            });

            brfsStream.pipe(brfsResult);
            //console.log(originalSource);
            brfsStream.write(originalSource);
            brfsStream.end();

            pending++;
        }
    });

    if (pending === 0) {
        finish(output);
    }

}

function folderify() {
    data = '';
    pending = 0;
    ifNames = {};
    tr = through(write, end);

    return tr;

}


module.exports = folderify;
@@ -413,24 +413,80 @@
 return c &&%0A
+        node.arguments%5B0%5D.value === 'include-folder' &&%0A
         node
@@ -895,27 +895,25 @@
 unction 
-unrequireIF
+isVarDecl
 (node) %7B
@@ -921,134 +921,186 @@
     
-function unrequire(n)%7B%0A        n.update(%22undefined%22);
+return isIF(node) &&%0A        node.parent.type === 'VariableDeclarator' &&
 %0A    
-%7D%0A
    
- if (isIF(node) &&
  node.
+p
 ar
-guments%5B0%5D.value === 'include-folder'
+ent.id.type === 'Identifier'%0A%0A    ;%0A%7D%0Afunction isVarAssign(node) %7B%0A    return isIF(node)
  &&%0A
@@ -1129,36 +1129,37 @@
 pe === '
-VariableDeclarator' 
+AssignmentExpression'
  &&%0A    
@@ -1166,34 +1166,36 @@
     node.parent.
-id
+left
 .type === 'Ident
@@ -1192,32 +1192,158 @@
 === 'Identifier'
+%0A%0A    ;%0A%7D%0Afunction unrequireIF(node) %7B%0A    function unrequire(n)%7B%0A        n.update(%22undefined%22);%0A    %7D%0A    if (isVarDecl(node)
 ) %7B%0A%0A        ifN
@@ -1380,42 +1380,8 @@
 ue;%0A
-        //console.dir(node.parent)
 %0A   
@@ -1423,185 +1423,35 @@
    %7D
-%0A    if (isIF(node) && node.arguments%5B0%5D.value === 'include-folder'   &&%0A        node.parent.type === 'AssignmentExpression'    &&%0A        node.parent.left.type === 'Identifier'
+ else if (isVarAssign(node)
 ) %7B%0A

improve climate