/************************************
Web Command Console
Copyright (C) 2005  Brian Murray
************************************/

function jsBash() {
	this.curcommand = "";
	//this.prompt = "$ ";
	this.history = Array();
	this.histpoint = 0;
	this.carrot = 0;
	this.show = true;
	this.histarea = 'history';
	this.typefield = 'typefield';
	this.Commands = new Array();
	this.ctrlKey = false;
	this.altKey = false;
	this.loadURI = function loadURI() {}
	var me = this;
	//parent.writeCommand("Hello");
this.signal = function signal(sig) {
	if (sig == "SIGKILL")
		this.Commands = null;

}
this.writeCommand = function writeCommand() {
	if (me.show) {
		var type = document.getElementById(me.typefield);
		var buffer = "\r" + me.getEnv("PS2");
		buffer += me.curcommand.substr(0, me.carrot);
		me.print(buffer);
		me.printCodes('<font style="background: #ccc">');
		
		me.print(me.curcommand.substr(me.carrot, 1));
		me.printCodes("</font>");
		buffer = me.curcommand.substr(me.carrot + 1, me.curcommand.length - me.carrot + 1);
		me.print(buffer);
		if (me.carrot >= me.curcommand.length)
			me.printCodes('<font style="background: #ccc">&nbsp;</font>');
			//me.printCodes("<blink>&#9615;</blink>");
		//me.printCodes(buffer);
	}
}
this.controlCode = function controlCode(ctrl, alt) {
	this.ctrlKey = ctrl;
	this.altKey = alt;

}
this.takeCode = function takeCode(code) {
	switch (code) {
		case 0: break;
		case 99: 
			if (this.ctrlKey) {
				me.writePrompt();
				me.clearCommand();
				me.writeCommand();
				break;
			}
		default:
			me.stdin(String.fromCharCode(code));
			me.writeCommand();
	}
}
this.editCode = function editCode(code) {
	switch (code) {
		case 8:
			me.delChar();
			me.writeCommand();
		break;
		case 46:
			me.deleteChar();
			me.writeCommand();
		break;
		case 13:
			me.doCommand();
		break;
		case 37:
			me.cursorLeft();
			me.writeCommand();
			//left
		break;
		case 39:
			me.cursorRight();
			me.writeCommand();
			//right
		break;
		case 38:
			me.cursorUp();
			me.writeCommand();
			//right
		break;
		case 40:
			me.cursorDown();
			me.writeCommand();
			//right
		break;
		case 36:
			//home
			me.cursorHome();
			me.writeCommand();
		break;
		case 35:
			//end
			me.cursorEnd();
			me.writeCommand();
		break;
		case 9:
			me.tabComplete();
			me.writeCommand();
			//right
		break;
		//default: alert(code);
	}
}
this.tabComplete = function tabComplete() {
	if (me.show) {
	var options = me.getCommands();
	var diroptions = me.getDirCommands();
	var old = me.curcommand;
	var find = "";
	var num = 0;
	options[options.length] = "exit";
	options[options.length] = "env";
	options[options.length] = "export";
	options[options.length] = "pwd";
	options[options.length] = "cd";
	options[options.length] = "clear";
	options[options.length] = "fg";
	options[options.length] = "ls";
	for (var i in diroptions) options[options.length] = "./" + diroptions[i];
	options.sort();
	for (var i in options) {
		if (options[i].indexOf(old) == 0) {
			if (num == 1) {
				var car = me.carrot;
				me.writePrompt();
				me.carrot = car;
				me.print(find + "\n");
			}
			num++;
			find = options[i];
			if (num > 1)
				me.print(options[i] + "\n");
		}
	}
	if (num == 1) {
		me.curcommand = find + " ";
		me.carrot = find.length + 1;
	} else me.curcommand = old;
	me.writeCommand();
	}
}
this.writePrompt = function writePrompt() {
	//me.print("\r" + me.prompt + me.curcommand + "\n");
	me.print("\r" + me.getEnv("PS2") + me.curcommand + "\n");
	me.clearCommand();
}
this.doCommand = function doCommand() {
	var buffer = "";
	//writeHistory(prompt + curcommand + "\n");
	var command = me.curcommand.split(' ');
	me.history[me.history.length] = me.curcommand;
	me.histpoint = 0;
	me.writePrompt();
	switch (command[0]) {
		case "exit":
		me.print("\r");
		me.exit();
		break;
		case "env":
			if (command[1])
			me.print(me.getEnv(command[1]) + "\n")
			else
			me.print("Incorrect Args\n");
			me.writeCommand();
		break;
		case "export":
			if (command[2]) 
				me.setEnv(command[1], command[2]);
			me.writeCommand();
		break;
		case "pwd":
			me.print(me.getEnv("cwd") + "\n");
			me.writeCommand();
		break;
		case "cd":
			me.cd(command[1]);
			me.writeCommand();
		break;
		case "clear":
			me.clearHistory();
			me.writeCommand();
		break;
		case "ls":
			var dirlist = me.getDirs();
			dirlist.sort();
			for (var i in dirlist) {
				me.printCodes("<b>"); 
				me.print(dirlist[i]);
				me.printCodes("</b>\n");
			}
			dirlist = me.getDirCommands();
			dirlist.sort();
			for (var i in dirlist) me.print(dirlist[i] + "\n");
			me.writeCommand();

		break;
		case "fg":
			if (!(command[1] && me.chprocess(command[1]))) {
				me.print("Cannot fork to " + command[1] + "\n");
				me.writeCommand();
			}
		break;
		default:
		if (command[0] == "") me.writeCommand();
		else {
			var ret = me.exec(command);
			if (!ret) {
				me.print("Whoops! Unknown command.\n");
				me.writeCommand();
			}
		}
	}
	//alert(command[0]);
}
this.stdin = function stdin(c) {
	var hold = me.curcommand;
	me.curcommand = hold.substr(0, me.carrot);
	me.curcommand += c;
	me.curcommand += hold.substr(me.carrot, hold.length - me.carrot);
	me.carrot++;
	me.writeCommand();
}
this.delChar = function delChar() {
	var hold = me.curcommand;
	me.curcommand = hold.substr(0, me.carrot - 1);
	me.curcommand += hold.substr(me.carrot, hold.length - me.carrot);
	if (me.carrot > 0)
		me.carrot--;
}
this.deleteChar = function deleteChar() {
	var hold = me.curcommand;
	me.curcommand = hold.substr(0, me.carrot);
	me.curcommand += hold.substr(me.carrot + 1, hold.length - me.carrot);
}
this.clearCommand = function clearCommand() {
	me.curcommand = "";
	me.carrot = 0;
}
this.cursorLeft = function cursorLeft() {
	if (me.carrot > 0)
		me.carrot--;
}
this.cursorRight = function cursorRight() {
	if (me.carrot < me.curcommand.length)
		me.carrot++;
}
this.cursorUp = function cursorUp() {
	me.histpoint++;
	if ( me.histpoint > me.history.length ) me.histpoint = 0;
	if (me.histpoint == 0) 
		me.curcommand = "";
	else
		me.curcommand = me.history[me.history.length - me.histpoint];
	me.carrot = me.curcommand.length;

}
this.cursorDown = function cursorDown() {
	me.histpoint--;
	if ( me.histpoint < 0) me.histpoint = me.history.length;
	if (me.histpoint == 0) 
		me.curcommand = "";
	else
		me.curcommand = me.history[me.history.length - me.histpoint];
	me.carrot = me.curcommand.length;

}
this.cursorEnd = function cursorEnd() {
	me.carrot = me.curcommand.length;
}
this.cursorHome = function cursorHome() {
	me.carrot = 0;
}
this.main = function main() {
	if (!this.getEnv("PS2"))
	this.setEnv("PS2", "$ ");
	this.writeCommand();
	return 1;
}

}





