"use strict"

var fs = require('fs');

RegExp.quote = function(str) {
  return (str+'').replace(/([.?*+^$[\]\\(){}|-])/g, "\\$1");
};

module.exports = function (name, cb) {
	// /Users/alessandrominoccheri/Sites/klikkahotel.com/css/backend/_style.css
	var path_explode = name.split('/');
	var last_value = path_explode[path_explode.length-1];
	if(last_value.indexOf('.css') > 0){
		fs.readFile(name, 'utf8', function (err,data) {
			if (err)
			    return console.log(err);

			var class_to_find = 'label-blu';
			var reg = new RegExp('\\.' + RegExp.quote(class_to_find) + '([,:\\s\\.][^\\{]*)?\\{', 'gmi');
			var found = data.match(reg);
			
			if((found != '') && (found != null))
				found = found.length;
			else
				found = 0;

			console.log('found in ' + name + ': ' + found + ' match of ' + class_to_find);
		});
	}
	else{
		if(last_value.indexOf('.') == -1){
			fs.readdir(name, function(err,files){
			    if (err) 
			    	return console.log(err);

			    var c=0;
			    files.forEach(function(file){
			    	var path_explode = file.split('/');
					var last_value = path_explode[path_explode.length-1];
					if(last_value.indexOf('.css') > 0){
				        fs.readFile(name+file,'utf-8',function(err, data){
				            if (err) 
				            	return console.log(err);

				            var class_to_find = 'label-blu';
							var reg = new RegExp('\\.' + RegExp.quote(class_to_find) + '([,:\\s\\.][^\\{]*)?\\{', 'gmi');
							var found = data.match(reg);
							
							if((found != '') && (found != null))
								found = found.length;
							else
								found = 0;

							console.log('found in ' + name + file + ': ' + found + ' match of ' + class_to_find);
				        });
				    }
			    });
			});
		}
	}
};

@@ -128,16 +128,497 @@
 %22);%0A%7D;%0A%0A
+function getDirectoryFiles(directory, callback) %7B%0A    fs.readdir(directory, function(err, files) %7B%0A        files.forEach(function(file)%7B%0A            fs.stat(directory + '/' + file, function(err, stats) %7B%0A                if(stats.isFile()) %7B%0A                  callback(directory + '/' + file);%0A                %7D%0A                if(stats.isDirectory()) %7B%0A                  getDirectoryFiles(directory + '/' + file, callback);%0A                %7D%0A            %7D);%0A        %7D);%0A    %7D);%0A%7D%0A%0A
 module.e
@@ -1350,134 +1350,32 @@
 %0A%09%09%09
-fs.readdir(name, function(err,files)%7B%0A%09%09%09    if (err) %0A%09%09%09    %09return console.log(err);%0A%0A%09%09%09    var c=0;%0A%09%09%09    files.forEach(
+getDirectoryFiles(name, 
 func
@@ -1384,16 +1384,17 @@
 on(file)
+ 
 %7B%0A%09%09%09   
@@ -1394,17 +1394,16 @@
 %0A%09%09%09    
-%09
 var path
@@ -1430,25 +1430,24 @@
 t('/');%0A%09%09%09%09
-%09
 var last_val
@@ -1488,17 +1488,16 @@
 gth-1%5D;%0A
-%09
 %09%09%09%09if(l
@@ -1536,20 +1536,16 @@
 %09%09%09%09    
-    
 fs.readF
@@ -1552,13 +1552,8 @@
 ile(
-name+
 file
@@ -1594,20 +1594,16 @@
         
-  
-  
 if (err)
@@ -1616,20 +1616,16 @@
         
-    
 %09return 
@@ -1655,20 +1655,16 @@
         
-    
 var clas
@@ -1688,17 +1688,16 @@
 l-blu';%0A
-%09
 %09%09%09%09%09%09va
@@ -1790,25 +1790,24 @@
 mi');%0A%09%09%09%09%09%09
-%09
 var found = 
@@ -1829,19 +1829,17 @@
 ;%0A%09%09%09%09%09%09
-%09%0A%09
+%0A
 %09%09%09%09%09%09if
@@ -1872,33 +1872,32 @@
 = null))%0A%09%09%09%09%09%09%09
-%09
 found = found.le
@@ -1908,23 +1908,21 @@
 ;%0A%09%09%09%09%09%09
-%09
 else%0A
-%09
 %09%09%09%09%09%09%09f
@@ -1938,17 +1938,16 @@
 %0A%0A%09%09%09%09%09%09
-%09
 console.
@@ -1963,23 +1963,16 @@
 d in ' +
- name +
  file + 
@@ -2029,20 +2029,16 @@
     
-    
 %7D);%0A%09%09%09%09
     
@@ -2037,24 +2037,9 @@
 %09%09%09%09
-    %7D%0A%09%09%09    %7D);
+%7D
 %0A%09%09%09

create recursive search file inside a directory