var fs = require("fs"),
	scheduler = require("node-schedule"),
	urlParser = require("url"),
	cheerio = require("cheerio"),
	express = require("express"),
	app = express(),
	http = require('http'),
	server = http.createServer(app),
	io = require('socket.io').listen(server);

process.chdir(__dirname);

config = JSON.parse(fs.readFileSync("config.json"));

app.use(express.static("frontend"));
app.use(express.bodyParser());

app.use("/uploads", express.static("uploads"));

var log = new Array();

var rule = new scheduler.RecurrenceRule();
rule.hour = 23;
rule.minute = 0;

var resetJob = scheduler.scheduleJob(rule, function()
{
	log = new Array();
	console.log("Reset links");
});

function saveAndPushLink(link)
{
	log.push(link);
	io.sockets.emit("link", link);
}


app.post("/uploadHandler", function(req,res)
{
	uploadedFile = req.files.file;
	if ( !uploadedFile )
		res.send("You didn't send a file!");
		
	fs.readFile(uploadedFile.path, function(err, data)
	{
		var fsName = (new Date().getTime())+uploadedFile.name;
		var fTitle = req.param("title");
		if ( !fTitle )
			fTitle = uploadedFile.name;
		console.log(data);
		fs.writeFile("uploads/"+fsName, data, function(err)
		{
			f = {type:"file", username: req.param("username"), fileName: uploadedFile.name, fsFileName: fsName, title: fTitle, room: req.param("room")};
			log.push(f);
			io.sockets.emit("file", f);
		});
	});
	
	res.send("OK");
});

io.on("connection", function(socket)
{
	socket.on("resync", function(data)
	{
		for( var i = 0; i < log.length; i++ )
		{
			socket.emit(log[i].type, log[i]);
		}
	});
	
	socket.on("link", function(data)
	{
		data.type = "link";
		data.linkTitle = data.url;
		if ( config.autoTitle && data.url.split(":")[0] == "http" )
		{
			url = urlParser.parse(data.url);
			var options = {
				host: url.hostname,
				path: url.pathname,
				port: 80,
				method: "GET",
				headers: {
					"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36"
				}
			};
			
			http.get(options, function(res) {
				var html = "";
				res.on("data", function(chunk)
				{
					html += chunk;
				});	
	
				res.on("end", function()
				{			
					$ = cheerio.load(html);
					title = $("title").html();
					if ( title )
						data.linkTitle = title;
					saveAndPushLink(data);
				});
			}).on('error', function(e) {
				saveAndPushLink(data);
			});
		}
		else
			saveAndPushLink(data);
	});	
});


server.listen(80);
if ( config.changeUser )
{
	process.setuid(config.user);
	process.setgid(config.group);
}

@@ -2524,17 +2524,17 @@
 cess.set
-u
+g
 id(confi
@@ -2535,20 +2535,21 @@
 (config.
-user
+group
 );%0A%09proc
@@ -2547,33 +2547,33 @@
 p);%0A%09process.set
-g
+u
 id(config.group)
@@ -2566,18 +2566,17 @@
 (config.
-group
+user
 );%0A%7D%0A

Change gid before uid