function rr(name) {
    console.log("RRR:", name)

    return require(name);



}

var util = require("util");



var config = {};

try {
	//running in docker / cloudron
	util._extend(config, require("/app/data/config.json"));


}catch(e){

	console.log("no config present")	

}



console.log("CONFIG",config);



 

    var express = require("express");

    var app = require('express')();
    var myserver = require('http').Server(app);

    var io = require('socket.io')(myserver);


	var chat = io
	  .of('/chat')
	  .on('connection', function (socket) {
	    socket.emit('a message', {
	        that: 'only'
	      , '/chat': 'will get'
	    });
	    chat.emit('a message', {
	        everyone: 'in'
	      , '/chat': 'will get'
	    });
	  });

	var news = io
	  .of('/news')
	  .on('connection', function (socket) {
	  	console.log("KLLK");
	    socket.on("woot",function(){
		  	console.log("SSSSS",arguments);
		  })


	    socket.emit('item', { news: 'item' });


	  });



if(config.peers){
	setTimeout(function(){

	var lives = config.peers.map(function(peer){

	var con = require("socket.io-client")('http://'+peer.ip+':'+peer.port+"/chat");
	
	con.on('connect', function () {
    	chat.emit('hi!');
	    console.log("CHATE",arguments);

	  }).on("a message",function(message){

		  	console.log("cccARG",message);

	  });
  

		return {peer:peer,con: con }
	})


	  console.log(lives)


	},5000)

/*

other_server.on("connect",function(){
    other_server.on('message',function(data){
        // We received a message from Server 2
        // We are going to forward/broadcast that message to the "Lobby" room
        console.log("LOG GOT MESSAGE");
 		chat.emit("a message",data);       
    });
});

*/	
}






function tester(){

	function newsa(){
   		news.emit("item",{"title":"titlllklkl","date":new Date(),"body":"jljlkjl"})
		setTimeout(newsa,Math.floor(Math.random()*100)*1000)		
	}

	function chata(){
   		chat.emit("a message",{"user":"u"+Math.random(),"date":new Date(),"body":"jljlkjl"})
		setTimeout(chata,Math.floor(Math.random()*20)*1000)		
	}

		setTimeout(newsa,Math.floor(Math.random()*100)*1000)		
		setTimeout(chata,Math.floor(Math.random()*20)*1000)		



}
tester();









    app.use(express.static("/app/data"));



    app.get("/", function(req, res, next) {

        try {
            var a = require("." + req.url)(req, res, next);

        } catch (e) {

            res.send(e);

        }





    })



    var http = require('http'),
        formidable = require('formidable'),
        fs = require('fs'),
        path = require('path');


    // All your express server code goes here.
    // ...

    // Upload route.
    app.post('/upload', function(req, res) {
        var form = new formidable.IncomingForm();
        form.multiples = true;
        form.parse(req, function(err, fields, afiles) {
            // `file` is the name of the <input> field of type `file`
            console.log(afiles, fields)

            var ffa = afiles.file[0];

            var old_path = ffa.path,
                file_size = ffa.size,
                file_ext = ffa.name.split('.').pop(),
                index = old_path.lastIndexOf('/') + 1,
                file_name = old_path.substr(index),
                new_path = path.join("/app/data", '/uploads/', file_name + '.' + file_ext);

            fs.readFile(old_path, function(err, data) {
                fs.writeFile(new_path, data, function(err) {
                    fs.unlink(old_path, function(err) {
                        if (err) {
                            res.status(500);
                            res.json({
                                'success': false
                            });
                        } else {
                            res.status(200);
                            res.json({
                                'success': true
                            });
                        }
                    });
                });
            });
        });
    });







    myserver.listen(3000);


 