'use strict'

function Boss(root, name){

    var boss = this;

    var loadFileSystem = root => {
        this.FileSystem = eval(root.dirs.sys.files.FileSystem.data);
    }

    var loadEnv = fs => {
        var files = this.fs.get_files('/etc');
        for(var file in files){
            var json = this.fs.get_file('/etc',files[file]).data;
            this.env[files[file]] = JSON.parse(json);
        }
    }

    var loadLibraries = fs => {
        var files = this.fs.get_files('/lib');
        for(var file in files){
            var code = this.fs.get_file('/lib',files[file]).data;
            this.lib[files[file]] = eval(code);
        }
    }

    var loadCommands = fs => {
        var help = [];
        var files = this.fs.get_files('/bin');
        for(var file in files){
            var code = this.fs.get_file('/bin',files[file]).data;
            this.cmd[files[file]] = eval(code);
            if(this.fs.get_file('/bin',files[file]).meta && this.fs.get_file('/bin',files[file]).meta.description){
                help.push(files[file] + ' - ' + this.fs.get_file('/bin',files[file]).meta.description);
            }
        }
        this.cmd['help'] = function(args, client){
            boss.lib.print.log('\n', client);
            for(var opt in help){
                boss.lib.print.log(help[opt], client);
            }
            boss.lib.print.log('\n', client);
        }
    }

    this.reload = function(){
        this.cmd = {};
        this.lib = {};
        this.env = {};
        loadEnv(this.fs);
        loadLibraries(this.fs);
        loadCommands(this.fs);
    }

    loadFileSystem(root);
    this.fs = new this.FileSystem(root, name);
    this.reload();
    this.interpreters = [new this.lib.Login(this).username];
}

var ConsoleClient = function(b){
    var daBoss = b;

    this.toolbar = {
        set_title: function(title){
            //nothing
        },
         set_message: function(message){
            //nothing
        },
         set_notification: function(app){
            //nothing
        }
    }

    this.out = function(text){
        console.log(text);
    }

    this.set_prompt = function(prompt){
        //Do nothing
    }

    this.get_prompt = function(){
        return '';
    }

    this.exec = (command) => {
        daBoss.interpreters[daBoss.interpreters.length - 1](command, this);
    }
}


@@ -172,250 +172,8 @@
  %7D%0A%0A
-    var loadEnv = fs =%3E %7B%0A        var files = this.fs.get_files('/etc');%0A        for(var file in files)%7B%0A            var json = this.fs.get_file('/etc',files%5Bfile%5D).data;%0A            this.env%5Bfiles%5Bfile%5D%5D = JSON.parse(json);%0A        %7D%0A    %7D%0A%0A
     
@@ -1172,61 +1172,243 @@
     
-this.reload = function()%7B%0A        this.cmd = %7B%7D;
+var runInit = fs =%3E %7B%0A        var files = this.fs.get_files('/etc');%0A        for(var file in files)%7B%0A            var code = this.fs.get_file('/etc',files%5Bfile%5D).data;%0A            var fn = eval(code);%0A            fn();%0A        %7D
 %0A    
+%7D%0A%0A
     
@@ -1416,17 +1416,28 @@
 his.
-lib = %7B%7D;
+reload = function()%7B
 %0A   
@@ -1450,11 +1450,11 @@
 his.
-env
+cmd
  = %7B
@@ -1468,24 +1468,21 @@
     
-loadEnv(this.fs)
+this.lib = %7B%7D
 ;%0A  
@@ -1538,24 +1538,43 @@
 s(this.fs);%0A
+        runInit();%0A
     %7D%0A%0A    l

add init