var EXPRESS=require("EXPRESS"),
	PATH=require("path"),
	FS=require("fs"),
	CLUSTER=require("cluster"),
	Q=require("q"),
	HDMA=require("./nodejs/api/hdma"),
	CONFIG=require("./nodejs/config"),
	LOGGER=require("./nodejs/config/logger.js"),
	NUMCPU=2, //require("os").cpus().length,
	models={
		geoviewer: null
	},
	domain="",
	domainFolder="",
	MONGODB=null,
	port=(CONFIG&&CONFIG.server)?CONFIG.server.port:8080,
	app=null,
	server=null,
	io=null,
	argv=require("minimist")(process.argv.slice(2));


//check arguemnts in the command line
var value;
for(var k in argv){
	value=argv[k]
	//port
	if(k=='p' && value && value!=''){
		port=value;
	}
}







/**
//cluster
if(CLUSTER.isMaster){
	for(var i=0;i<NUMCPU;i++){
		CLUSTER.fork();
	}

	Q.all([HDMA.mongodb.connect("localhost:27017", "HDMA"), HDMA.mongodb.connect("localhost:27017", "IBSS")]).then(function(results){
		console.log(results);
		//console.log(b);
	}).catch(function(err){
		console.log("error", err)
	})



	//init();
}else{
	//init();
}

// Listen for dying workers
CLUSTER.on('exit', function (worker) {
    // Replace the dead worker,
    // we're not sentimental
    console.log('Worker ' + worker.id + ' died :(');
    CLUSTER.fork();
});

*/
init();



//init server
function 
(){
	app=EXPRESS();
	server=app.listen(port);
	io=require("socket.io").listen(server, {resource:"/socket/socket.io", log:false});//, transports:["xhr-polling"]});  //because we are using iis7 as the main web server which does not support websocket. we need to change to long-polling for socket. please refer to http://schmod.ruhoh.com/windows/socket-io-and-iis/

	//winston.addColors({debug: 'green',info:  'cyan',silly: 'magenta',warn:  'yellow',error: 'red'})
	LOGGER.info("Server is started and listened on port "+port);



	//config----------------------------------------------------------------------
	//log
	var logFile = FS.createWriteStream('./log/server.log', {flags: 'a'}); //use {flags: 'w'} to open in write mode
	app.use(EXPRESS.logger({stream: logFile}))

	//gzip conpress method
	app.use(EXPRESS.compress())
	//parse the post data of the body
	app.use(EXPRESS.bodyParser());

	//render engine
	app.set('views', __dirname+"/views/");
	app.set('view engine', 'jade');

	//jsonp
	app.set("jsonp callback", true);

	//maxlistener
	//app.setMaxListeners(0);
	//io.setMaxListeners(0);


	//passport required config
	//app.use(EXPRESS.static("public"))
	app.use(EXPRESS.cookieParser());
	app.use(EXPRESS.session({secret:'hdma@SDSU'})); // session secret
	app.use(app.router)
	//-------------------------------------------------------------------------------



	models.geoviewer=require("./nodejs/geoviewer")({mongodb:null, io:io, router:app});



	/***********************************************************************************
	 *  RESTful
	 *********************************************************************************/
	//if the domain is not equal to domainFolder, we may need to manually set up the path!!!
	app.use("/geoviewer", EXPRESS.static(PATH.join(__dirname, "/public/geoviewer/final")));
	app.use("/common", EXPRESS.static(PATH.join(__dirname, "/public/common")));

	LOGGER.info("******************************")
	LOGGER.info("Server Routes inited!")

}

init