-- A simple interpreter to run commands with in a Lua environment.


-- Pulls the raw information out of a file, and into a string.
local function ReadFileRaw(inval)
	local infile
	if IsStringToBool(inval) then
		infile = io.open(inval,"rb")
	elseif IsFileToBool(inval) then
		infile = inval
	end
	if infile ~= nil then
		local instr = infile:read("*all")
		if instr ~= nil then
			return instr
		end
	end
end

local function OSExecuteToString(instr)
	local infile = io.popen(instr,"rb")
	local instr
	while instr == nil and instr ~= "" do
		instr = infile:read()
	end
	infile:close()
	return instr
end

local function UnixReadRaw()
	return OSExecuteToString('stty raw ; read -s -r -n 1 input ; echo -n $input ; stty cooked',"rb")
end

local ReadRaw = UnixReadRaw

local function SeparateStringBySeparator(separator,reststr,instr)
	instr = MatchReplace(instr,"")
	if reststr ~= "" then
		local results = {}
		if string.sub(reststr,1,1) == separator then
			if instr ~= "" then
				results = RecursiveTableAdd(results,instr)
				instr = ""
			end
		else
			instr = instr .. string.sub(reststr,1,1)
		end
		results = RecursiveTableAdd(results,SeparateStringBySeparator(separator,string.sub(reststr,2,string.len(reststr)),instr))
		return TableUnpack(results)
	else
		return instr
	end
end

local function UnixConsoleSize()
	local width,height = SeparateStringBySeparator(" ",OSExecuteToString("ttysize"))
	return tonumber(width),tonumber(height)
end


local ModuleString = ReadFileRaw("ModulesOnly.lua")

Load = MatchReplace(loadstring,load)

InterpreterPrompt = Select(IsTableToBool(InterpreterPrompt),InterpreterPrompt,{ExecuteFromTemplate,InputAskTemplate," ?"," "})

local function Interpreter(inval,exec)
	while string.lower(tostring(inval)) ~= "exit" do
		if type(exec) == "function" then
			local result = {exec()}
			if result[1] ~= nil or result[2] ~= nil then
				OSWrite(TableUnpack(result))
			end
		elseif string.lower(tostring(inval)) ~= 'exit' and inval ~= nil then
			if _G[inval] ~= nil then
				OSWrite((ValueToString(_G[inval])),"\n")
			else
				OSWrite("-..- invalid Lua function")
			end
		end

		inval = ExecuteTable(InterpreterPrompt)
		if inval ~= nil then
			exec = Load(ModuleString .. "\n" .. tostring(inval))
		else
			OSWrite('\n')
			inval = 'exit'
		end
		if exec == nil and inval ~= nil then exec = Load(ModuleString .. "\n return ValueToString(" .. tostring(inval) .. ")") end
	end
end

local InputMap = {}

local function MapKeys()
	local inkey
	repeat
		if inkey ~= nil then
			InputMap[inkey] = ExecuteFromTemplate(InputAskTemplate,"Map To?")
		end
		io.write("Press a key: ")
		inkey = ReadRaw()
		print("Key pressed - Real: " .. tostring(inkey) .. " - Mapped: " .. Select(InputMap[inkey],"<" .. tostring(InputMap[inkey]) .. ">","None"))
	until InputMap[inkey] == "Finished"
end

--print("This console is " .. ConcatenateValuesWithSeparator(" lines by ",SwitchVariables(UnixConsoleSize())).. " columns.")

Greeneye = MatchReplace(Greeneye,"\x1b]P00C1111\x1b]P1432022\x1b]P2425D4F\x1b]P3715949\x1b]P41E2838\x1b]P55D353A\x1b]P64F7475\x1b]P76B9B7B\x1b]P8595C51\x1b]P9A1615A\x1b]PA6B917E\x1b]PBACA285\x1b]PC425C7B\x1b]PD9E8095\x1b]PE79A7A8\x1b]PFCBCBCB\x1b[0;32m")
OSWrite(Greeneye)
WelcomeMessage = MatchReplace(WelcomeMessage,"Welcome to Idiot/Linux! There are no safety rails.     _____________________\n                                                      |    NO      RAILS    |\n     If you encounter problems, please consult        |                     |\n        the Linux kernel documentation, your          |   =======X=======   |\n         sadistic system administrator , or           |                     |\n              your nearest tissue box.                `---------------------'\n\n  Brought to you by Lua: Stumbling drunkenly all over your\n                                  userspace needs since\n                                 roughly five minutes ago.\n\n\n\n\n\n\n\n\n\n\n\n\n\n")
print(WelcomeMessage)

Interpreter()

--MapKeys()
