-- minux main api's
-- recursive listing
function lsr(searchpath)
	expect(1, searchpath, "string")
	os.run({}, "/etc/minux-main/ls/ls.sys" , searchpath)
end

-- login system
function logintype()
	if fs.exists("/etc/auth.cfg") then
		file = fs.open("/etc/auth.cfg" , "r")
		authtype = file.readLine()
		file.close()
	else
		authtype = "disabled"
	end
	return authtype
end

function login(login, password)
	expect(1, login, "string")
	expect(2, password, "string")
	os.run({}, "/bin/login.sh" , login , password)
end

function lock()
	if logintype() ~= "disabled" then
		term.clear()
		term.setCursorPos(1,1)
		print("Minux V:".._G.version)
		print(" ")
		print("This system is currently locked by user:".._G.login)
		print("type the password to unlock this system")
		print("if you already unlocked it, hit enter")
		print(" ")
		_G.lockactive = true
			while _G.lockactive == true do
			write("password:")
			input = read("*")
			if _G.lockactive == false then return 0 end
			if input == _G.masterpass then _G.lockactive = false
			else print("Wrong password") end
		end
	else
		print("cannot enable lock with users disabled")
		print("hit enter to continue")
		read()
	end
end

-- sanity check
function sanitycheck()
	if logintype() == "network" and auth == nil then
		apt.install("auth-client")
		apt.install("netlib")
		apt.aliasbuild()
		apt.bootbuild()
		minux.restart()
	end
	if logintype() == "network" and netlib == nil then
		apt.install("netlib")
		apt.aliasbuild()
		apt.bootbuild()
		minux.restart()
	end
	if _G.version == "cleaninstall" then
		apt.update()
		apt.aliasbuild()
		apt.bootbuild()
		minux.restart()
	end
	if fs.exists("/usr/minux-main/resetconfig") == false then
		local tempfile = fs.open("/usr/minux-main/resetconfig","w")
		tempfile.writeLine("delete this file to reset the system config")
		tempfile.close()
		term.clear()
		term.setCursorPos(1,1)
		os.run({},"/etc/minux-main/menu/login.sys")
		os.run({},"/etc/minux-main/menu/startup.sys")
		minux.restart()
	end
end

-- minux monitorprint
function monitorprint(printstring)
	expect(1, printstring, "string")
	os.run({}, "/etc/minux-main/sys/monitorprint.sys" , printstring)
end
-- minux configuration
function getconfig(value)
	expect(1, value, "string")
	if value ~= nil then
		local tempdata = nil
		if value == "login" then
			if fs.exists("/etc/auth.cfg") == true then
				local tempfile = fs.open("/etc/auth.cfg","r")
				tempdata = tempfile.readLine()
				tempfile.close()
			else
				tempdata = "disabled"
				local tempfile = fs.open("/etc/auth.cfg","w")
				tempfile.writeLine(tempdata)
				tempfile.close()
			end
			return tempdata
		elseif value == "ui" then
			if fs.exists("/usr/minux-main/menu.cfg") == true then
				local tempfile = fs.open("/usr/minux-main/menu.cfg","r")
				tempdata = tempfile.readLine()
				tempfile.close()
			else
				tempdata = "menu"
				local tempfile = fs.open("/usr/minux-main/menu.cfg","w")
				tempfile.writeLine(tempdata)
				tempfile.close()
			end
			return tempdata
		elseif value == "debug" then
			if fs.exists("/usr/minux-main/debug.cfg") then
				local tempfile = fs.open("/usr/minux-main/debug.cfg","r")
				tempdata = tempfile.readLine()
				tempfile.close()
				return tempdata
			else
				local tempfile = fs.open("/usr/minux-main/debug.cfg","w")
				tempdata = "disabled"
				tempfile.writeLine(tempdata)
				tempfile.close()
				return tempdata
			end
		elseif value == "update" then
			if fs.exists("/usr/apt/update.cfg") == true then
				tempfile = fs.open("/usr/apt/update.cfg","r")
				tempdata = tempfile.readLine()
				tempfile.close()
			else
				tempdata = "disabled"
			end
			return tempdata
		elseif value == "welcome" then
			if fs.exists("/usr/minux-main/welcome.cfg") == false then
				return "disabled"
			else
				tempfile = fs.open("/usr/minux-main/welcome.cfg","r")
				tempdata = tempfile.readLine()
				tempfile.close()
				return tempdata
			end
		else
			return false
		end
	else
		return false
	end
end

function setconfig(config,setting)
	expect(1, config, "string")
	expect(2, setting, "string")
-- error catcher
	if _G.admin ~= true and _G.owner ~= _G.login then
		minux.debug("config:denied! not admin or owner" , "login")
		return false
	end
	minux.debug("config:login:granted" , "minux")

-- login type
	if config == "login" then
		minux.debug("config:option:login" , "minux")
		if setting == "local" or setting == "network" or setting == "disabled" then
			minux.debug("config:setting:"..setting , "minux")
			local file = fs.open("/etc/auth.cfg" , "w")
			file.write(setting)
			file.close()
			return true
		else
			minux.debug("config:login-invalid setting" , "minux")
			return false
		end
	elseif config == "update" then
		minux.debug("config:option:update" , "minux")
		if setting == "always" or setting == "enabled" then
			minux.debug("config:setting:"..setting , "minux")
			local tempfile = fs.open("/usr/apt/update.cfg","w")
			tempfile.writeLine(setting)
			tempfile.close()
			return true
		elseif setting == "disabled" then
			minux.debug("config:setting:disabled" , "minux")
			fs.delete("/usr/apt/update.cfg","w")
			return true
		else
			minux.debug("config:login-invalid setting" , "minux")
			return false
		end
	elseif config == "welcome" then
		minux.debug("config:option:update" , "minux")
		if setting == "enabled" or setting == "disabled" then
			minux.debug("config:setting:"..setting , "minux")
			local tempfile = fs.open("/usr/minux-main/welcome.cfg","w")
			tempfile.writeLine(setting)
			tempfile.close()
			return true
		else
			minux.debug("config:login-invalid setting" , "minux")
			return false
		end
	elseif config == "menu" or config == "ui" then
		minux.debug("config:option:menu" , "minux")
		if setting == "prompt" or setting == "menu" or setting == "workspace" or setting == "craftos" then
			minux.debug("config:setting:"..setting , "minux")
			tempfile = fs.open("/usr/minux-main/menu.cfg","w")
			tempfile.writeLine(setting)
			tempfile.close()
			return true
		else
			minux.debug("config:menu-invalid input" , "minux")
			return false
		end
	elseif config == "debug" then
		minux.debug("config:option:debug" , "minux")
		if setting == "enabled" or setting == "disabled" or setting == "logging" or setting == "full" then
			tempfile = fs.open("/usr/minux-main/debug.cfg","w")
			tempfile.writeLine(setting)
			tempfile.close()
			minux.debug("config:setting:"..setting , "minux")
			_G.debugmode = setting
			return true
		else
			minux.debug("config:debug-invalid setting" , "minux")
			return false
		end
	else
		minux.debug("config: invalid input" , "minux")
		return false
	end
end

-- bash colours
function bashcolor(textcolor, backgroundcolor, promptcolor)
	expect(1, textcolor, "string", "nil")
	expect(2, backgroundcolor, "string", "nil")
	expect(3, promptcolor, "string", "nil")
	if textcolor == nil then textcolor = "white" end
	if backgroundcolor == nil then backgroundcolor = "black" end
	if promptcolor == nil then promtcolor = "yellow" end
	local tempfile = fs.open("/usr/minux-main/bash/colours.cfg","w")
	tempfile.writeLine(textcolor)
	tempfile.writeLine(backgroundcolor)
	tempfile.writeLine(promptcolor)
	return true
end

function getbashcolor()
	returntable = { }
	if fs.exists("/usr/minux-main/bash/colours.cfg") and term.isColor() then
		local tempfile = fs.open("/usr/minux-main/bash/colours.cfg","r")
		tmptextColour = tempfile.readLine()
		tmpbgColour = tempfile.readLine()
		tmppromptColour = tempfile.readLine()
		if tmptextColour == "white" then textColour = colors.white end
		if tmptextColour == "orange" then textColour = colors.orange end
		if tmptextColour == "magenta" then textColour = colors.magenta end
		if tmptextColour == "lightBlue" then textColour = colors.lightBlue end
		if tmptextColour == "yellow" then textColour = colors.yellow end
		if tmptextColour == "lime" then textColour = colors.lime end
		if tmptextColour == "pink" then textColour = colors.pink end
		if tmptextColour == "gray" then textColour = colors.gray  end
		if tmptextColour == "lightGray" then textColour = colors.lightGray end
		if tmptextColour == "cyan" then textColour = colors.cyan end
		if tmptextColour == "purple" then textColour = colors.purple end
		if tmptextColour == "blue" then textColour = colors.blue end
		if tmptextColour == "brown" then textColour = colors.brown end
		if tmptextColour == "green" then textColour = colors.green end
		if tmptextColour == "red" then textColour = colors.red end
		if tmptextColour == "black" then textColour = colors.black end
		if tmpbgColour == "white" then bgColour = colors.white end
		if tmpbgColour == "orange" then bgColour = colors.orange end
		if tmpbgColour == "magenta" then bgColour = colors.magenta end
		if tmpbgColour == "lightBlue" then bgColour = colors.lightBlue end
		if tmpbgColour == "yellow" then bgColour = colors.yellow end
		if tmpbgColour == "lime" then bgColour = colors.lime end
		if tmpbgColour == "pink" then bgColour = colors.pink end
		if tmpbgColour == "gray" then bgColour = colors.gray  end
		if tmpbgColour == "lightGray" then bgColour = colors.lightGray end
		if tmpbgColour == "cyan" then bgColour = colors.cyan end
		if tmpbgColour == "purple" then bgColour = colors.purple end
		if tmpbgColour == "blue" then bgColour = colors.blue end
		if tmpbgColour == "brown" then bgColour = colors.brown end
		if tmpbgColour == "green" then bgColour = colors.green end
		if tmpbgColour == "red" then bgColour = colors.red end
		if tmpbgColour == "black" then bgColour = colors.black end
		if tmppromptColour == "white" then promptColour = colors.white end
		if tmppromptColour == "orange" then promptColour = colors.orange end
		if tmppromptColour == "magenta" then promptColour = colors.magenta end
		if tmppromptColour == "lightBlue" then promptColour = colors.lightBlue end
		if tmppromptColour == "yellow" then promptColour = colors.yellow end
		if tmppromptColour == "lime" then promptColour = colors.lime end
		if tmppromptColour == "pink" then promptColour = colors.pink end
		if tmppromptColour == "gray" then promptColour = colors.gray  end
		if tmppromptColour == "lightGray" then promptColour = colors.lightGray end
		if tmppromptColour == "cyan" then promptColour = colors.cyan end
		if tmppromptColour== "purple" then promptColour = colors.purple end
		if tmppromptColour == "blue" then promptColour = colors.blue end
		if tmppromptColour == "brown" then promptColour = colors.brown end
		if tmppromptColour == "green" then promptColour = colors.green end
		if tmppromptColour == "red" then promptColour = colors.red end
		if tmppromptColour == "black" then promptColour = colors.black end
		tempfile.close()
		returntable[1] = textColour
		returntable[2] = bgColour
		returntable[3] = promptColour
	elseif term.isColor() then
		returntable[1] = colors.green
		returntable[2] = colors.black
		returntable[3] = colors.red
	else
		returntable[1] = colors.white
		returntable[2] = colors.black
		returntable[3] = colors.gray
	end
	return returntable
end
--minux halt and restart
function halt()
	tempfile = fs.open("/temp/minux/boot/fail4.tmp" ,"w")
	tempfile.write("0")
	tempfile.close()
	os.shutdown()
end
function restart()
	tempfile = fs.open("/temp/minux/boot/fail4.tmp" ,"w")
	tempfile.write("0")
	tempfile.close()
	os.reboot()
end
-- minux logging
function debug(printstring, programname)
	expect(1, printstring, "string")
	expect(2, programname, "string","nil")
	if programname == nil then programname = "minux" end
	if _G.debugmode == "enabled" or _G.debugmode == "full" then
		print(printstring)
	elseif _G.debugmode == "logging" or _G.debugmode == "full" then
		logfile = fs.open("/var/log/"..programname..".txt" , "a")
		logfile.writeLine(printstring)
		logfile.close()
	end
	return true
end
--file/string api's
function countline(filename)
	expect(1, filename, "string")
	if fs.exists(filename) then
		local tempfile = fs.open(filename , "r")
		local linecount = 1
		local line = "start"
		while line ~= nil do
			line = tempfile.readLine()
			if line ~= nil then linecount = linecount + 1 end
		end
		tempfile.close()
		return linecount
	else
		return false
	end
end
function findline(filename,target)
	expect(1, filename, "string")
	expect(2, target, "string")
	if fs.exists(filename) then
		local linenumber = 1
		local tempfile = fs.open(filename , "r")
		local line = "start"
		while line ~= nil do 
			if string.find(line, target) ~= nil then tempfile.close() linenumber = linenumber - 1 return linenumber end
			line = tempfile.readLine()
			linenumber = linenumber + 1
		end
	end
	return false
end
function findfile(filename)
	expect(1, filename, "string")
	minux.lsr("/")
	local linenumber = 1
	local tempfile = fs.open("/temp/ls/files.ls","r")
	local line = "start"
	local hitsfound = 0
	local returntable = { }
	returntable[1] = "noresult"
	while line ~= nil do
		line = tempfile.readLine()
		if line ~= nil then
			if string.find(line,filename) ~= nil then
				hitsfound = hitsfound + 1
				returntable[hitsfound] = linenumber
			end
		end
		linenumber = linenumber + 1
	end
	tempfile.close()
	return returntable
end
function printline(filename, linenumber)
	expect(1, filename, "string")
	expect(2, linenumber, "number")
	if fs.exists(filename) == nil then return false end
	local tempfile = fs.open(filename , "r")
	linecounter = 0
	line = "start"
	stopnumber = tonumber(linenumber)
	while linecounter ~= stopnumber and line ~= nil do
		line = tempfile.readLine()
		linecounter = linecounter + 1 
	end
	tempfile.close()
	return line
end
function removeline(filename, linenumber)
	expect(1, filename, "string")
	expect(2, linenumber, "number")
	if fs.exists(filename) == false then return false end
	local tempfile = fs.open(filename, "r")
	local newfile = fs.open("/temp/dev/removeline.tmp","w")
	local line = "start"
	local counter = 0
	while line ~= nil do
		counter = counter + 1
		line = tempfile.readLine()
		if line ~= nil and counter ~= linenumber then newfile.writeLine(line) end
	end
	tempfile.close()
	newfile.close()
	fs.delete(filename)
	fs.move("/temp/dev/removeline.tmp",filename)
	return true
end
function removestring(filename, string)
	expect(1, filename, "string")
	expect(2, string, "string")
	if fs.exists(filename) == false then return false end
	local tempfile = fs.open(filename, "r")
	local newfile = fs.open("/temp/dev/removeline.tmp","w")
	local line = "start"
	while line ~= nil do
		line = tempfile.readLine()
		if line ~= nil and line ~= string then newfile.writeLine(line) end
	end
	tempfile.close()
	newfile.close()
	fs.delete(filename)
	fs.move("/temp/dev/removeline.tmp",filename)
	return true
end
function replaceline(filename,input, output)
	expect(1, filename, "string")
	expect(2, input, "string")
	expect(3, output, "string")
	if fs.exists(filename) == false then return false end
	local tempfile = fs.open(filename,"r")
	local newfile = fs.open("/temp/dev/removeline.tmp","w")
	local line = "start"
	while line ~= nil do
		line = tempfile.readLine()
		if line ~= nil and line ~= input then newfile.writeLine(line) end
		if line == input then newfile.writeLine(output) returnmsg = true end
	end
	tempfile.close()
	newfile.close()
	fs.delete(filename)
	fs.move("/temp/dev/removeline.tmp",filename)
	if returnmsg == true then return true
	else return false 
	end
end

function insertline(filename, input)
	expect(1, filename, "string")
	expect(2, input, "string")
	if fs.exists(filename) then
		local temp = fs.open(filename,"a")
		temp.writeLine(input)
		temp.close()
		return true
	else
		return false
	end
end

-- http get api
function download(adress, filepath)
	expect(1,adress, "string")
	expect(2,filepath, "string")
	local requestcount = 0
	local requestretry = false
	-- test the url
	if http.checkURL(adress) == true then
		requestretry = true
	else
		print("server unreachable")
		return false
	end
	-- try to download the file
	while requestcount ~= 3 and requestretry == true do
		minux.debug("minux:http-request:"..adress,"minux")
		getrequest = http.get(adress)
		request = getrequest.readAll()
		getrequest.close()
		if request ~= nil then
			requestretry = false
			tempfile = fs.open(filepath , "w")
			tempfile.write(request)
			tempfile.close()
			minux.debug("getpack:file retrieved","apt","minux")
		else
			requestcount = requestcount + 1
			minux.debug("http-get:failed attempt "..requestcount.."/3","minux")
			os.sleep(3)
		end
	end
	if requestcount == 3 and requestretry == true then
		minux.debug("http-get:attempt failed","minux")
		return false
	else
		minux.debug("http-get:file retrieved","minux")
		return true
	end
end
-- end minux api's
