/*
 * LICENCE[[
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1/CeCILL 2.O 
 *
 * The contents of this file are subject to the Mozilla Public License Version 
 * 1.1 (the "License"); you may not use this file except in compliance with 
 * the License. You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, 
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
 * for the specific language governing rights and limitations under the 
 * License. 
 * 
 * The Original Code is utc.fr code. 
 * 
 * The Initial Developer of the Original Code is 
 * david.rivron@utc.fr
 * 
 * Portions created by the Initial Developer are Copyright (C) 2012
 * the Initial Developer. All Rights Reserved. 
 * 
 * Contributor(s): 
 * 
 * 
 * Alternatively, the contents of this file may be used under the terms of 
 * either of the GNU General Public License Version 2 or later (the "GPL"), 
 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), 
 * or the CeCILL Licence Version 2.0 (http://www.cecill.info/licences.en.html), 
 * in which case the provisions of the GPL, the LGPL or the CeCILL are applicable 
 * instead of those above. If you wish to allow use of your version of this file 
 * only under the terms of either the GPL or the LGPL, and not to allow others 
 * to use your version of this file under the terms of the MPL, indicate your 
 * decision by deleting the provisions above and replace them with the notice 
 * and other provisions required by the GPL or the LGPL. If you do not delete 
 * the provisions above, a recipient may use your version of this file under 
 * the terms of any one of the MPL, the GPL, the LGPL or the CeCILL.
 * ]]LICENCE
 */
 
var CollabServer = require('./CollabServer.js'),
	UriMessagesHandler = require('./UriMessagesHandler.js'),
	RequestMessagesHandler = require('./RequestMessagesHandler.js'),
	DialogMessagesHandler = require('./DialogMessagesHandler.js');

/*
 * Lectures des paramètres de la ligne de commande
 */
function parseArguments () {
	var vParams = {};
	try {
		for (var vParamsIndex=0; vParamsIndex<process.argv.length && process.argv[vParamsIndex].indexOf('--') != 0; vParamsIndex++);
		for (var i=vParamsIndex; i<process.argv.length; i+=2) {
			var vParamName = process.argv[i].substring(2);
			vParams[vParamName] = process.argv[i+1];
		}
		
		var vIntParamNames = [ 'port', 'systemClientPort', 'systemServerPort' ];
		vIntParamNames.forEach(function(vParamName) {
			vParams[vParamName] = vParams[vParamName] !== undefined ? parseInt(vParams[vParamName]) : NaN;
		});
		
		var vBoolParamNames = [ 'log' ];
		vBoolParamNames.forEach(function(vParamName) {
			vParams[vParamName] = vParams[vParamName] == "true";
		});
	} catch (e) {
		usage();
		process.exit(1);
	}
	
	if (!vParams.port || (!vParams.systemClientPort && (!vParams.systemServerPort))) {
		usage();
		process.exit(1);
	}
	
	return vParams;
}

function usage() {
	console.info('Usage: node liveCollabServer.js --port <int> ( --systemServerPort <int> | --systemClientPort <int> ) [ --connectUrl <ws://host:port/path/> ] [ --systemConnectUrl <url> ] [ --logDebug <bool> ]');
	console.info("  --port\t\tPort d'écoute du serveur [obligatoire]");
	console.info("  --systemServerPort\tPort de communication avec le serveur Java [obligatoire]");
	console.info("  --systemClientPort\tPort de communication avec le serveur Java lorsque liveCollab est esclave [utilisé uniquement par le serveur Java]");
	console.info("  --connectUrl\t\tURL publique de connexion au serveur");
	console.info("  --systemConnectUrl\tURL vers le code action Reconnect du service SvcLiveCollab du serveur Java, permet une reconnection automatique des deux serveurs au démarrage");
	console.info("  --logPath\t\tChemin vers un fichier de log, par défaut les messages sont affichés sur la sortie standard");
	console.info("  --logDebug\t\tActivation les messages de debug");
	console.info("\t");
}

var vCollabServer = new CollabServer(parseArguments());
new UriMessagesHandler(vCollabServer);
new RequestMessagesHandler(vCollabServer);
new DialogMessagesHandler(vCollabServer);
