console.log("HERE reset on load");

//var http=require("./xdd");


//console.log(http);



/* 
	MA DE LA PA DA BA 
	

	/
		


	/
	




*/
var fs = require("fs");
/*
fs.exists(PATH+"/emsa",function(present){
	if(present){
		var files = fs.readdirSync(PATH+"/emsa/");
		console.log("file:", files);

	}else{

		console.log("no emsa folder")
	}

	var files = fs.readdirSync(PATH);
	console.log("files:", files);	

})
*/
var ppath = require("path");

$lib=typeof($lib)==="undefined"?{}:$lib;


function mkbitfield() {
    var Container = typeof Buffer !== "undefined" ? Buffer //in node, use buffers
        : typeof Int8Array !== "undefined" ? Int8Array //in newer browsers, use webgl int8arrays
        : function(l) {
            var a = new Array(l);
            for (var i = 0; i < l; i++) a[i] = 0;
        }; //else, do something similar

    function BitField(data, opts) {
        if (!(this instanceof BitField)) {
            return new BitField(data, opts);
        }

        if (arguments.length === 0) {
            data = 0;
        }

        this.grow = opts && (isFinite(opts.grow) && getByteSize(opts.grow) || opts.grow) || 0;

        if (typeof data === "number" || data === undefined) {
            data = new Container(getByteSize(data));
            if (data.fill && !data._isBuffer) data.fill(0); // clear node buffers of garbage
        }
        this.buffer = data;
    }

    function getByteSize(num) {
        var out = num >> 3;
        if (num % 8 !== 0) out++;
        return out;
    }

    BitField.prototype.get = function(i) {
        var j = i >> 3;
        return (j < this.buffer.length) &&
            !!(this.buffer[j] & (128 >> (i % 8)));
    };

    BitField.prototype.set = function(i, b) {
        var j = i >> 3;
        if (b || arguments.length === 1) {
            if (this.buffer.length < j + 1) this._grow(Math.max(j + 1, Math.min(2 * this.buffer.length, this.grow)));
            // Set
            this.buffer[j] |= 128 >> (i % 8);
        } else if (j < this.buffer.length) {
            /// Clear
            this.buffer[j] &= ~(128 >> (i % 8));
        }
    };

    BitField.prototype._grow = function(length) {
        if (this.buffer.length < length && length <= this.grow) {
            var newBuffer = new Container(length);
            if (newBuffer.fill) newBuffer.fill(0);
            if (this.buffer.copy) this.buffer.copy(newBuffer, 0);
            else {
                for (var i = 0; i < this.buffer.length; i++) {
                    newBuffer[i] = this.buffer[i];
                }
            }
            this.buffer = newBuffer;
        }
    };
    return BitField
}


var bf=mkbitfield();

var t= bf(512)

console.log(t);
t.set(128,1);
t.set(228,1);
console.log(t);
for(var i=0;i<512;i++){
	t.set(i,Math.random()>0.3?1:0)
}
console.log(t);


$lib.deepfind = function deepfind(path, cb) {
    var origpath = ppath.resolve(path);
    var paths = [origpath];
    var crawled = [];
    var dons = [];
    var index = 0;

    function step() {
        var pa = paths.shift();

        var allfiles = fs.readdirSync(pa).map(function(file) {
            var ffa = {
                file: file,
                stat: null
            }
            try {
                ffa.stat = fs.statSync(pa + "/" + file)
            } catch (e) {
                ffa.stat = null;
            }
            return ffa
        });

        var folders = allfiles.filter(function(file) {
            return file.stat && file.stat.isDirectory();
        })
        var files = allfiles.filter(function(file) {
            return file.stat && file.stat.isFile();
        })

        folders.map(function(file) {
            paths.push(ppath.join(pa, file.file))
        })

        files.map(function(file) {
            dons.push(ppath.join(pa, file.file))
        })

        crawled.push(pa);

        if (paths.length) {
            setTimeout(step, 20);
        } else {
            dons = dons.map(function(file) {
                return file.replace(origpath, "").substring(1)
            });
            cb(dons);
        }
    }

    function status() {
        return {
            paths: paths.length,
            files: dons.length,
            crawled: crawled.length
        }
    }

    step();

    return status;
}



$lib.test = function() {


    return $lib.deepfind("./", function(files) {


        console.log("..", files.length);


    })



}

$lib.tt = $lib.test();




/*
	virkelig simple flat fil cms
	med tools
		export til [ sitea, siteb .. ]

		
		flade filer
	
		bruger opretelse
			emsaconfig.json {
							endpoints: []
			}

			emsa/
					users/jannick/
									autoauth.txt


					pages/
						  page1/
						  			somedatadata
			
						

*/