/*
	
	IRC-Bot "Kvasir"
    Copyright (C) 2016	Fenris Wolf (fenris@folksprak.org)
	
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
	
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
	
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
       
*/

declare var process;


/**
 * @author fenris
 */
function cxefajxo(argumentoj : Array<string>) : void
{
	function malkonfesado() : void
	{
		console.info("Kvasir  Copyright (C) 2016  Fenris Wolf");
		console.info("This program comes with ABSOLUTELY NO WARRANTY.");
		console.info("This is free software, and you are welcome to redistribute it");
		console.info("under certain conditions.");
	}
	
	function sekurigi() : void
	{
		process.on
		(
			"uncaughtException",
			function (event : Event) : void
			{
				console.warn("malloka escepto: ", event);
			}
		);
	}
	
	function lancxi(agordoj : Object, fino : ()=>void = function () {}) : void
	{
		let kliento : klaso_kliento = new klaso_kliento_irc(agordoj, agordoj["prokrasto"]);
		kliento.stirado_arangxi(agordoj["administrantoj"]);
		vokado_atendi
		(
			agordoj["kromajxoj"].map
			(
				function (ero : Object) : (preta : ()=>void)=>void
				{
					return (
						function (preta : ()=>void) : void
						{
							console.log("-- sxarganta na '" + ero["vojo"] + "' ...");
							klaso_kromajxo_ekstera.krei
							(
								ero["vojo"],
								function (eraro : Error, kromajxo : klaso_kromajxo_ekstera) : void
								{
									if (eraro != null)
									{
										console.warn(eraro);
									}
									else
									{
										kliento.kromajxo_aldoni
										(
											kromajxo,
											ero["aktiva"],
											undefined,
											function (eraro : Error) : void
											{
												if (eraro != null)
 												{
 													throw (new Error("-- la kromajxo '" + ero["vojo"] + "' ne sxargeblas: " + String(eraro)));
												}
												else
												{
													preta();
												}
											}
										);
									}
									// console.log("-- ... preta");
									// preta();
								}
							);
						}
					);
				}
			),
			function () : void
			{
				console.log("-- konektanta ...");
				kliento.konekti
				(
					{},
					function () : void
					{
						console.log("-- konektita");
						agordoj["auxtolancxo"].forEach(x => kliento.enigi(new klaso_evento_eniga_mesagxo(null, null, x)));
						fino();
					}
				);
			}
		);
	}
	
	function agordoj_legi(vojo : string, uzado : (agordoj : Object)=>void) : void
	{
		_fs.readFile
		(
			vojo,
			{"encoding": "utf8", "flag": "r"},
			function (eraro : any, enhavo : string) : void
			{
				if (eraro != undefined)
				{
					throw (new Error("agordoj ne enlegeblas: " + eraro.toString()));
				}
				else
				{
					let agordoj : Object;
					try
					{
						agordoj = JSON.parse(enhavo);
					}
					catch (escpto)
					{
						throw (new Error("agordoj ne enlegeblas: " + escpto.toString()));
						agordoj = null;
					}
					if (agordoj != null)
					{
						uzado(agordoj);
					}
				}
			}
		);
	}
	
	console.error(argumentoj);
	malkonfesado();
	sekurigi();
	let vojo : string = ((argumentoj.length > 0) ? argumentoj.shift() : "agordoj.json");
	agordoj_legi(vojo, agordoj => lancxi(agordoj));
}

cxefajxo(process.argv.slice(2));