-- minux main api's
-- recursive listing
function lsr(searchpath) os.run({}, "/etc/minux-main/ls/ls.sys" , searchpath) end
-- login system
function login(login, password) os.run({}, "/bin/login.sh" , login , password) end
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 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
-- minux monitorprint
function monitorprint(printstring) os.run({}, "/etc/minux-main/sys/monitorprint.sys" , printstring) end
-- minux configuration
function getconfig(value)
	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)
-- error catcher
	if setting == nil or setting == "" then
		return false
	end
	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)
	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)
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) os.run({},"/etc/minux-main/sys/debug.sys" , printstring, programname) end
--file/string api's
function countline(filename)
	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)
	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)
	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)
	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)
	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)
	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)
	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
-- end minux api's
